Coverage for faststream/redis/broker/broker.py: 95%

76 statements  

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

1import logging 

2from typing import ( 

3 TYPE_CHECKING, 

4 Any, 

5 ClassVar, 

6 Optional, 

7 cast, 

8) 

9from urllib.parse import urlparse 

10 

11import anyio 

12from anyio import move_on_after 

13from fast_depends import dependency_provider 

14from redis.asyncio.connection import ( 

15 parse_url, 

16) 

17from redis.exceptions import ConnectionError 

18from typing_extensions import Unpack, overload, override 

19 

20from faststream._internal.broker import BrokerUsecase 

21from faststream._internal.constants import EMPTY 

22from faststream._internal.context.repository import ContextRepo 

23from faststream._internal.di import FastDependsConfig 

24from faststream.message import gen_cor_id 

25from faststream.redis.configs import ( 

26 ConnectionState, 

27 RedisBrokerConfig, 

28 RedisConnectionState, 

29) 

30from faststream.redis.message import UnifyRedisDict 

31from faststream.redis.parser import BinaryMessageFormatV1, MessageFormat 

32from faststream.redis.publisher.producer import RedisFastProducer 

33from faststream.redis.response import RedisPublishCommand 

34from faststream.redis.schemas.types import NON_CONNECTION_PARAMS 

35from faststream.redis.security import parse_security 

36from faststream.response.publish_type import PublishType 

37from faststream.specification.schema import BrokerSpec 

38 

39from .logging import make_redis_logger_state 

40from .registrator import RedisRegistrator 

41 

42if TYPE_CHECKING: 

43 from types import TracebackType 

44 

45 from redis.asyncio.client import Pipeline, Redis 

46 

47 from faststream._internal.basic_types import SendableMessage 

48 from faststream.redis.message import RedisChannelMessage 

49 from faststream.redis.publisher.producer import RedisClusterFastProducer 

50 from faststream.redis.schemas.types import RedisBrokerParams 

51 from faststream.security import BaseSecurity 

52 

53 

54class RedisBroker( 

55 RedisRegistrator, 

56 BrokerUsecase[UnifyRedisDict, "Redis[bytes]", RedisBrokerConfig], 

57): 

58 """Redis broker.""" 

59 

60 # Extra params (beyond NON_CONNECTION_PARAMS) that must not leak into the 

61 # connection options. Subclasses extend it (e.g. Sentinel adds SENTINEL_PARAMS). 

62 _EXTRA_NON_CONNECTION_PARAMS: ClassVar[frozenset[str]] = frozenset() 

63 

64 def __init__( 

65 self, 

66 url: str = "redis://localhost:6379", 

67 **kwargs: Unpack["RedisBrokerParams"], 

68 ) -> None: 

69 """Initialized the RedisBroker.""" 

70 self._init_broker(url, dict(kwargs)) 

71 

72 def _init_broker(self, url: str, kwargs: dict[str, Any]) -> None: 

73 """Shared setup for RedisBroker and its Cluster/Sentinel subclasses. 

74 

75 The parts that differ between brokers — connection state, producer and 

76 constructor validation — are delegated to the ``_make_connection_state``, 

77 ``_make_producer`` and ``_validate_init_params`` hooks, so the subclasses 

78 only override those hooks instead of copying this method. 

79 """ 

80 self._validate_init_params(kwargs) 

81 

82 host = kwargs.pop("host", EMPTY) 

83 port = kwargs.pop("port", EMPTY) 

84 security = kwargs.pop("security", None) 

85 specification_url = kwargs.pop("specification_url", None) 

86 retry_on_error = kwargs.pop("retry_on_error", None) 

87 protocol = kwargs.pop("protocol", None) 

88 message_format: type[MessageFormat] = kwargs.pop( 

89 "message_format", BinaryMessageFormatV1 

90 ) 

91 self.message_format = message_format 

92 

93 if specification_url is None: 

94 netloc = urlparse(url).netloc 

95 specification_url = url.replace(netloc, netloc.rpartition("@")[-1], 1) 

96 if protocol is None: 

97 protocol = urlparse(specification_url).scheme 

98 if retry_on_error is None: 98 ↛ 101line 98 didn't jump to line 101 because the condition on line 98 was always true

99 retry_on_error = [] 

100 

101 excluded_params = NON_CONNECTION_PARAMS | self._EXTRA_NON_CONNECTION_PARAMS 

102 connection_kwargs = {k: v for k, v in kwargs.items() if k not in excluded_params} 

103 connection_options = self._resolve_url_options( 

104 url, 

105 security=security, 

106 host=host, 

107 port=port, 

108 retry_on_error=retry_on_error, 

109 **connection_kwargs, 

110 ) 

111 

112 connection_state = self._make_connection_state(connection_options, kwargs) 

113 producer = self._make_producer(connection_state, kwargs) 

114 

115 super().__init__( 

116 **connection_options, 

117 routers=kwargs.get("routers", ()), 

118 config=RedisBrokerConfig( 

119 connection=connection_state, 

120 producer=producer, 

121 message_format=self.message_format, 

122 broker_middlewares=kwargs.get("middlewares", ()), 

123 broker_parser=kwargs.get("parser"), 

124 broker_decoder=kwargs.get("decoder"), 

125 broker_codec=kwargs.get("codec"), 

126 logger=make_redis_logger_state( 

127 logger=kwargs.get("logger", EMPTY), 

128 log_level=kwargs.get("log_level", logging.INFO), 

129 ), 

130 fd_config=FastDependsConfig( 

131 use_fastdepends=kwargs.get("apply_types", True), 

132 serializer=kwargs.get("serializer", EMPTY), 

133 provider=kwargs.get("provider") or dependency_provider, 

134 context=kwargs.get("context") or ContextRepo(), 

135 ), 

136 broker_dependencies=kwargs.get("dependencies", ()), 

137 graceful_timeout=kwargs.get("graceful_timeout", 15.0), 

138 ack_policy=kwargs.get("ack_policy", EMPTY), 

139 id_generator=kwargs.get("id_generator", gen_cor_id), 

140 extra_context={"broker": self}, 

141 underlying_driver_annotations=kwargs.get("underlying_driver_annotations") 

142 or {}, 

143 ), 

144 specification=BrokerSpec( 

145 description=kwargs.get("description"), 

146 url=[specification_url], 

147 protocol=protocol, 

148 protocol_version=kwargs.get("protocol_version", "custom"), 

149 security=security, 

150 tags=kwargs.get("tags", ()), 

151 ), 

152 ) 

153 

154 def _validate_init_params(self, kwargs: dict[str, Any]) -> None: 

155 """Validate constructor params. Hook for subclasses; no-op by default.""" 

156 

157 def _make_connection_state( # ruff: ignore[no-self-use] 

158 self, 

159 connection_options: dict[str, Any], 

160 kwargs: dict[str, Any], 

161 ) -> "ConnectionState[Any]": 

162 """Build the connection state. Overridden by Cluster/Sentinel brokers.""" 

163 return RedisConnectionState(connection_options) 

164 

165 def _make_producer( 

166 self, 

167 connection_state: "ConnectionState[Any]", 

168 kwargs: dict[str, Any], 

169 ) -> "RedisFastProducer | RedisClusterFastProducer": 

170 """Build the producer. Overridden by the Cluster broker.""" 

171 return RedisFastProducer( 

172 connection=connection_state, 

173 parser=kwargs.get("parser"), 

174 decoder=kwargs.get("decoder"), 

175 message_format=self.message_format, 

176 serializer=kwargs.get("serializer"), 

177 ) 

178 

179 @override 

180 async def _connect(self) -> "Redis[bytes]": 

181 await self.config.connect() 

182 return cast("Redis[bytes]", self.config.broker_config.connection.client) 

183 

184 async def stop( 

185 self, 

186 exc_type: type[BaseException] | None = None, 

187 exc_val: BaseException | None = None, 

188 exc_tb: Optional["TracebackType"] = None, 

189 ) -> None: 

190 await super().stop(exc_type, exc_val, exc_tb) 

191 await self.config.disconnect() 

192 self._connection = None 

193 

194 async def start(self) -> None: 

195 _ = await self.connect() 

196 await super().start() 

197 

198 @overload 

199 async def publish( 

200 self, 

201 message: "SendableMessage" = None, 

202 channel: str | None = None, 

203 *, 

204 reply_to: str = "", 

205 headers: dict[str, Any] | None = None, 

206 correlation_id: str | None = None, 

207 list: str | None = None, 

208 stream: None = None, 

209 maxlen: int | None = None, 

210 pipeline: Optional["Pipeline[bytes]"] = None, 

211 ) -> int: ... 

212 

213 @overload 

214 async def publish( 

215 self, 

216 message: "SendableMessage" = None, 

217 channel: str | None = None, 

218 *, 

219 reply_to: str = "", 

220 headers: dict[str, Any] | None = None, 

221 correlation_id: str | None = None, 

222 list: str | None = None, 

223 stream: str = ..., 

224 maxlen: int | None = None, 

225 pipeline: Optional["Pipeline[bytes]"] = None, 

226 ) -> bytes: ... 

227 

228 @override 

229 async def publish( 

230 self, 

231 message: "SendableMessage" = None, 

232 channel: str | None = None, 

233 *, 

234 reply_to: str = "", 

235 headers: dict[str, Any] | None = None, 

236 correlation_id: str | None = None, 

237 list: str | None = None, 

238 stream: str | None = None, 

239 maxlen: int | None = None, 

240 pipeline: Optional["Pipeline[bytes]"] = None, 

241 ) -> int | bytes: 

242 """Publish message directly. 

243 

244 This method allows you to publish a message in a non-AsyncAPI-documented way. 

245 It can be used in other frameworks or to publish messages at specific intervals. 

246 

247 Args: 

248 message: 

249 Message body to send. 

250 channel: 

251 Redis PubSub object name to send message. 

252 reply_to: 

253 Reply message destination PubSub object name. 

254 headers: 

255 Message headers to store metainformation. 

256 correlation_id: 

257 Manual message correlation_id setter. correlation_id is a useful option to trace messages. 

258 list: 

259 Redis List object name to send message. 

260 stream: 

261 Redis Stream object name to send message. 

262 maxlen: 

263 Redis Stream maxlen publish option. Remove eldest message if maxlen exceeded. 

264 pipeline: 

265 Redis pipeline to use for publishing messages. 

266 

267 Returns: 

268 int: The result of the publish operation, typically the number of messages published. 

269 """ 

270 cmd = RedisPublishCommand( 

271 message, 

272 correlation_id=correlation_id or self.config.id_generator(), 

273 channel=channel, 

274 list=list, 

275 stream=stream, 

276 maxlen=maxlen, 

277 reply_to=reply_to, 

278 headers=headers, 

279 pipeline=pipeline, 

280 _publish_type=PublishType.PUBLISH, 

281 message_format=self.message_format, 

282 ) 

283 

284 result: int | bytes = await super()._basic_publish( 

285 cmd, 

286 producer=self.config.producer, 

287 ) 

288 return result 

289 

290 @override 

291 async def request( # type: ignore[override] 

292 self, 

293 message: "SendableMessage", 

294 channel: str | None = None, 

295 *, 

296 list: str | None = None, 

297 stream: str | None = None, 

298 maxlen: int | None = None, 

299 correlation_id: str | None = None, 

300 headers: dict[str, Any] | None = None, 

301 timeout: float | None = 30.0, 

302 ) -> "RedisChannelMessage": 

303 cmd = RedisPublishCommand( 

304 message, 

305 correlation_id=correlation_id or self.config.id_generator(), 

306 channel=channel, 

307 list=list, 

308 stream=stream, 

309 maxlen=maxlen, 

310 headers=headers, 

311 timeout=timeout, 

312 _publish_type=PublishType.REQUEST, 

313 message_format=self.message_format, 

314 ) 

315 msg: RedisChannelMessage = await super()._basic_request( 

316 cmd, 

317 producer=self.config.producer, 

318 ) 

319 return msg 

320 

321 @override 

322 async def publish_batch( # type: ignore[override] 

323 self, 

324 *messages: "SendableMessage", 

325 list: str, 

326 correlation_id: str | None = None, 

327 reply_to: str = "", 

328 headers: dict[str, Any] | None = None, 

329 pipeline: Optional["Pipeline[bytes]"] = None, 

330 ) -> int: 

331 """Publish multiple messages to Redis List by one request. 

332 

333 Args: 

334 *messages: Messages bodies to send. 

335 list: Redis List object name to send messages. 

336 correlation_id: Manual message **correlation_id** setter. **correlation_id** is a useful option to trace messages. 

337 reply_to: Reply message destination PubSub object name. 

338 headers: Message headers to store metainformation. 

339 pipeline: Redis pipeline to use for publishing messages. 

340 

341 Returns: 

342 int: The result of the batch publish operation. 

343 """ 

344 cmd = RedisPublishCommand( 

345 *messages, 

346 list=list, 

347 reply_to=reply_to, 

348 headers=headers, 

349 correlation_id=correlation_id or self.config.id_generator(), 

350 pipeline=pipeline, 

351 _publish_type=PublishType.PUBLISH, 

352 message_format=self.message_format, 

353 ) 

354 

355 result: int = await self._basic_publish_batch( 

356 cmd, 

357 producer=self.config.producer, 

358 ) 

359 return result 

360 

361 @override 

362 async def ping(self, timeout: float | None = 3) -> bool: 

363 sleep_time = (timeout or 10) / 10 

364 

365 with move_on_after(timeout) as cancel_scope: 

366 if self._connection is None: 

367 return False 

368 

369 while True: 

370 if cancel_scope.cancel_called: 370 ↛ 371line 370 didn't jump to line 371 because the condition on line 370 was never true

371 return False 

372 

373 try: 

374 if await self._connection.ping(): 

375 return True 

376 

377 except ConnectionError: 

378 pass 

379 

380 await anyio.sleep(sleep_time) 

381 

382 return False 

383 

384 @staticmethod 

385 def _resolve_url_options( 

386 url: str, 

387 *, 

388 security: Optional["BaseSecurity"], 

389 **kwargs: Any, 

390 ) -> dict[str, Any]: 

391 return { 

392 **dict(parse_url(url)), 

393 **parse_security(security), 

394 **{k: v for k, v in kwargs.items() if v is not EMPTY}, 

395 }