-
Notifications
You must be signed in to change notification settings - Fork 25
implement redis cluster broker #47
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,78 @@ | ||
from typing import Any, AsyncGenerator, Callable, Optional, TypeVar | ||
|
||
from redis.asyncio import RedisCluster | ||
from taskiq.abc.broker import AsyncBroker | ||
from taskiq.abc.result_backend import AsyncResultBackend | ||
from taskiq.message import BrokerMessage | ||
|
||
_T = TypeVar("_T") | ||
|
||
|
||
class BaseRedisClusterBroker(AsyncBroker): | ||
"""Base broker that works with Redis Cluster.""" | ||
|
||
def __init__( | ||
self, | ||
url: str, | ||
task_id_generator: Optional[Callable[[], str]] = None, | ||
result_backend: Optional[AsyncResultBackend[_T]] = None, | ||
queue_name: str = "taskiq", | ||
max_connection_pool_size: int = 2**31, | ||
**connection_kwargs: Any, | ||
) -> None: | ||
""" | ||
Constructs a new broker. | ||
|
||
:param url: url to redis. | ||
:param task_id_generator: custom task_id generator. | ||
:param result_backend: custom result backend. | ||
:param queue_name: name for a list in redis. | ||
:param max_connection_pool_size: maximum number of connections in pool. | ||
:param connection_kwargs: additional arguments for aio-redis ConnectionPool. | ||
""" | ||
super().__init__( | ||
result_backend=result_backend, | ||
task_id_generator=task_id_generator, | ||
) | ||
|
||
self.redis: RedisCluster[bytes] = RedisCluster.from_url( | ||
url=url, | ||
max_connections=max_connection_pool_size, | ||
**connection_kwargs, | ||
) | ||
|
||
self.queue_name = queue_name | ||
|
||
async def shutdown(self) -> None: | ||
"""Closes redis connection pool.""" | ||
await self.redis.aclose() # type: ignore[attr-defined] | ||
await super().shutdown() | ||
|
||
|
||
class ListQueueClusterBroker(BaseRedisClusterBroker): | ||
"""Broker that works with Redis Cluster and distributes tasks between workers.""" | ||
|
||
async def kick(self, message: BrokerMessage) -> None: | ||
""" | ||
Put a message in a list. | ||
|
||
This method appends a message to the list of all messages. | ||
|
||
:param message: message to append. | ||
""" | ||
await self.redis.lpush(self.queue_name, message.message) | ||
|
||
async def listen(self) -> AsyncGenerator[bytes, None]: | ||
""" | ||
Listen redis queue for new messages. | ||
|
||
This function listens to the queue | ||
and yields new messages if they have BrokerMessage type. | ||
|
||
:yields: broker messages. | ||
""" | ||
redis_brpop_data_position = 1 | ||
while True: | ||
yield (await self.redis.brpop([self.queue_name]))[ | ||
redis_brpop_data_position | ||
] |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.