Coverage for faststream/redis/configs/state.py: 89%

125 statements  

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

1from abc import ABC, abstractmethod 

2from collections.abc import Mapping, Sequence 

3from concurrent.futures import ThreadPoolExecutor 

4from functools import partial 

5from typing import Any, Generic, TypeVar 

6 

7from redis.asyncio.client import Redis 

8from redis.asyncio.cluster import RedisCluster 

9from redis.asyncio.connection import ConnectionPool 

10from redis.asyncio.sentinel import Sentinel 

11from redis.cluster import ( 

12 ClusterNode, 

13 RedisCluster as SyncRC, 

14) 

15 

16from faststream.__about__ import __version__ 

17from faststream._internal.utils.functions import run_in_executor 

18from faststream.exceptions import IncorrectState 

19from faststream.redis._compat import REDIS_V720 

20 

21if REDIS_V720: 

22 from redis.driver_info import DriverInfo 

23 

24 

25ClientT = TypeVar("ClientT") 

26 

27 

28def _get_driver_info() -> dict[str, Any]: 

29 if REDIS_V720: 29 ↛ 36line 29 didn't jump to line 36 because the condition on line 29 was always true

30 return { 

31 "driver_info": DriverInfo( 

32 name="faststream", 

33 lib_version=__version__, 

34 ) 

35 } 

36 return { 

37 "lib_name": "faststream", 

38 "lib_version": __version__, 

39 } 

40 

41 

42class ConnectionState(ABC, Generic[ClientT]): 

43 """Base connection state.""" 

44 

45 __slots__ = ( 

46 "_client", 

47 "_connected", 

48 "_options", 

49 "_sync_cluster", 

50 "_thread_pool", 

51 ) 

52 

53 def __init__(self, options: dict[str, Any] | None = None) -> None: 

54 self._options = options or {} 

55 

56 self._connected = False 

57 self._client: ClientT | None = None 

58 self._sync_cluster: Any = None 

59 self._thread_pool: Any = None 

60 

61 @property 

62 def client(self) -> ClientT: 

63 if not self._client: 63 ↛ 64line 63 didn't jump to line 64 because the condition on line 63 was never true

64 msg = "Connection is not available yet. Please, connect the broker first." 

65 raise IncorrectState(msg) 

66 

67 return self._client 

68 

69 def __bool__(self) -> bool: 

70 return self._connected 

71 

72 @abstractmethod 

73 async def connect(self) -> ClientT: ... 

74 

75 async def disconnect(self) -> None: 

76 if self._client: 

77 await self._client.aclose() # type: ignore[attr-defined] 

78 

79 self._client = None 

80 self._connected = False 

81 

82 

83class RedisConnectionState(ConnectionState["Redis[bytes]"]): 

84 __slots__ = () 

85 

86 async def connect(self) -> "Redis[bytes]": 

87 connection_kwargs = self._options | _get_driver_info() 

88 

89 pool = ConnectionPool(**connection_kwargs) 

90 client: Redis[bytes] = Redis.from_pool(pool) # type: ignore[attr-defined] 

91 

92 self._client = client 

93 self._connected = True 

94 

95 return client 

96 

97 

98class RedisSentinelConnectionState(RedisConnectionState): 

99 """Builds the client via ``Sentinel.master_for`` for HA / failover. 

100 

101 The underlying ``SentinelConnectionPool`` re-discovers the current master 

102 on every reconnect, so publishers and stream consumers fail over for free 

103 (both go through ``connection.client``). 

104 """ 

105 

106 __slots__ = ( 

107 "_master_name", 

108 "_sentinel_kwargs", 

109 "_sentinels", 

110 ) 

111 

112 def __init__( 

113 self, 

114 options: dict[str, Any] | None = None, 

115 *, 

116 sentinels: Sequence[tuple[str, int]], 

117 master_name: str, 

118 sentinel_kwargs: Mapping[str, Any] | None = None, 

119 ) -> None: 

120 super().__init__(options) 

121 self._sentinels = list(sentinels) 

122 self._master_name = master_name 

123 self._sentinel_kwargs = sentinel_kwargs 

124 

125 async def connect(self) -> "Redis[bytes]": 

126 # ``host``/``port`` describe a single node and are meaningless for 

127 # Sentinel — the master address is discovered from the sentinels. 

128 connection_kwargs = { 

129 k: v for k, v in self._options.items() if k not in {"host", "port"} 

130 } 

131 connection_kwargs |= _get_driver_info() 

132 

133 manager = Sentinel( 

134 self._sentinels, 

135 sentinel_kwargs=dict(self._sentinel_kwargs) 

136 if self._sentinel_kwargs is not None 

137 else None, 

138 **connection_kwargs, 

139 ) 

140 client: Redis[bytes] = manager.master_for(self._master_name) 

141 

142 self._client = client 

143 self._connected = True 

144 

145 return client 

146 

147 

148class RedisClusterConnectionState(ConnectionState["RedisCluster[bytes]"]): 

149 """Manages a Redis Cluster connection lifecycle. 

150 

151 Uses an **async** ``RedisCluster`` for List/Stream/KV commands and a 

152 **sync** ``redis.cluster.RedisCluster`` (wrapped via 

153 ``run_in_executor``) for Pub/Sub — the async client doesn't expose 

154 ``publish`` / ``pubsub`` until ``redis-py >= 8.0.0``. 

155 """ 

156 

157 __slots__ = () 

158 

159 def __init__(self, options: dict[str, Any] | None = None) -> None: 

160 self._options = options or {} 

161 

162 self._connected = False 

163 self._client: RedisCluster[bytes] | None = None 

164 self._sync_cluster: Any = None 

165 self._thread_pool: ThreadPoolExecutor | None = None 

166 

167 @property 

168 def client(self) -> "RedisCluster[bytes]": 

169 if not self._client: 

170 msg = "Connection is not available yet. Please, connect the broker first." 

171 raise IncorrectState(msg) 

172 return self._client 

173 

174 def __bool__(self) -> bool: 

175 return self._connected 

176 

177 async def connect(self) -> "RedisCluster[bytes]": 

178 if self._connected: 178 ↛ 179line 178 didn't jump to line 179 because the condition on line 178 was never true

179 return self._client # type: ignore[return-value] 

180 

181 connection_kwargs = {k: v for k, v in self._options.items() if v is not None} 

182 connection_kwargs |= _get_driver_info() 

183 

184 client: RedisCluster[bytes] = RedisCluster(**connection_kwargs) 

185 self._client = client 

186 self._connected = True 

187 return client 

188 

189 async def disconnect(self) -> None: 

190 if self._thread_pool is not None: 

191 self._thread_pool.shutdown(wait=False) 

192 self._thread_pool = None 

193 self._sync_cluster = None 

194 if self._client: 

195 await self._client.aclose() # type: ignore[attr-defined] 

196 self._client = None 

197 self._connected = False 

198 

199 async def sync_publish(self, channel: str, body: bytes) -> int: 

200 sync = self._get_sync_cluster() 

201 return await run_in_executor(self._thread_pool, sync.publish, channel, body) 

202 

203 def pubsub(self) -> "_SyncPubSubProxy": 

204 if self._thread_pool is not None: 

205 pool = self._thread_pool 

206 elif self._client is not None: 206 ↛ 210line 206 didn't jump to line 210 because the condition on line 206 was always true

207 # Test mode: store the pool so disconnect() cleans it up 

208 pool = self._thread_pool = ThreadPoolExecutor(max_workers=1) 

209 else: 

210 msg = "Pub/Sub proxy is not available" 

211 raise IncorrectState(msg) 

212 

213 sync = self._get_sync_cluster() 

214 return _SyncPubSubProxy(sync, pool) 

215 

216 def _get_sync_cluster(self) -> Any: 

217 if self._sync_cluster is not None: 

218 return self._sync_cluster 

219 

220 # Test mode: _client set by _fake_connect, _connected stays False 

221 if not self._connected and self._client is not None: 221 ↛ 222line 221 didn't jump to line 222 because the condition on line 221 was never true

222 return self._client 

223 

224 raw = self._options 

225 nodes = [ClusterNode(n.host, n.port) for n in raw.get("startup_nodes", [])] 

226 if not nodes: 

227 host = raw.get("host", "127.0.0.1") 

228 port = int(raw.get("port", 6379)) 

229 nodes.append(ClusterNode(host, port)) 

230 

231 self._sync_cluster = SyncRC( 

232 startup_nodes=nodes, 

233 password=raw.get("password"), 

234 username=raw.get("username"), 

235 ssl=raw.get("ssl", False), 

236 socket_timeout=raw.get("socket_timeout"), 

237 socket_connect_timeout=raw.get("socket_connect_timeout"), 

238 ) 

239 self._thread_pool = ThreadPoolExecutor(max_workers=4) 

240 return self._sync_cluster 

241 

242 

243class _SyncPubSubProxy: 

244 """Wraps a **sync** ``redis.cluster.RedisCluster.pubsub()`` for async use. 

245 

246 ``redis-py``'s async ``RedisCluster`` lacks ``publish`` / ``pubsub`` 

247 until version 8.0.0. We use the sync client via a 

248 ``ThreadPoolExecutor`` (following the same pattern as the Confluent 

249 adapter) so Pub/Sub works with ``redis-py >= 7.4.0``. 

250 """ 

251 

252 __slots__ = ( 

253 "_pool", 

254 "_psub", 

255 ) 

256 

257 def __init__(self, sync_cluster: Any, pool: ThreadPoolExecutor) -> None: 

258 self._pool = pool 

259 self._psub = sync_cluster.pubsub() 

260 

261 async def subscribe(self, channel: str) -> None: 

262 await run_in_executor(self._pool, self._psub.subscribe, channel) 

263 

264 async def psubscribe(self, pattern: str) -> None: 

265 await run_in_executor(self._pool, self._psub.psubscribe, pattern) 

266 

267 async def unsubscribe(self) -> None: 

268 await run_in_executor(self._pool, self._psub.unsubscribe) 

269 

270 async def get_message( 

271 self, 

272 ignore_subscribe_messages: bool = False, 

273 timeout: float | None = 0.0, 

274 ) -> Any: 

275 return await run_in_executor( 

276 self._pool, 

277 partial(self._psub.get_message, ignore_subscribe_messages, timeout), 

278 ) 

279 

280 async def aclose(self) -> None: 

281 await run_in_executor(self._pool, self._psub.close)