Bases: BaseRedisFastProducer
Producer that routes channel operations through the sync cluster.
Source code in faststream/redis/publisher/producer.py
204
205
206
207
208
209
210
211 | 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 | @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
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
292
293
294
295 | @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:
response_msg = None
while response_msg is None:
response_msg = await psub.get_message(
ignore_subscribe_messages=True,
timeout=cmd.timeout,
)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 | @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
|