-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve callback handlers #134
Changes from 2 commits
939c749
f06ccc9
641c0e8
dc88983
55e7ea0
7016cfc
25c6cbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
import time | ||
from collections import defaultdict | ||
from contextlib import suppress | ||
from dataclasses import dataclass | ||
from typing import ( | ||
Annotated, | ||
Any, | ||
|
@@ -57,6 +58,12 @@ class Addr(NamedTuple): | |
port: int | ||
|
||
|
||
@dataclass | ||
class FrameEntry: | ||
handler: HT[Any] | ||
frame: schema.Frame | ||
|
||
|
||
class BaseClient: | ||
def __init__( | ||
self, | ||
|
@@ -95,6 +102,7 @@ def __init__( | |
|
||
self._last_heartbeat: float = 0 | ||
self._connection_closed_handler = connection_closed_handler | ||
self._frames: asyncio.Queue = asyncio.Queue() | ||
|
||
def start_task(self, name: str, coro: Awaitable[None]) -> None: | ||
assert name not in self._tasks | ||
|
@@ -172,9 +180,22 @@ async def start(self) -> None: | |
self._conn = Connection(self.host, self.port, self._ssl_context) | ||
await self._conn.open() | ||
self.start_task("listener", self._listener()) | ||
self.start_task("run_delivery_handlers", self._run_delivery_handlers()) | ||
self.add_handler(schema.Heartbeat, self._on_heartbeat) | ||
self.add_handler(schema.Close, self._on_close) | ||
|
||
async def _run_delivery_handlers(self): | ||
|
||
while True: | ||
try: | ||
frame_entry = await self._frames.get() | ||
maybe_coro = await frame_entry.handler(frame_entry.frame) | ||
if maybe_coro is not None: | ||
await maybe_coro | ||
except Exception as e: | ||
logger.debug("Error reading item from _run_delivery_handlers: " + str(e)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems to be a critical error, probably shouldn't be This try/except wraps both library code and user-provided callback, which is why it's difficult to handle errors carefully. But I think at least we should log an error message (so the user can get a monitoring alert) and if we choose to suppress such kind of errors, the loop shouldn't be stopped: while True:
try:
...
except Exception:
logger.exception("Error while handlilng %s frame", frame.__class__)
# no break |
||
break | ||
|
||
async def _listener(self) -> None: | ||
assert self._conn | ||
while True: | ||
|
@@ -206,9 +227,12 @@ async def _listener(self) -> None: | |
|
||
for _, handler in self._handlers.get(frame.__class__, {}).items(): | ||
try: | ||
maybe_coro = handler(frame) | ||
if maybe_coro is not None: | ||
await maybe_coro | ||
if frame.__class__ == schema.Deliver: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This special handling of Deliver frame feels kinda hacky, maybe move this to |
||
await self._frames.put(FrameEntry(handler, frame)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can get a corresponding handler for a frame when receiving it from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HI @qweeze do you mean to get it from self._handlers in _run_delivery_handlers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh never mind I think I've understood now! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, exactly |
||
else: | ||
maybe_coro = handler(frame) | ||
if maybe_coro is not None: | ||
await maybe_coro | ||
except Exception: | ||
logger.exception("Error while running handler %s of frame %s", handler, frame) | ||
|
||
|
@@ -243,6 +267,7 @@ def is_started(self) -> bool: | |
|
||
async def close(self) -> None: | ||
logger.info("Stopping client %s:%s", self.host, self.port) | ||
|
||
if self._conn is None: | ||
return | ||
|
||
|
@@ -256,6 +281,7 @@ async def close(self) -> None: | |
resp_schema=schema.CloseResponse, | ||
) | ||
|
||
await self.stop_task("run_delivery_handlers") | ||
await self.stop_task("listener") | ||
|
||
await self._conn.close() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,6 +107,24 @@ async def consumer(pytestconfig, ssl_context): | |
await consumer.close() | ||
|
||
|
||
@pytest.fixture() | ||
async def test_consumer_close(pytestconfig, ssl_context): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixture fully duplicates the previous one, do we need it? |
||
consumer = Consumer( | ||
host=pytestconfig.getoption("rmq_host"), | ||
port=pytestconfig.getoption("rmq_port"), | ||
ssl_context=ssl_context, | ||
username=pytestconfig.getoption("rmq_username"), | ||
password=pytestconfig.getoption("rmq_password"), | ||
frame_max=1024 * 1024, | ||
heartbeat=60, | ||
) | ||
await consumer.start() | ||
try: | ||
yield consumer | ||
finally: | ||
await consumer.close() | ||
|
||
|
||
@pytest.fixture() | ||
async def producer(pytestconfig, ssl_context): | ||
producer = Producer( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an extra
await
hereThis thing with
if maybe_coro is not None
was introduced to allow to have both sync and async handlers. The equivalent code would be(but if the handler is sync, there's no point in moving it to another asyncio.Task)