Coverage for faststream/redis/publisher/producer.py: 94%

106 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1from contextlib import suppress 

2from typing import TYPE_CHECKING, Any, Optional, cast 

3 

4import anyio 

5from typing_extensions import override 

6 

7from faststream._internal.endpoint.utils import ParserComposition 

8from faststream._internal.parser import DefaultCodec 

9from faststream._internal.producer import ProducerProto 

10from faststream._internal.utils.nuid import NUID 

11from faststream.redis.configs.state import RedisClusterConnectionState 

12from faststream.redis.exceptions import UnreachablePathError 

13from faststream.redis.message import DATA_KEY 

14from faststream.redis.parser import RedisPubSubParser, SimpleParserConfig 

15from faststream.redis.response import DestinationType, RedisPublishCommand 

16 

17if TYPE_CHECKING: 

18 from fast_depends.library.serializer import SerializerProto 

19 from redis.asyncio.client import Redis 

20 from redis.asyncio.cluster import RedisCluster 

21 

22 from faststream._internal.parser import CodecProto 

23 from faststream._internal.types import CustomCallable 

24 from faststream.redis.configs import ConnectionState 

25 from faststream.redis.parser import MessageFormat 

26 

27 

28class BaseRedisFastProducer(ProducerProto[RedisPublishCommand]): 

29 """Shared logic for Redis producers.""" 

30 

31 __slots__ = ( 

32 "_connection", 

33 "_decoder", 

34 "_parser", 

35 "codec", 

36 "serializer", 

37 ) 

38 

39 _connection: "ConnectionState[Any]" 

40 _decoder: "ParserComposition" 

41 _parser: "ParserComposition" 

42 

43 def __init__( 

44 self, 

45 connection: "ConnectionState[Any]", 

46 parser: Optional["CustomCallable"], 

47 decoder: Optional["CustomCallable"], 

48 message_format: type["MessageFormat"], 

49 serializer: Optional["SerializerProto"], 

50 codec: Optional["CodecProto"] = None, 

51 ) -> None: 

52 self._connection = connection 

53 

54 default = RedisPubSubParser(SimpleParserConfig(message_format)) 

55 self._parser = ParserComposition( 

56 parser, 

57 default.parse_message, 

58 ) 

59 self._decoder = ParserComposition( 

60 decoder, 

61 default.decode_message, 

62 ) 

63 self.serializer = serializer 

64 self.codec = codec or DefaultCodec() 

65 

66 @override 

67 async def publish_batch(self, cmd: "RedisPublishCommand") -> int: 

68 batch = [ 

69 await cmd.message_format.encode( 

70 message=msg, 

71 correlation_id=cmd.correlation_id or "", 

72 reply_to=cmd.reply_to, 

73 headers=cmd.headers, 

74 serializer=self.serializer, 

75 codec=self.codec, 

76 ) 

77 for msg in cmd.batch_bodies 

78 ] 

79 

80 connection = cmd.pipeline or self._connection.client 

81 return cast("int", await connection.rpush(cmd.destination, *batch)) 

82 

83 def connect( 

84 self, 

85 serializer: Optional["SerializerProto"] = None, 

86 codec: Optional["CodecProto"] = None, 

87 ) -> None: 

88 self.serializer = serializer 

89 if codec is not None: 89 ↛ exitline 89 didn't return from function 'connect' because the condition on line 89 was always true

90 self.codec = codec 

91 

92 def _build_child( 

93 self, **kwargs: Any 

94 ) -> "RedisFastProducer | RedisClusterFastProducer": 

95 return self.__class__(**kwargs) # type: ignore[return-value] 

96 

97 

98class RedisFastProducer(BaseRedisFastProducer): 

99 """Producer for a single-node Redis.""" 

100 

101 __slots__ = () 

102 

103 _connection: "ConnectionState[Redis[bytes]]" 

104 

105 def __init__( 

106 self, 

107 connection: "ConnectionState[Redis[bytes]]", 

108 parser: Optional["CustomCallable"], 

109 decoder: Optional["CustomCallable"], 

110 message_format: type["MessageFormat"], 

111 serializer: Optional["SerializerProto"], 

112 ) -> None: 

113 super().__init__( 

114 connection=connection, 

115 parser=parser, 

116 decoder=decoder, 

117 message_format=message_format, 

118 serializer=serializer, 

119 ) 

120 

121 @override 

122 async def publish(self, cmd: "RedisPublishCommand") -> int | bytes: 

123 msg = await cmd.message_format.encode( 

124 message=cmd.body, 

125 reply_to=cmd.reply_to, 

126 headers=cmd.headers, 

127 correlation_id=cmd.correlation_id or "", 

128 serializer=self.serializer, 

129 codec=self.codec, 

130 ) 

131 

132 return await self.__publish(msg, cmd) 

133 

134 async def __publish( 

135 self, 

136 msg: bytes, 

137 cmd: "RedisPublishCommand", 

138 ) -> int | bytes: 

139 connection = cmd.pipeline or self._connection.client 

140 

141 if cmd.destination_type is DestinationType.Channel: 

142 return await connection.publish(cmd.destination, msg) 

143 

144 if cmd.destination_type is DestinationType.List: 

145 return await connection.rpush(cmd.destination, msg) 

146 

147 if cmd.destination_type is DestinationType.Stream: 147 ↛ 157line 147 didn't jump to line 157 because the condition on line 147 was always true

148 return cast( 

149 "bytes", 

150 await connection.xadd( 

151 name=cmd.destination, 

152 fields={DATA_KEY: msg}, 

153 maxlen=cmd.maxlen, 

154 ), 

155 ) 

156 

157 error_msg = "unreachable" 

158 raise AssertionError(error_msg) 

159 

160 @override 

161 async def request(self, cmd: "RedisPublishCommand") -> "Any": 

162 nuid = NUID() 

163 reply_to = str(nuid.next(), "utf-8") 

164 psub = self._connection.client.pubsub() 

165 

166 try: 

167 await psub.subscribe(reply_to) 

168 

169 msg = await cmd.message_format.encode( 

170 message=cmd.body, 

171 reply_to=reply_to, 

172 headers=cmd.headers, 

173 correlation_id=cmd.correlation_id or "", 

174 serializer=self.serializer, 

175 codec=self.codec, 

176 ) 

177 

178 await self.__publish(msg, cmd) 

179 

180 with anyio.fail_after(cmd.timeout) as scope: 

181 response_msg = None 

182 while response_msg is None: 

183 response_msg = await psub.get_message( 

184 ignore_subscribe_messages=True, 

185 timeout=cmd.timeout, # type: ignore[arg-type] 

186 ) 

187 

188 if scope.cancel_called: 188 ↛ 189line 188 didn't jump to line 189 because the condition on line 188 was never true

189 raise TimeoutError 

190 

191 return response_msg 

192 

193 finally: 

194 with suppress(Exception): 

195 await psub.unsubscribe() 

196 await psub.aclose() # type: ignore[attr-defined] 

197 

198 

199class RedisClusterFastProducer(BaseRedisFastProducer): 

200 """Producer that routes channel operations through the sync cluster.""" 

201 

202 __slots__ = ("_cluster_state",) 

203 

204 def __init__( 

205 self, 

206 connection: "ConnectionState[RedisCluster[bytes]]", 

207 cluster_state: RedisClusterConnectionState, 

208 **kwargs: Any, 

209 ) -> None: 

210 super().__init__(connection=connection, **kwargs) 

211 self._cluster_state = cluster_state 

212 

213 @property 

214 def cluster_state(self) -> RedisClusterConnectionState: 

215 return self._cluster_state 

216 

217 @override 

218 def _build_child(self, **kwargs: Any) -> "RedisClusterFastProducer": 

219 return RedisClusterFastProducer(cluster_state=self._cluster_state, **kwargs) 

220 

221 @override 

222 async def publish(self, cmd: "RedisPublishCommand") -> int | bytes: 

223 msg = await cmd.message_format.encode( 

224 message=cmd.body, 

225 reply_to=cmd.reply_to, 

226 headers=cmd.headers, 

227 correlation_id=cmd.correlation_id or "", 

228 serializer=self.serializer, 

229 codec=self.codec, 

230 ) 

231 

232 if cmd.destination_type is DestinationType.Channel: 

233 return await self._cluster_state.sync_publish(cmd.destination, msg) 

234 

235 if cmd.destination_type is DestinationType.List: 

236 return cast("int", await self._connection.client.rpush(cmd.destination, msg)) 

237 if cmd.destination_type is DestinationType.Stream: 

238 return cast( 

239 "bytes", 

240 await self._connection.client.xadd( 

241 name=cmd.destination, 

242 fields={DATA_KEY: msg}, 

243 maxlen=cmd.maxlen, 

244 ), 

245 ) 

246 raise UnreachablePathError 

247 

248 @override 

249 async def request(self, cmd: "RedisPublishCommand") -> "Any": 

250 nuid = NUID() 

251 reply_to = str(nuid.next(), "utf-8") 

252 psub = self._cluster_state.pubsub() 

253 

254 try: 

255 await psub.subscribe(reply_to) 

256 

257 msg = await cmd.message_format.encode( 

258 message=cmd.body, 

259 reply_to=reply_to, 

260 headers=cmd.headers, 

261 correlation_id=cmd.correlation_id or "", 

262 serializer=self.serializer, 

263 codec=self.codec, 

264 ) 

265 

266 if cmd.destination_type is DestinationType.Channel: 

267 await self._cluster_state.sync_publish(cmd.destination, msg) 

268 elif cmd.destination_type is DestinationType.List: 

269 await self._connection.client.rpush(cmd.destination, msg) 

270 elif cmd.destination_type is DestinationType.Stream: 

271 await self._connection.client.xadd( 

272 name=cmd.destination, 

273 fields={DATA_KEY: msg}, 

274 maxlen=cmd.maxlen, 

275 ) 

276 else: 

277 raise UnreachablePathError 

278 

279 with anyio.fail_after(cmd.timeout) as scope: 

280 response_msg = None 

281 while response_msg is None: 

282 response_msg = await psub.get_message( 

283 ignore_subscribe_messages=True, 

284 timeout=cmd.timeout, 

285 ) 

286 

287 if scope.cancel_called: 287 ↛ 288line 287 didn't jump to line 288 because the condition on line 287 was never true

288 raise TimeoutError 

289 

290 return response_msg 

291 

292 finally: 

293 with suppress(Exception): 

294 await psub.unsubscribe() 

295 await psub.aclose()