Coverage for faststream/mqtt/subscriber/usecase.py: 92%
100 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1import warnings
2from abc import abstractmethod
3from collections.abc import AsyncIterator, Sequence
4from contextlib import suppress
5from typing import TYPE_CHECKING, Any, Literal
7import anyio
8import zmqtt
9from typing_extensions import override
11from faststream._internal.endpoint.subscriber import SubscriberUsecase
12from faststream._internal.endpoint.subscriber.mixins import ConcurrentMixin, TasksMixin
13from faststream._internal.endpoint.utils import process_msg
14from faststream.middlewares import AckPolicy
15from faststream.mqtt.parser import MQTTBaseParser, MQTTVersion, parser_for
16from faststream.mqtt.publisher.fake import MQTTFakePublisher
18if TYPE_CHECKING:
19 from faststream._internal.endpoint.publisher import PublisherProto
20 from faststream._internal.endpoint.subscriber import SubscriberSpecification
21 from faststream._internal.endpoint.subscriber.call_item import CallsCollection
22 from faststream._internal.utils.path import Address
23 from faststream.message import StreamMessage
24 from faststream.mqtt.broker.config import MQTTBrokerConfig
25 from faststream.mqtt.message import MQTTMessage
26 from faststream.mqtt.subscriber.config import MQTTSubscriberConfig
29class MQTTBaseSubscriber(TasksMixin, SubscriberUsecase[zmqtt.Message]):
30 """Base class for all MQTT subscribers."""
32 __slots__ = (
33 "_address",
34 "_qos",
35 "_shared",
36 "_subscription",
37 "last_unsubscribe_result",
38 )
40 _outer_config: "MQTTBrokerConfig"
42 def __init__(
43 self,
44 config: "MQTTSubscriberConfig",
45 specification: "SubscriberSpecification[Any, Any]",
46 calls: "CallsCollection[zmqtt.Message]",
47 ) -> None:
48 self._address = config.address
49 # version may not be available yet when subscriber is created on a router
50 # before include_router is called; default to V5 and re-resolve in start().
51 parser = self._make_parser(config._outer_config)
52 config.parser = parser.parse_message
53 config.decoder = parser.decode_message
54 super().__init__(config, specification, calls)
55 self._shared = config.shared
56 self._qos = config.qos
57 self._subscription: zmqtt.Subscription | None = None
58 self.last_unsubscribe_result: zmqtt.UnsubscribeResult | None = None
60 if config.ack_policy is AckPolicy.NACK_ON_ERROR: 60 ↛ 61line 60 didn't jump to line 61 because the condition on line 60 was never true
61 warnings.warn(
62 "MQTT has no nack primitive; with NACK_ON_ERROR, "
63 "on error QoS 1/2 messages will not be acknowledged "
64 "and the broker will redeliver them.",
65 RuntimeWarning,
66 stacklevel=3,
67 )
69 def _build_parser(self) -> MQTTBaseParser:
70 return self._make_parser(self._outer_config)
72 def _make_parser(self, outer_config: "MQTTBrokerConfig") -> MQTTBaseParser:
73 version: MQTTVersion | Literal["unset"] = outer_config.version
74 if version == "unset":
75 # Declared on a Router, before a Broker composes its version in.
76 version = "5.0"
77 prefix = getattr(outer_config, "prefix", "")
78 return parser_for(version)(path_regex=self._address.add_prefix(prefix).regex)
80 @property
81 def address(self) -> "Address":
82 """The topic this Subscriber was declared with, and its Broker address."""
83 return self._address.add_prefix(self._outer_config.prefix)
85 @property
86 def topic(self) -> str:
87 full = self.address.broker_address
88 return f"$share/{self._shared}/{full}" if self._shared else full
90 def _make_response_publisher(
91 self,
92 message: "StreamMessage[Any]",
93 ) -> Sequence["PublisherProto"]:
94 return (
95 MQTTFakePublisher(
96 producer=self._outer_config.producer,
97 topic=message.reply_to,
98 ),
99 )
101 @staticmethod
102 def build_log_context(
103 message: "StreamMessage[zmqtt.Message] | None",
104 topic: str = "",
105 ) -> dict[str, str]:
106 return {
107 "topic": topic,
108 "message_id": getattr(message, "message_id", ""),
109 }
111 def get_log_context(
112 self,
113 message: "StreamMessage[zmqtt.Message] | None",
114 ) -> dict[str, str]:
115 return self.build_log_context(message=message, topic=self.topic)
117 @override
118 async def start(self) -> None:
119 # Re-resolve the parser now that _outer_config is fully composed
120 # (i.e. include_router has been called and the broker's MQTTBrokerConfig
121 # is reachable through the config chain).
122 parser = self._build_parser()
123 self._parser = parser.parse_message
124 self._decoder = parser.decode_message
126 await super().start()
128 if self.calls:
129 await self._create_subscription()
130 self.add_task(self._consume_loop, restart_on_failure=False)
132 self._post_start()
134 @override
135 async def stop(self) -> None:
136 # Stop receiving before TasksMixin cancels the consume task. In particular,
137 # StopConsume calls this method from that task, so cancelling it first would
138 # also cancel an in-flight UNSUBSCRIBE.
139 self.running = False
140 if self._subscription is not None:
141 self.last_unsubscribe_result = None
142 with suppress(Exception):
143 self.last_unsubscribe_result = await self._subscription.stop()
144 self._subscription = None
146 await super().stop()
148 async def _create_subscription(self) -> None:
149 auto_ack = self.ack_policy is AckPolicy.ACK_FIRST
150 self._subscription = self._outer_config.client.subscribe(
151 self.topic,
152 qos=zmqtt.QoS(self._qos),
153 auto_ack=auto_ack,
154 )
155 await self._subscription.start()
157 @override
158 async def get_one(
159 self,
160 *,
161 timeout: float = 5.0,
162 ) -> "MQTTMessage | None":
163 assert not self.calls, (
164 "You can't use `get_one` method if subscriber has registered handlers."
165 )
167 if self._subscription is None:
168 auto_ack = self.ack_policy is AckPolicy.ACK_FIRST
169 self._subscription = self._outer_config.client.subscribe(
170 self.topic,
171 qos=zmqtt.QoS(self._qos),
172 auto_ack=auto_ack,
173 )
174 await self._subscription.start()
176 async_parser, async_decoder = self._get_parser_and_decoder()
178 raw_msg: zmqtt.Message | None = None
179 with anyio.move_on_after(timeout):
180 raw_msg = await self._subscription.get_message()
182 context = self._outer_config.context
183 msg: MQTTMessage | None = await process_msg( # type: ignore[assignment]
184 msg=raw_msg,
185 middlewares=(m(raw_msg, context=context) for m in self._broker_middlewares),
186 parser=async_parser,
187 decoder=async_decoder,
188 )
189 return msg
191 @override
192 async def __aiter__(self) -> AsyncIterator["MQTTMessage"]:
193 if self._subscription is None: 193 ↛ 196line 193 didn't jump to line 196 because the condition on line 193 was always true
194 await self._create_subscription()
196 assert self._subscription is not None
197 context = self._outer_config.context
198 async_parser, async_decoder = self._get_parser_and_decoder()
199 async for raw_msg in self._subscription:
200 msg: MQTTMessage = await process_msg( # type: ignore[assignment]
201 msg=raw_msg,
202 middlewares=(
203 m(raw_msg, context=context) for m in self._broker_middlewares
204 ),
205 parser=async_parser,
206 decoder=async_decoder,
207 )
208 yield msg
210 @abstractmethod
211 async def _consume_loop(self) -> None:
212 raise NotImplementedError
215class MQTTDefaultSubscriber(MQTTBaseSubscriber):
216 """Sequential MQTT subscriber — processes one message at a time."""
218 __slots__ = ()
220 async def _consume_loop(self) -> None:
221 assert self._subscription is not None
222 async for msg in self._subscription:
223 await self.consume(msg)
226class MQTTConcurrentSubscriber(ConcurrentMixin[zmqtt.Message], MQTTBaseSubscriber):
227 """Concurrent MQTT subscriber — processes up to max_workers messages in parallel."""
229 __slots__ = ()
231 @override
232 async def start(self) -> None:
233 await super().start()
234 self.start_consume_task()
236 async def _consume_loop(self) -> None:
237 assert self._subscription is not None
238 async for msg in self._subscription:
239 await self._put_msg(msg)