Coverage for faststream/redis/subscriber/usecases/list_subscriber.py: 95%

95 statements  

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

1from collections.abc import AsyncIterator 

2from typing import TYPE_CHECKING, Any, Optional, TypeAlias 

3 

4import anyio 

5from typing_extensions import override 

6 

7from faststream._internal.endpoint.subscriber.mixins import ConcurrentMixin 

8from faststream._internal.endpoint.utils import process_msg 

9from faststream.redis.message import ( 

10 BatchListMessage, 

11 DefaultListMessage, 

12 RedisListMessage, 

13) 

14from faststream.redis.parser import ( 

15 RedisBatchListParser, 

16 RedisListParser, 

17) 

18 

19from .basic import LogicSubscriber 

20 

21if TYPE_CHECKING: 

22 from redis.asyncio.client import Redis 

23 

24 from faststream._internal.endpoint.subscriber import SubscriberSpecification 

25 from faststream._internal.endpoint.subscriber.call_item import ( 

26 CallsCollection, 

27 ) 

28 from faststream.message import StreamMessage as BrokerStreamMessage 

29 from faststream.redis.schemas import ListSub 

30 from faststream.redis.subscriber.config import RedisSubscriberConfig 

31 from faststream.redis.subscriber.specification import RedisSubscriberSpecification 

32 

33TopicName: TypeAlias = bytes 

34Offset: TypeAlias = bytes 

35 

36 

37class _ListHandlerMixin(LogicSubscriber): 

38 __slots__ = ( 

39 "_list_sub", 

40 "_read_lock", 

41 ) 

42 

43 def __init__( 

44 self, 

45 config: "RedisSubscriberConfig", 

46 specification: "SubscriberSpecification[Any, Any]", 

47 calls: "CallsCollection[Any]", 

48 ) -> None: 

49 super().__init__(config, specification, calls) 

50 self._read_lock = anyio.Lock() 

51 assert config.list_sub 

52 self._list_sub = config.list_sub 

53 

54 @property 

55 def list_sub(self) -> "ListSub": 

56 return self._list_sub.add_prefix(self._outer_config.prefix) 

57 

58 def get_log_context( 

59 self, 

60 message: Optional["BrokerStreamMessage[Any]"], 

61 ) -> dict[str, str]: 

62 return self.build_log_context( 

63 message=message, 

64 channel=self.list_sub.name, 

65 ) 

66 

67 @override 

68 async def _consume( # type: ignore[override] 

69 self, 

70 client: "Redis[bytes]", 

71 *, 

72 start_signal: "anyio.Event", 

73 ) -> None: 

74 if await client.ping(): 74 ↛ 76line 74 didn't jump to line 76 because the condition on line 74 was always true

75 start_signal.set() 

76 await super()._consume(client, start_signal=start_signal) 

77 

78 @override 

79 async def start(self) -> None: 

80 await super().start(self._client) 

81 

82 @override 

83 async def stop(self) -> None: 

84 with anyio.move_on_after(self._outer_config.graceful_timeout): 

85 async with self._read_lock: 

86 await super().stop() 

87 

88 @override 

89 async def get_one( 

90 self, 

91 *, 

92 timeout: float = 5.0, 

93 ) -> "RedisListMessage | None": 

94 assert not self.calls, ( 

95 "You can't use `get_one` method if subscriber has registered handlers." 

96 ) 

97 

98 sleep_interval = timeout / 10 

99 raw_message = None 

100 

101 with anyio.move_on_after(timeout): 

102 while ( # ruff: ignore[async-busy-wait] 

103 raw_message := await self._client.lpop(name=self.list_sub.name) 

104 ) is None: 

105 await anyio.sleep(sleep_interval) 

106 

107 if raw_message is None: 

108 return None 

109 

110 redis_incoming_msg = DefaultListMessage( 

111 type="list", 

112 data=raw_message, 

113 channel=self.list_sub.name, 

114 ) 

115 

116 context = self._outer_config.context 

117 async_parser, async_decoder = self._get_parser_and_decoder() 

118 

119 msg: RedisListMessage = await process_msg( # type: ignore[assignment] 

120 msg=redis_incoming_msg, 

121 middlewares=( 

122 m(redis_incoming_msg, context=context) for m in self._broker_middlewares 

123 ), 

124 parser=async_parser, 

125 decoder=async_decoder, 

126 ) 

127 return msg 

128 

129 @override 

130 async def __aiter__(self) -> AsyncIterator["RedisListMessage"]: 

131 assert not self.calls, ( 

132 "You can't use iterator if subscriber has registered handlers." 

133 ) 

134 

135 timeout = 5 

136 sleep_interval = timeout / 10 

137 raw_message = None 

138 

139 context = self._outer_config.context 

140 async_parser, async_decoder = self._get_parser_and_decoder() 

141 

142 while True: 

143 with anyio.move_on_after(timeout): 

144 while ( # ruff: ignore[async-busy-wait] 144 ↛ 147line 144 didn't jump to line 147 because the condition on line 144 was never true

145 raw_message := await self._client.lpop(name=self.list_sub.name) 

146 ) is None: 

147 await anyio.sleep(sleep_interval) 

148 

149 if raw_message is None: 149 ↛ 150line 149 didn't jump to line 150 because the condition on line 149 was never true

150 continue 

151 

152 redis_incoming_msg = DefaultListMessage( 

153 type="list", 

154 data=raw_message, 

155 channel=self.list_sub.name, 

156 ) 

157 

158 msg: RedisListMessage = await process_msg( # type: ignore[assignment] 

159 msg=redis_incoming_msg, 

160 middlewares=( 

161 m(redis_incoming_msg, context=context) 

162 for m in self._broker_middlewares 

163 ), 

164 parser=async_parser, 

165 decoder=async_decoder, 

166 ) 

167 yield msg 

168 

169 

170class ListSubscriber(_ListHandlerMixin): 

171 __slots__ = () 

172 

173 def __init__( 

174 self, 

175 config: "RedisSubscriberConfig", 

176 specification: "RedisSubscriberSpecification", 

177 calls: "CallsCollection[Any]", 

178 ) -> None: 

179 parser = RedisListParser(config) 

180 config.parser = parser.parse_message 

181 config.decoder = parser.decode_message 

182 super().__init__(config, specification, calls) 

183 

184 async def _get_msgs(self, client: "Redis[bytes]") -> None: 

185 async with self._read_lock: 

186 raw_msg = await client.blpop( 

187 self.list_sub.name, 

188 timeout=self.list_sub.polling_interval, 

189 ) 

190 

191 if raw_msg: 

192 _, msg_data = raw_msg 

193 

194 msg = DefaultListMessage( 

195 type="list", 

196 data=msg_data, 

197 channel=self.list_sub.name, 

198 ) 

199 

200 await self.consume_one(msg) 

201 

202 

203class ListBatchSubscriber(_ListHandlerMixin): 

204 __slots__ = () 

205 

206 def __init__( 

207 self, 

208 config: "RedisSubscriberConfig", 

209 specification: "RedisSubscriberSpecification", 

210 calls: "CallsCollection[Any]", 

211 ) -> None: 

212 parser = RedisBatchListParser(config) 

213 config.parser = parser.parse_message 

214 config.decoder = parser.decode_message 

215 super().__init__(config, specification, calls) 

216 

217 async def _get_msgs(self, client: "Redis[bytes]") -> None: 

218 async with self._read_lock: 

219 raw_msgs = await client.lpop( 

220 name=self.list_sub.name, 

221 count=self.list_sub.max_records, 

222 ) 

223 

224 if raw_msgs: 

225 msg = BatchListMessage( 

226 type="blist", 

227 channel=self.list_sub.name, 

228 data=raw_msgs, 

229 ) 

230 

231 await self.consume_one(msg) 

232 

233 if not raw_msgs: 

234 await anyio.sleep(self.list_sub.polling_interval) 

235 

236 

237class ListConcurrentSubscriber( 

238 ConcurrentMixin["BrokerStreamMessage[Any]"], 

239 ListSubscriber, 

240): 

241 __slots__ = () 

242 

243 async def start(self) -> None: 

244 await super().start() 

245 self.start_consume_task() 

246 

247 async def consume_one(self, msg: "BrokerStreamMessage[Any]") -> None: 

248 await self._put_msg(msg)