Bases: BaseRedisFastProducer
Producer that routes channel operations through the sync cluster.
Source code in faststream/redis/publisher/producer.py
197
198
199
200
201
202
203
204 | def __init__(
self,
connection: "ConnectionState[RedisCluster[bytes]]",
cluster_state: RedisClusterConnectionState,
**kwargs: Any,
) -> None:
super().__init__(connection=connection, **kwargs)
self._cluster_state = cluster_state
|
codec instance-attribute
codec = codec or DefaultCodec()
serializer instance-attribute
publish async
Source code in faststream/redis/publisher/producer.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 | @override
async def publish(self, cmd: "RedisPublishCommand") -> int | bytes:
msg = await cmd.message_format.encode(
message=cmd.body,
reply_to=cmd.reply_to,
headers=cmd.headers,
correlation_id=cmd.correlation_id or "",
serializer=self.serializer,
codec=self.codec,
)
if cmd.destination_type is DestinationType.Channel:
return await self._cluster_state.sync_publish(cmd.destination, msg)
if cmd.destination_type is DestinationType.List:
return cast("int", await self._connection.client.rpush(cmd.destination, msg))
if cmd.destination_type is DestinationType.Stream:
return cast(
"bytes",
await self._connection.client.xadd(
name=cmd.destination,
fields={DATA_KEY: msg},
maxlen=cmd.maxlen,
),
)
raise UnreachablePathError
|
request async
Source code in faststream/redis/publisher/producer.py
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291 | @override
async def request(self, cmd: "RedisPublishCommand") -> "Any":
nuid = NUID()
reply_to = str(nuid.next(), "utf-8")
psub = self._cluster_state.pubsub()
try:
await psub.subscribe(reply_to)
msg = await cmd.message_format.encode(
message=cmd.body,
reply_to=reply_to,
headers=cmd.headers,
correlation_id=cmd.correlation_id or "",
serializer=self.serializer,
codec=self.codec,
)
if cmd.destination_type is DestinationType.Channel:
await self._cluster_state.sync_publish(cmd.destination, msg)
elif cmd.destination_type is DestinationType.List:
await self._connection.client.rpush(cmd.destination, msg)
elif cmd.destination_type is DestinationType.Stream:
await self._connection.client.xadd(
name=cmd.destination,
fields={DATA_KEY: msg},
maxlen=cmd.maxlen,
)
else:
raise UnreachablePathError
with anyio.fail_after(cmd.timeout) as scope:
await psub.get_message(
ignore_subscribe_messages=True,
timeout=cmd.timeout or 0.0,
)
response_msg = await psub.get_message(
ignore_subscribe_messages=True,
timeout=cmd.timeout or 0.0,
)
if scope.cancel_called:
raise TimeoutError
return response_msg
finally:
with suppress(Exception):
await psub.unsubscribe()
await psub.aclose()
|
publish_batch async
Source code in faststream/redis/publisher/producer.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 | @override
async def publish_batch(self, cmd: "RedisPublishCommand") -> int:
batch = [
await cmd.message_format.encode(
message=msg,
correlation_id=cmd.correlation_id or "",
reply_to=cmd.reply_to,
headers=cmd.headers,
serializer=self.serializer,
codec=self.codec,
)
for msg in cmd.batch_bodies
]
connection = cmd.pipeline or self._connection.client
return cast("int", await connection.rpush(cmd.destination, *batch))
|
connect
connect(
serializer: Optional[SerializerProto] = None,
codec: Optional[CodecProto] = None,
) -> None
Source code in faststream/redis/publisher/producer.py
| def connect(
self,
serializer: Optional["SerializerProto"] = None,
codec: Optional["CodecProto"] = None,
) -> None:
self.serializer = serializer
if codec is not None:
self.codec = codec
|