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

126 statements  

« prev     ^ index     » next       coverage.py v7.17.4, created at 2026-11-21 11:35 +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 pool_class = connection_kwargs.pop("connection_pool_class", ConnectionPool) 

89 

90 pool = pool_class(**connection_kwargs) 

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

92 

93 self._client = client 

94 self._connected = True 

95 

96 return client 

97 

98 

99class RedisSentinelConnectionState(RedisConnectionState): 

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

101 

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

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

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

105 """ 

106 

107 __slots__ = ( 

108 "_master_name", 

109 "_sentinel_kwargs", 

110 "_sentinels", 

111 ) 

112 

113 def __init__( 

114 self, 

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

116 *, 

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

118 master_name: str, 

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

120 ) -> None: 

121 super().__init__(options) 

122 self._sentinels = list(sentinels) 

123 self._master_name = master_name 

124 self._sentinel_kwargs = sentinel_kwargs 

125 

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

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

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

129 connection_kwargs = { 

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

131 } 

132 connection_kwargs |= _get_driver_info() 

133 

134 manager = Sentinel( 

135 self._sentinels, 

136 sentinel_kwargs=dict(self._sentinel_kwargs) 

137 if self._sentinel_kwargs is not None 

138 else None, 

139 **connection_kwargs, 

140 ) 

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

142 

143 self._client = client 

144 self._connected = True 

145 

146 return client 

147 

148 

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

150 """Manages a Redis Cluster connection lifecycle. 

151 

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

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

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

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

156 """ 

157 

158 __slots__ = () 

159 

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

161 self._options = options or {} 

162 

163 self._connected = False 

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

165 self._sync_cluster: Any = None 

166 self._thread_pool: ThreadPoolExecutor | None = None 

167 

168 @property 

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

170 if not self._client: 

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

172 raise IncorrectState(msg) 

173 return self._client 

174 

175 def __bool__(self) -> bool: 

176 return self._connected 

177 

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

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

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

181 

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

183 connection_kwargs |= _get_driver_info() 

184 

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

186 self._client = client 

187 self._connected = True 

188 return client 

189 

190 async def disconnect(self) -> None: 

191 if self._thread_pool is not None: 

192 self._thread_pool.shutdown(wait=False) 

193 self._thread_pool = None 

194 self._sync_cluster = None 

195 if self._client: 

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

197 self._client = None 

198 self._connected = False 

199 

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

201 sync = self._get_sync_cluster() 

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

203 

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

205 if self._thread_pool is not None: 

206 pool = self._thread_pool 

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

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

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

210 else: 

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

212 raise IncorrectState(msg) 

213 

214 sync = self._get_sync_cluster() 

215 return _SyncPubSubProxy(sync, pool) 

216 

217 def _get_sync_cluster(self) -> Any: 

218 if self._sync_cluster is not None: 

219 return self._sync_cluster 

220 

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

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

223 return self._client 

224 

225 raw = self._options 

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

227 if not nodes: 

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

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

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

231 

232 self._sync_cluster = SyncRC( 

233 startup_nodes=nodes, 

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

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

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

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

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

239 ) 

240 self._thread_pool = ThreadPoolExecutor(max_workers=4) 

241 return self._sync_cluster 

242 

243 

244class _SyncPubSubProxy: 

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

246 

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

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

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

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

251 """ 

252 

253 __slots__ = ( 

254 "_pool", 

255 "_psub", 

256 ) 

257 

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

259 self._pool = pool 

260 self._psub = sync_cluster.pubsub() 

261 

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

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

264 

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

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

267 

268 async def unsubscribe(self) -> None: 

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

270 

271 async def get_message( 

272 self, 

273 ignore_subscribe_messages: bool = False, 

274 timeout: float | None = 0.0, 

275 ) -> Any: 

276 return await run_in_executor( 

277 self._pool, 

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

279 ) 

280 

281 async def aclose(self) -> None: 

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