Skip to content
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

feat: async pubsub #381

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

feat: async pubsub #381

wants to merge 1 commit into from

Conversation

jamagalhaes
Copy link
Collaborator

@jamagalhaes jamagalhaes commented Jan 31, 2025

in response to #333 (comment)

Base automatically changed from fix/embed-synchronizer to main February 2, 2025 18:21
@@ -26,7 +26,7 @@ def wrapper(self: "Http", *args: Any, **kwargs: Any) -> Any:
result = fn(self, *args, **kwargs)
end_time = time.time()
timing = int((end_time - start_time) * 1000)
self.statsd_client.timing(f"http_{fn.__name__}", timing)
self.statsd_client.timing(f"plugins.http_{fn.__name__}", timing)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is now in main

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@djantzen This branch was originally based on your PR #333. Since that branch is no longer available (it was merged), the target was automatically switched to the main branch. You merged #333 yesterday, but there were some comments related to this PR. Please take a look.

@@ -10,12 +12,18 @@ class PubSubLogHandler(logging.Handler):

def __init__(self) -> None:
self.publisher = Publisher()
self.async_publisher = AsyncPublisher()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also use this in the synchronizer? Massively lower volume of messages to be published there, but would be a win for consistency perhaps.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async_publisher will be used within the synchronizer each time you do log.<level> because synchronizer runs in an async context

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean when publishing the message to pubsub for other containers to reload

@jamagalhaes jamagalhaes requested a review from csande February 3, 2025 18:44
return redis.Redis.from_url(self.redis_endpoint, decode_responses=True)

return None
def _create_client(self) -> redis.Redis | redis.asyncio.Redis | None: ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want PubSubBase to be an abstract base class, we should have it inherit from ABC and mark this method as an abstractmethod.

Suggested change
def _create_client(self) -> redis.Redis | redis.asyncio.Redis | None: ...
@abstractmethod
def _create_client(self) -> redis.Redis | redis.asyncio.Redis | None: ...

try:
loop = asyncio.get_running_loop()
loop.create_task(self.async_publisher.publish(message))
except RuntimeError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lack of specificity of this exception makes me mildly concerned since there are two actions in the try. I don't know if this is a valid concern or not since I don't know what could cause create_task to raise, but what would you think about this small refactor?

try:
    loop = asyncio.get_running_loop()
except RuntimeError:
    loop = None

if loop:
    loop.create_task(self.async_publisher.publish(message))
else:
    self.publisher.publish(message)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants