-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rework: threads all the way down (#44)
A pretty sizeable rework to the underlying code but no changes to the API ALL flushing to Supergood is done in a separate thread. Currently the library uses a synchronous scheme when it detects it is operating in a fork. This was in an effort to optimize data integrity, but it does add additional delay. Instead, a worker thread is spun up just like it would have been on the main thread. What this means is that sometimes a process that shuts down too quickly after finishing their HTTP calls won't end up flushing to Supergood. In the future, we intend on utilizing a separately deployable agent that can manage all this off-process, but for now this is my best effort to balance data integrity without compromising speed. --------- Co-authored-by: Steve Bunting <steve@supergood.ai>
- Loading branch information
1 parent
1480cba
commit 7ed21c8
Showing
20 changed files
with
356 additions
and
433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import os | ||
import threading | ||
from collections.abc import Callable | ||
from queue import Empty, Full, Queue | ||
from time import sleep | ||
from typing import Optional | ||
|
||
|
||
# A version of the threading.Timer class with 2 differences: | ||
# - instead of running the function once after <interval> seconds, it loops and runs again <interval> seconds later | ||
# - also, runs the provided function immediately on start (for initital config fetch) | ||
class Repeater(threading.Timer): | ||
def run(self): | ||
if not self.finished.is_set(): | ||
self.function(*self.args, **self.kwargs) | ||
while not self.finished.wait(self.interval): | ||
self.function(*self.args, **self.kwargs) | ||
|
||
|
||
class Worker: | ||
def __init__(self, repeater): | ||
# type: (Callable[[dict],None], Optional[int]) -> None | ||
# print(f"[{os.getpid()}] worker init") | ||
self._queue = Queue(42) | ||
self._lock = threading.Lock() | ||
self._thread = None | ||
self._pid = None | ||
self._fn = repeater | ||
|
||
@property | ||
def is_alive(self): | ||
# type: () -> bool | ||
if self._pid != os.getpid(): | ||
# This case occurs when an initialized client has been forked | ||
# threads do not get persisted on fork, so they must be re-started | ||
return False | ||
if not self._thread: | ||
return True | ||
return self._thread.is_alive() | ||
|
||
def _ensure_running(self): | ||
# type: () -> None | ||
if not self.is_alive: | ||
self.start() | ||
|
||
def start(self): | ||
# type: () -> None | ||
with self._lock: | ||
if not self.is_alive: | ||
self._thread = threading.Thread( | ||
target=self._run, name="supergood-repeater" | ||
) | ||
self._thread.daemon = True | ||
try: | ||
self._thread.start() | ||
self._pid = os.getpid() | ||
except RuntimeError: | ||
# thread init failed. | ||
# May be out of available thread ids, or shutting down | ||
self._thread = None | ||
|
||
def flush(self): | ||
# type: () -> None | ||
with self._lock: | ||
if self._thread: | ||
try: | ||
self._queue.put_nowait({}) | ||
except Full: | ||
# full, drop events | ||
pass | ||
|
||
def kill(self): | ||
# type: () -> None | ||
with self._lock: | ||
if self._thread: | ||
try: | ||
self._queue.put_nowait(None) | ||
except Full: | ||
# full, drop events | ||
pass | ||
self._thread = None | ||
self._pid = None | ||
|
||
def append(self, entry): | ||
# type: (dict) -> None | ||
self._ensure_running() | ||
with self._lock: | ||
try: | ||
self._queue.put(entry) | ||
return True | ||
except Full as e: | ||
# full, drop events | ||
return False | ||
|
||
def _run(self): | ||
# type: () -> None | ||
while True: | ||
entries = {} | ||
# get() blocks here. it should receive a None object to terminate gracefully | ||
entry = self._queue.get() | ||
if entry is None: | ||
# terminate | ||
return | ||
entries.update(entry) | ||
# once we've gotten _a_ thing, check to see if we can bundle a few together. Up to 10 | ||
terminate = False | ||
for _ in range(10): | ||
try: | ||
entry = self._queue.get_nowait() | ||
if entry is None: | ||
# terminate | ||
terminate = True | ||
break | ||
entries.update(entry) | ||
except Empty: | ||
# nothing else to do immediately, flush what you got | ||
break | ||
|
||
if len(entries) != 0: | ||
# TODO: invoke this with a timeout? | ||
self._fn(entries) | ||
elif terminate: | ||
return | ||
else: | ||
sleep(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,48 @@ | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
|
||
from supergood import Client | ||
from tests.helper import get_config, get_remote_config | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def broken_redaction(session_mocker): | ||
session_mocker.patch( | ||
"supergood.client.redact_values", side_effect=Exception | ||
).start() | ||
yield session_mocker | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
@pytest.fixture(scope="function") | ||
def monkeysession(): | ||
with pytest.MonkeyPatch.context() as mp: | ||
yield mp | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def broken_client(broken_redaction, monkeysession): | ||
config = get_config() | ||
remote_config = get_remote_config() | ||
broken_redaction.patch("supergood.api.Api.post_events", return_value=None).start() | ||
broken_redaction.patch("supergood.api.Api.post_errors", return_value=None).start() | ||
broken_redaction.patch( | ||
"supergood.api.Api.get_config", return_value=remote_config | ||
).start() | ||
Client.initialize( | ||
client_id="client_id", | ||
client_secret_id="client_secret_id", | ||
base_url="https://api.supergood.ai", | ||
telemetry_url="https://telemetry.supergood.ai", | ||
config=config, | ||
) | ||
monkeysession.setenv("SG_OVERRIDE_AUTO_FLUSH", "false") | ||
monkeysession.setenv("SG_OVERRIDE_AUTO_CONFIG", "false") | ||
Client._get_config() | ||
yield Client | ||
Client.kill() # on exit | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def supergood_client(request, session_mocker, monkeysession): | ||
# Allows for a param dictionary to control behavior | ||
# currently looks for "config" "remote_config" and "auto" | ||
config = get_config() | ||
auto = False | ||
remote_config = get_remote_config() | ||
if getattr(request, "param", None): | ||
if request.param.get("config", None): | ||
config = request.param["config"] | ||
if request.param.get("remote_config", None): | ||
remote_config = request.param["remote_config"] | ||
if request.param.get("auto", None): | ||
auto = request.param["auto"] | ||
|
||
session_mocker.patch("supergood.api.Api.post_events", return_value=None).start() | ||
session_mocker.patch("supergood.api.Api.post_errors", return_value=None).start() | ||
session_mocker.patch( | ||
"supergood.api.Api.get_config", return_value=remote_config | ||
).start() | ||
session_mocker.patch("supergood.api.Api.post_telemetry", return_value=None).start() | ||
|
||
if not auto: | ||
monkeysession.setenv("SG_OVERRIDE_AUTO_FLUSH", "false") | ||
monkeysession.setenv("SG_OVERRIDE_AUTO_CONFIG", "false") | ||
|
||
Client.initialize( | ||
client_id="client_id", | ||
client_secret_id="client_secret_id", | ||
base_url="https://api.supergood.ai", | ||
telemetry_url="https://telemetry.supergood.ai", | ||
config=config, | ||
) | ||
Client._get_config() | ||
yield Client | ||
Client.kill() # on exit | ||
@pytest.fixture | ||
def supergood_client(request, mocker): | ||
with pytest.MonkeyPatch.context() as mp: | ||
config = get_config() | ||
remote_config = get_remote_config() | ||
|
||
if getattr(request, "param", None): | ||
if request.param.get("config", None): | ||
config = request.param["config"] | ||
if request.param.get("remote_config", None): | ||
remote_config = request.param["remote_config"] | ||
|
||
Client.initialize( | ||
client_id="client_id", | ||
client_secret_id="client_secret_id", | ||
base_url="https://api.supergood.ai", | ||
telemetry_url="https://telemetry.supergood.ai", | ||
config=config, | ||
) | ||
# first 3 are just to make sure we don't post anything externally | ||
mocker.patch("supergood.api.Api.post_events", return_value=None).start() | ||
mocker.patch("supergood.api.Api.post_errors", return_value=None).start() | ||
mocker.patch("supergood.api.Api.post_telemetry", return_value=None).start() | ||
# next we make sure we don't call get externally, and stub in our remote config | ||
mocker.patch("supergood.api.Api.get_config", return_value=remote_config).start() | ||
# Turns off the worker, pytest mocks don't always play well with threads | ||
mocker.patch("supergood.worker.Worker.start", return_value=None).start() | ||
mocker.patch("supergood.worker.Repeater.start", return_value=None).start() | ||
mocker.patch("supergood.worker.Worker.append", return_value=True).start() | ||
mp.setenv("SG_OVERRIDE_AUTO_FLUSH", "false") | ||
mp.setenv("SG_OVERRIDE_AUTO_CONFIG", "false") | ||
Client._get_config() | ||
yield Client | ||
Client.kill() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.