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

73 statements  

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

1import warnings 

2from collections.abc import Iterable 

3from typing import TYPE_CHECKING, Any, Optional, Union, cast 

4 

5from redis.asyncio.cluster import ClusterNode 

6from redis.asyncio.connection import SSLConnection 

7from typing_extensions import Unpack, override 

8 

9from faststream._internal.constants import EMPTY 

10from faststream.redis.broker import RedisBroker 

11from faststream.redis.configs.state import ( 

12 ConnectionState, 

13 RedisClusterConnectionState, 

14) 

15from faststream.redis.publisher.producer import ( 

16 RedisClusterFastProducer, 

17) 

18from faststream.redis.schemas.types import ( 

19 CLUSTER_INCOMPATIBLE_PARAMS, 

20) 

21from faststream.redis.subscriber.usecases.basic import LogicSubscriber 

22 

23if TYPE_CHECKING: 

24 from types import TracebackType 

25 

26 from redis.asyncio.client import Pipeline 

27 

28 from faststream._internal.basic_types import SendableMessage 

29 from faststream.redis.schemas import ListSub, PubSub, StreamSub 

30 from faststream.redis.schemas.types import RedisClusterParams 

31 from faststream.redis.subscriber.usecases import ChannelSubscriber 

32 from faststream.security import BaseSecurity 

33 

34 

35class RedisClusterBroker(RedisBroker): 

36 """A Redis Cluster broker.""" 

37 

38 def __init__( 

39 self, 

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

41 **kwargs: Unpack["RedisClusterParams"], 

42 ) -> None: 

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

44 

45 @override 

46 def _make_connection_state( 

47 self, 

48 connection_options: dict[str, Any], 

49 kwargs: dict[str, Any], 

50 ) -> "ConnectionState[Any]": 

51 return RedisClusterConnectionState(connection_options) 

52 

53 def _make_producer( 

54 self, 

55 connection_state: "ConnectionState[Any]", 

56 kwargs: dict[str, Any], 

57 ) -> "RedisClusterFastProducer": 

58 state = cast("RedisClusterConnectionState", connection_state) 

59 return RedisClusterFastProducer( 

60 connection=state, 

61 cluster_state=state, 

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

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

64 message_format=self.message_format, 

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

66 ) 

67 

68 @property 

69 def _cluster_state(self) -> RedisClusterConnectionState: 

70 return cast("RedisClusterConnectionState", self.config.broker_config.connection) 

71 

72 def subscriber( # type: ignore[override] 

73 self, 

74 channel: Union["PubSub", str, None] = None, 

75 *, 

76 list: Union["ListSub", str, None] = None, 

77 stream: Union["StreamSub", str, None] = None, 

78 **kwargs: Any, 

79 ) -> "LogicSubscriber": 

80 if channel is not None: 

81 return self._make_channel_subscriber(channel, **kwargs) 

82 return super().subscriber( 

83 channel=None, 

84 list=list, 

85 stream=stream, 

86 **kwargs, 

87 ) 

88 

89 def _make_channel_subscriber( 

90 self, 

91 channel: Union["PubSub", str], 

92 **kwargs: Any, 

93 ) -> "ChannelSubscriber": 

94 state = self._cluster_state 

95 

96 sub = cast( 

97 "ChannelSubscriber", 

98 super().subscriber(channel=channel, list=None, stream=None, **kwargs), 

99 ) 

100 

101 async def _patched_start() -> None: 

102 if sub.subscription: 

103 return 

104 psub = state.pubsub() 

105 sub.subscription = psub # type: ignore[assignment] 

106 if sub.channel.pattern: 

107 await psub.psubscribe(sub.channel.name) 

108 else: 

109 await psub.subscribe(sub.channel.name) 

110 await LogicSubscriber.start(sub, psub) 

111 

112 sub.start = _patched_start # type: ignore[method-assign] 

113 return sub 

114 

115 async def publish( # type: ignore[override] 

116 self, 

117 message: "SendableMessage" = None, 

118 channel: str | None = None, 

119 *, 

120 reply_to: str = "", 

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

122 correlation_id: str | None = None, 

123 list: str | None = None, 

124 stream: str | None = None, 

125 maxlen: int | None = None, 

126 pipeline: Optional["Pipeline[bytes]"] = EMPTY, 

127 ) -> int | bytes: 

128 if pipeline is not EMPTY: 

129 warnings.warn( 

130 "Pipeline is not supported in Redis Cluster and will be ignored.", 

131 category=RuntimeWarning, 

132 stacklevel=2, 

133 ) 

134 

135 publish_kwargs: dict[str, Any] = {} 

136 if stream is not None: 

137 publish_kwargs["stream"] = stream 

138 if maxlen is not None: 138 ↛ 139line 138 didn't jump to line 139 because the condition on line 138 was never true

139 publish_kwargs["maxlen"] = maxlen 

140 

141 return cast( 

142 "int | bytes", 

143 await super().publish( 

144 message, 

145 channel, 

146 reply_to=reply_to, 

147 headers=headers, 

148 correlation_id=correlation_id, 

149 list=list, 

150 **publish_kwargs, 

151 ), 

152 ) 

153 

154 async def _connect(self) -> Any: 

155 await self.config.connect() 

156 return self.config.broker_config.connection.client 

157 

158 async def stop( 

159 self, 

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

161 exc_val: BaseException | None = None, 

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

163 ) -> None: 

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

165 await self.config.disconnect() 

166 self._connection = None 

167 

168 async def start(self) -> None: 

169 _ = await self.connect() 

170 await super().start() 

171 

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

173 self, 

174 *messages: "SendableMessage", 

175 list: str, 

176 correlation_id: str | None = None, 

177 reply_to: str = "", 

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

179 pipeline: Optional["Pipeline[bytes]"] = EMPTY, 

180 ) -> int: 

181 if pipeline is not EMPTY: 

182 warnings.warn( 

183 "Pipeline is not supported in Redis Cluster and will be ignored.", 

184 category=RuntimeWarning, 

185 stacklevel=2, 

186 ) 

187 

188 if not self._cluster_state: 188 ↛ 189line 188 didn't jump to line 189 because the condition on line 188 was never true

189 await self._connect() 

190 

191 return await super().publish_batch( 

192 *messages, 

193 list=list, 

194 correlation_id=correlation_id, 

195 reply_to=reply_to, 

196 headers=headers, 

197 ) 

198 

199 @staticmethod 

200 def _resolve_url_options( 

201 url: str, 

202 *, 

203 startup_nodes: Iterable[tuple[str, int]] = (), 

204 host: str = EMPTY, 

205 port: str | int = EMPTY, 

206 security: Optional["BaseSecurity"] = None, 

207 **kwargs: Any, 

208 ) -> dict[str, Any]: 

209 options = RedisBroker._resolve_url_options( 

210 url, 

211 security=security, 

212 host=host, 

213 port=port, 

214 **kwargs, 

215 ) 

216 

217 nodes: list[ClusterNode] = [] 

218 cluster_host = str(host) if host is not EMPTY else options.get("host") 

219 cluster_port = int(port) if port is not EMPTY else int(options.get("port", 6379)) 

220 if cluster_host: 220 ↛ 222line 220 didn't jump to line 222 because the condition on line 220 was always true

221 nodes.append(ClusterNode(cluster_host, cluster_port)) 

222 for h, p in startup_nodes: 

223 nodes.append(ClusterNode(h, int(p))) 

224 

225 # TLS is conveyed via `connection_class` (from `parse_security()` or a 

226 # `rediss://` URL), but RedisCluster doesn't accept it — translate to 

227 # its native `ssl` flag before the filter drops it. 

228 connection_class = options.get("connection_class") 

229 use_ssl = (security is not None and security.use_ssl) or ( 

230 isinstance(connection_class, type) 

231 and issubclass(connection_class, SSLConnection) 

232 ) 

233 

234 result = { 

235 k: v for k, v in options.items() if k not in CLUSTER_INCOMPATIBLE_PARAMS 

236 } | {"startup_nodes": nodes} 

237 

238 if use_ssl: 

239 result.setdefault("ssl", True) 

240 

241 if security is not None and security.ssl_context is not None: 

242 warnings.warn( 

243 "RedisCluster does not support a custom `ssl_context`, so it" 

244 " will be ignored. Use `ssl_ca_certs`, `ssl_certfile`," 

245 " `ssl_keyfile` and other `ssl_*` connection options instead.", 

246 category=RuntimeWarning, 

247 stacklevel=3, 

248 ) 

249 

250 return result