-
Notifications
You must be signed in to change notification settings - Fork 389
feat(redis): support cluster publishing pipelines #3096
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import asyncio | ||
|
|
||
| from faststream.redis import RedisClusterBroker | ||
|
|
||
| broker = RedisClusterBroker("redis://127.0.0.1:7001") | ||
|
|
||
|
|
||
| async def increment_and_publish( | ||
| broker: RedisClusterBroker, | ||
| key: str = "orders", | ||
| ) -> list[int | bytes]: | ||
| client = await broker.connect() | ||
| async with client.pipeline(transaction=True) as pipe: | ||
| pipe.incr(f"{{{key}}}:count") | ||
| await broker.publish( | ||
| "created", | ||
| stream=f"{{{key}}}:events", | ||
| pipeline=pipe, | ||
| ) | ||
| return await pipe.execute() | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| async with broker: | ||
| await increment_and_publish(broker) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ | |
| from types import TracebackType | ||
|
|
||
| from redis.asyncio.client import Pipeline, Redis | ||
| from redis.asyncio.cluster import ClusterPipeline | ||
|
|
||
| from faststream._internal.basic_types import SendableMessage | ||
| from faststream.redis.message import RedisChannelMessage | ||
|
|
@@ -204,7 +205,7 @@ async def publish( | |
| list: str | None = None, | ||
| stream: None = None, | ||
| maxlen: int | None = None, | ||
| pipeline: Optional["Pipeline[bytes]"] = None, | ||
| pipeline: None = None, | ||
| ) -> int: ... | ||
|
|
||
| @overload | ||
|
|
@@ -219,9 +220,24 @@ async def publish( | |
| list: str | None = None, | ||
| stream: str = ..., | ||
| maxlen: int | None = None, | ||
| pipeline: Optional["Pipeline[bytes]"] = None, | ||
| pipeline: None = None, | ||
| ) -> bytes: ... | ||
|
|
||
| @overload | ||
| async def publish( | ||
| self, | ||
| message: "SendableMessage" = None, | ||
| channel: str | None = None, | ||
| *, | ||
| reply_to: str = "", | ||
| headers: dict[str, Any] | None = None, | ||
| correlation_id: str | None = None, | ||
| list: str | None = None, | ||
| stream: str | None = None, | ||
| maxlen: int | None = None, | ||
| pipeline: "Pipeline[bytes]", | ||
| ) -> "Pipeline[bytes]": ... | ||
|
|
||
| @override | ||
| async def publish( | ||
| self, | ||
|
|
@@ -234,8 +250,8 @@ async def publish( | |
| list: str | None = None, | ||
| stream: str | None = None, | ||
| maxlen: int | None = None, | ||
| pipeline: Optional["Pipeline[bytes]"] = None, | ||
| ) -> int | bytes: | ||
| pipeline: Optional["Pipeline[bytes] | ClusterPipeline[bytes]"] = None, | ||
| ) -> "int | bytes | Pipeline[bytes] | ClusterPipeline[bytes]": | ||
| """Publish message directly. | ||
|
|
||
| This method allows you to publish a message in a non-AsyncAPI-documented way. | ||
|
|
@@ -259,10 +275,11 @@ async def publish( | |
| maxlen: | ||
| Redis Stream maxlen publish option. Remove eldest message if maxlen exceeded. | ||
| pipeline: | ||
| Redis pipeline to use for publishing messages. | ||
| Redis pipeline to use for publishing messages. Queued commands run on | ||
| pipeline.execute(). | ||
|
|
||
| Returns: | ||
| int: The result of the publish operation, typically the number of messages published. | ||
| The publish result, or the pipeline when the command is queued. | ||
| """ | ||
| cmd = RedisPublishCommand( | ||
| message, | ||
|
|
@@ -278,7 +295,9 @@ async def publish( | |
| message_format=self.message_format, | ||
| ) | ||
|
|
||
| result: int | bytes = await super()._basic_publish( | ||
| result: ( | ||
| int | bytes | Pipeline[bytes] | ClusterPipeline[bytes] | ||
|
Collaborator
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. only Pipeline. It can only be used with ClusterRedis |
||
| ) = await super()._basic_publish( | ||
| cmd, | ||
| producer=self.config.producer, | ||
| ) | ||
|
|
@@ -315,16 +334,38 @@ async def request( # type: ignore[override] | |
| ) | ||
| return msg | ||
|
|
||
| @overload # type: ignore[override] | ||
| async def publish_batch( | ||
| self, | ||
| *messages: "SendableMessage", | ||
| list: str, | ||
| correlation_id: str | None = None, | ||
| reply_to: str = "", | ||
| headers: dict[str, Any] | None = None, | ||
| pipeline: None = None, | ||
| ) -> int: ... | ||
|
|
||
| @overload | ||
| async def publish_batch( | ||
| self, | ||
| *messages: "SendableMessage", | ||
| list: str, | ||
| correlation_id: str | None = None, | ||
| reply_to: str = "", | ||
| headers: dict[str, Any] | None = None, | ||
| pipeline: "Pipeline[bytes]", | ||
| ) -> "Pipeline[bytes]": ... | ||
|
|
||
| @override | ||
| async def publish_batch( # type: ignore[override] | ||
| async def publish_batch( | ||
| self, | ||
| *messages: "SendableMessage", | ||
| list: str, | ||
| correlation_id: str | None = None, | ||
| reply_to: str = "", | ||
| headers: dict[str, Any] | None = None, | ||
| pipeline: Optional["Pipeline[bytes]"] = None, | ||
| ) -> int: | ||
| pipeline: Optional["Pipeline[bytes] | ClusterPipeline[bytes]"] = None, | ||
| ) -> "int | Pipeline[bytes] | ClusterPipeline[bytes]": | ||
|
Comment on lines
+367
to
+368
Collaborator
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. only Pipeline. It can only be used with ClusterRedis |
||
| """Publish multiple messages to Redis List by one request. | ||
|
|
||
| Args: | ||
|
|
@@ -336,7 +377,7 @@ async def publish_batch( # type: ignore[override] | |
| pipeline: Redis pipeline to use for publishing messages. | ||
|
|
||
| Returns: | ||
| int: The result of the batch publish operation. | ||
| The batch publish result, or the pipeline when it is queued. | ||
| """ | ||
| cmd = RedisPublishCommand( | ||
| *messages, | ||
|
|
@@ -349,7 +390,9 @@ async def publish_batch( # type: ignore[override] | |
| message_format=self.message_format, | ||
| ) | ||
|
|
||
| result: int = await self._basic_publish_batch( | ||
| result: ( | ||
| int | Pipeline[bytes] | ClusterPipeline[bytes] | ||
|
Collaborator
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. only Pipeline. It can only be used with ClusterRedis |
||
| ) = await self._basic_publish_batch( | ||
| cmd, | ||
| producer=self.config.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.
only Pipeline. It can only be used with ClusterRedis