Coverage for faststream/rabbit/broker/broker.py: 90%
69 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 logging
2from collections.abc import Iterable, Sequence
3from typing import (
4 TYPE_CHECKING,
5 Any,
6 Optional,
7 Union,
8 cast,
9)
10from urllib.parse import urlparse
12import anyio
13from aio_pika import IncomingMessage, RobustConnection, connect_robust
14from fast_depends import Provider, dependency_provider
15from typing_extensions import override
17from faststream.__about__ import SERVICE_NAME
18from faststream._internal.broker import BrokerUsecase
19from faststream._internal.constants import EMPTY
20from faststream._internal.context.repository import ContextRepo
21from faststream._internal.di import FastDependsConfig
22from faststream._internal.types import IdGenerator
23from faststream.message import gen_cor_id
24from faststream.middlewares import AckPolicy
25from faststream.rabbit.configs import RabbitBrokerConfig
26from faststream.rabbit.helpers.channel_manager import ChannelManagerImpl
27from faststream.rabbit.helpers.declarer import RabbitDeclarerImpl
28from faststream.rabbit.publisher.producer import (
29 AioPikaFastProducerImpl,
30)
31from faststream.rabbit.response import RabbitPublishCommand
32from faststream.rabbit.schemas import (
33 RABBIT_REPLY,
34 Channel,
35 RabbitExchange,
36 RabbitQueue,
37)
38from faststream.rabbit.security import parse_security
39from faststream.rabbit.utils import build_url
40from faststream.response.publish_type import PublishType
41from faststream.specification.schema import BrokerSpec
43from .logging import make_rabbit_logger_state
44from .registrator import RabbitRegistrator
46if TYPE_CHECKING:
47 from types import TracebackType
49 import aiormq
50 from aio_pika import (
51 RobustChannel,
52 RobustExchange,
53 RobustQueue,
54 )
55 from aio_pika.abc import (
56 AbstractConnection,
57 AbstractRobustConnection,
58 CallbackType,
59 DateType,
60 HeadersType,
61 SSLOptions,
62 TimeoutType,
63 )
64 from fast_depends.dependencies import Dependant
65 from fast_depends.library.serializer import SerializerProto
66 from yarl import URL
68 from faststream._internal.basic_types import LoggerProto
69 from faststream._internal.configs import UnderlyingDriverAnnotations
70 from faststream._internal.parser import CodecProto
71 from faststream._internal.types import (
72 BrokerMiddleware,
73 CustomCallable,
74 )
75 from faststream.rabbit.helpers import RabbitDeclarer
76 from faststream.rabbit.message import RabbitMessage
77 from faststream.rabbit.types import AioPikaSendableMessage
78 from faststream.rabbit.utils import RabbitClientProperties
79 from faststream.security import BaseSecurity
80 from faststream.specification.schema.extra import Tag, TagDict
83class RabbitBroker(
84 RabbitRegistrator,
85 BrokerUsecase[IncomingMessage, RobustConnection, RabbitBrokerConfig],
86):
87 """A class to represent a RabbitMQ broker."""
89 def __init__(
90 self,
91 url: Union[str, "URL", None] = "amqp://guest:guest@localhost:5672/",
92 *,
93 host: str | None = None,
94 port: int | None = None,
95 virtualhost: str | None = None,
96 ssl_options: Optional["SSLOptions"] = None,
97 client_properties: Optional["RabbitClientProperties"] = None,
98 timeout: "TimeoutType" = None,
99 fail_fast: bool = True,
100 reconnect_interval: "TimeoutType" = 5.0,
101 on_close: Sequence[
102 "CallbackType[AbstractConnection, [BaseException | None], None]"
103 ]
104 | None = None,
105 on_reconnected: Sequence["CallbackType[AbstractRobustConnection, [], None]"]
106 | None = None,
107 default_channel: Optional["Channel"] = None,
108 app_id: str | None = SERVICE_NAME,
109 # broker base args
110 graceful_timeout: float | None = 15.0,
111 ack_policy: AckPolicy = EMPTY,
112 id_generator: IdGenerator = gen_cor_id,
113 decoder: Optional["CustomCallable"] = None,
114 codec: Optional["CodecProto"] = None,
115 parser: Optional["CustomCallable"] = None,
116 dependencies: Sequence["Dependant"] = (),
117 middlewares: Sequence["BrokerMiddleware[Any, Any]"] = (),
118 routers: Iterable[RabbitRegistrator] = (),
119 # AsyncAPI args
120 security: Optional["BaseSecurity"] = None,
121 specification_url: str | None = None,
122 protocol: str | None = None,
123 protocol_version: str | None = "0.9.1",
124 description: str | None = None,
125 tags: Iterable[Union["Tag", "TagDict"]] = (),
126 # logging args
127 logger: Optional["LoggerProto"] = EMPTY,
128 log_level: int = logging.INFO,
129 # FastDepends args
130 apply_types: bool = True,
131 serializer: Optional["SerializerProto"] = EMPTY,
132 provider: Optional["Provider"] = None,
133 context: Optional["ContextRepo"] = None,
134 underlying_driver_annotations: "UnderlyingDriverAnnotations | None" = None,
135 ) -> None:
136 """Initialize the RabbitBroker.
138 Args:
139 url: RabbitMQ destination location to connect.
140 host: Destination host. This option overrides `url` option host.
141 port: Destination port. This option overrides `url` option port.
142 virtualhost: RabbitMQ virtual host to use in the current broker connection.
143 ssl_options: Extra ssl options to establish connection.
144 client_properties: Add custom client capability.
145 timeout: Connection establishment timeout.
146 fail_fast: Broker startup raises `AMQPConnectionError` if RabbitMQ is unreachable.
147 reconnect_interval: Time to sleep between reconnection attempts.
148 on_close: Callbacks receiving the connection and closing exception when the connection closes, including during shutdown. Supports sync and async functions. Execution order is not guaranteed.
149 on_reconnected: Callbacks receiving the connection after reconnection. Supports sync and async functions. Execution order is not guaranteed.
150 default_channel: Default channel settings to use.
151 app_id: Application name to mark outgoing messages by.
152 graceful_timeout: Graceful shutdown timeout. Broker waits for all running subscribers completion before shut down.
153 ack_policy: Default acknowledgement policy for all subscribers. Individual subscribers can override.
154 id_generator: Factory used to generate `correlation_id` when a publish/request call doesn't set one explicitly.
155 Defaults to `gen_cor_id` (uuid4-based).
156 decoder: Custom decoder object.
157 codec: Custom codec object.
158 parser: Custom parser object.
159 dependencies: Dependencies to apply to all broker subscribers.
160 middlewares: Middlewares to apply to all broker publishers/subscribers.
161 routers: RabbitRouters to build a broker with.
162 security: Security options to connect broker and generate AsyncAPI server security information.
163 Use RabbitExternalAuth for RabbitMQ SASL EXTERNAL.
164 specification_url: AsyncAPI hardcoded server addresses. Use `servers` if not specified.
165 protocol: AsyncAPI server protocol.
166 protocol_version: AsyncAPI server protocol version.
167 description: AsyncAPI server description.
168 tags: AsyncAPI server tags.
169 logger: User-specified logger to pass into Context and log service messages.
170 log_level: Service messages log level.
171 apply_types: Whether to use FastDepends or not.
172 serializer: FastDepends-compatible serializer to validate incoming messages.
173 provider: Provider for FastDepends.
174 context: Context for FastDepends.
175 underlying_driver_annotations: Extra driver type hints that
176 FastStream cannot inject, mapped to the annotation to use
177 instead. Merged over the broker's own rows. Wrap a value in
178 `UnderlyingDriverAnnotation` to name the import to suggest.
179 """
180 security_args = parse_security(security)
182 amqp_url = build_url(
183 url,
184 host=host,
185 port=port,
186 virtualhost=virtualhost,
187 ssl_options=ssl_options,
188 client_properties=client_properties,
189 login=security_args.get("login"),
190 password=security_args.get("password"),
191 ssl=security_args.get("ssl"),
192 auth=security_args.get("auth"),
193 )
195 if specification_url is None:
196 specification_url = str(amqp_url.with_user(None))
198 # respect ascynapi_url argument scheme
199 built_asyncapi_url = urlparse(specification_url)
200 if protocol is None:
201 protocol = built_asyncapi_url.scheme
203 cm = ChannelManagerImpl(default_channel)
204 declarer = RabbitDeclarerImpl(cm)
206 producer = AioPikaFastProducerImpl(
207 declarer=declarer,
208 decoder=decoder,
209 parser=parser,
210 id_generator=id_generator,
211 )
213 super().__init__(
214 # connection args
215 url=str(amqp_url),
216 ssl_context=security_args.get("ssl_context"),
217 timeout=timeout,
218 fail_fast=fail_fast,
219 reconnect_interval=reconnect_interval,
220 # Basic args
221 routers=routers,
222 config=RabbitBrokerConfig(
223 channel_manager=cm,
224 producer=producer,
225 declarer=declarer,
226 app_id=app_id,
227 virtual_host=built_asyncapi_url.path,
228 # both args
229 broker_middlewares=middlewares,
230 broker_parser=parser,
231 broker_decoder=decoder,
232 broker_codec=codec,
233 logger=make_rabbit_logger_state(
234 logger=logger,
235 log_level=log_level,
236 ),
237 fd_config=FastDependsConfig(
238 use_fastdepends=apply_types,
239 serializer=serializer,
240 provider=provider or dependency_provider,
241 context=context or ContextRepo(),
242 ),
243 # subscriber args
244 broker_dependencies=dependencies,
245 graceful_timeout=graceful_timeout,
246 ack_policy=ack_policy,
247 id_generator=id_generator,
248 extra_context={
249 "broker": self,
250 },
251 underlying_driver_annotations=underlying_driver_annotations or {},
252 ),
253 specification=BrokerSpec(
254 description=description,
255 url=[specification_url],
256 protocol=protocol or built_asyncapi_url.scheme,
257 protocol_version=protocol_version,
258 security=security,
259 tags=tags,
260 ),
261 )
263 self._channel: RobustChannel | None = None
264 self._on_close = tuple(on_close) if on_close is not None else ()
265 self._on_reconnected = tuple(on_reconnected) if on_reconnected is not None else ()
267 @override
268 async def _connect(self) -> "RobustConnection":
269 connection = cast(
270 "RobustConnection",
271 await connect_robust(**self._connection_kwargs),
272 )
274 for close_callback in self._on_close:
275 connection.close_callbacks.add(close_callback)
276 for reconnect_callback in self._on_reconnected:
277 connection.reconnect_callbacks.add(reconnect_callback)
279 if self._channel is None: 279 ↛ 283line 279 didn't jump to line 283 because the condition on line 279 was always true
280 self.config.connect(connection)
281 self._channel = await self.config.channel_manager.get_channel()
283 return connection
285 async def stop(
286 self,
287 exc_type: type[BaseException] | None = None,
288 exc_val: BaseException | None = None,
289 exc_tb: Optional["TracebackType"] = None,
290 ) -> None:
291 await super().stop(exc_type, exc_val, exc_tb)
293 if self._channel is not None:
294 if not self._channel.is_closed: 294 ↛ 297line 294 didn't jump to line 297 because the condition on line 294 was always true
295 await self._channel.close()
297 self._channel = None
299 if self._connection is not None:
300 await self._connection.close()
301 self._connection = None
303 self.config.disconnect()
305 async def start(self) -> None:
306 """Connect broker to RabbitMQ and startup all subscribers."""
307 await self.connect()
308 await self.declare_queue(RABBIT_REPLY)
309 await super().start()
311 @override
312 async def publish(
313 self,
314 message: "AioPikaSendableMessage" = None,
315 queue: Union["RabbitQueue", str] = "",
316 exchange: Union["RabbitExchange", str, None] = None,
317 *,
318 routing_key: str = "",
319 # publish options
320 mandatory: bool = True,
321 immediate: bool = False,
322 timeout: "TimeoutType" = None,
323 persist: bool = False,
324 reply_to: str | None = None,
325 correlation_id: str | None = None,
326 # message options
327 headers: Optional["HeadersType"] = None,
328 content_type: str | None = None,
329 content_encoding: str | None = None,
330 expiration: Optional["DateType"] = None,
331 message_id: str | None = None,
332 timestamp: Optional["DateType"] = None,
333 message_type: str | None = None,
334 user_id: str | None = None,
335 priority: int | None = None,
336 ) -> Optional["aiormq.abc.ConfirmationFrameType"]:
337 """Publish message directly.
339 This method allows you to publish message in not AsyncAPI-documented way. You can use it in another frameworks
340 applications or to publish messages from time to time.
342 Please, use `@broker.publisher(...)` or `broker.publisher(...).publish(...)` instead in a regular way.
344 Args:
345 message:
346 Message body to send.
347 queue:
348 Message routing key to publish with.
349 exchange:
350 Target exchange to publish message to.
351 routing_key:
352 Message routing key to publish with. Overrides `queue` option if presented.
353 mandatory:
354 Client waits for confirmation that the message is placed to some queue. RabbitMQ returns message to client if there is no suitable queue.
355 immediate:
356 Client expects that there is consumer ready to take the message to work. RabbitMQ returns message to client if there is no suitable consumer.
357 timeout:
358 Send confirmation time from RabbitMQ.
359 persist:
360 Restore the message on RabbitMQ reboot.
361 reply_to:
362 Reply message routing key to send with (always sending to default exchange).
363 correlation_id:
364 Manual message **correlation_id** setter. **correlation_id** is a useful option to trace messages.
365 headers:
366 Message headers to store metainformation.
367 content_type:
368 Message **content-type** header. Used by application, not core RabbitMQ. Will be set automatically if not specified.
369 content_encoding:
370 Message body content encoding, e.g. **gzip**.
371 expiration:
372 Message expiration (lifetime) in seconds (or datetime or timedelta).
373 message_id:
374 Arbitrary message id. Generated automatically if not present.
375 timestamp:
376 Message publish timestamp. Generated automatically if not presented.
377 message_type:
378 Application-specific message type, e.g. **orders.created**.
379 user_id:
380 Publisher connection User ID, validated if set.
381 priority:
382 The message priority (0 by default).
384 Returns:
385 An optional `aiormq.abc.ConfirmationFrameType` representing the confirmation frame if RabbitMQ is configured to send confirmations.
386 """
387 cmd = RabbitPublishCommand(
388 message,
389 routing_key=routing_key or RabbitQueue.validate(queue).routing(),
390 exchange=RabbitExchange.validate(exchange),
391 correlation_id=correlation_id or self.config.id_generator(),
392 app_id=self.config.app_id,
393 mandatory=mandatory,
394 immediate=immediate,
395 persist=persist,
396 reply_to=reply_to,
397 headers=headers,
398 content_type=content_type,
399 content_encoding=content_encoding,
400 expiration=expiration,
401 message_id=message_id,
402 message_type=message_type,
403 timestamp=timestamp,
404 user_id=user_id,
405 timeout=timeout,
406 priority=priority,
407 _publish_type=PublishType.PUBLISH,
408 )
410 result: aiormq.abc.ConfirmationFrameType | None = await super()._basic_publish(
411 cmd,
412 producer=self._producer,
413 )
414 return result
416 @override
417 async def request( # type: ignore[override]
418 self,
419 message: "AioPikaSendableMessage" = None,
420 queue: Union["RabbitQueue", str] = "",
421 exchange: Union["RabbitExchange", str, None] = None,
422 *,
423 routing_key: str = "",
424 mandatory: bool = True,
425 immediate: bool = False,
426 timeout: "TimeoutType" = None,
427 persist: bool = False,
428 # message args
429 correlation_id: str | None = None,
430 headers: Optional["HeadersType"] = None,
431 content_type: str | None = None,
432 content_encoding: str | None = None,
433 expiration: Optional["DateType"] = None,
434 message_id: str | None = None,
435 timestamp: Optional["DateType"] = None,
436 message_type: str | None = None,
437 user_id: str | None = None,
438 priority: int | None = None,
439 ) -> "RabbitMessage":
440 """Make a synchronous request to RabbitMQ.
442 This method uses Direct Reply-To pattern to send a message and wait for a reply.
443 It is a blocking call and will wait for a reply until timeout.
445 Args:
446 message: Message body to send.
447 queue: Message routing key to publish with.
448 exchange: Target exchange to publish message to.
449 routing_key: Message routing key to publish with. Overrides `queue` option if presented.
450 mandatory: Client waits for confirmation that the message is placed to some queue.
451 RabbitMQ returns message to client if there is no suitable queue.
452 immediate: Client expects that there is a consumer ready to take the message to work.
453 RabbitMQ returns message to client if there is no suitable consumer.
454 timeout: Send confirmation time from RabbitMQ.
455 persist: Restore the message on RabbitMQ reboot.
456 correlation_id: Manual message **correlation_id** setter. **correlation_id** is a useful option to trace messages.
457 headers: Message headers to store metainformation.
458 content_type: Message **content-type** header. Used by application, not core RabbitMQ.
459 Will be set automatically if not specified.
460 content_encoding: Message body content encoding, e.g. **gzip**.
461 expiration: Message expiration (lifetime) in seconds (or datetime or timedelta).
462 message_id: Arbitrary message id. Generated automatically if not present.
463 timestamp: Message publish timestamp. Generated automatically if not present.
464 message_type: Application-specific message type, e.g. **orders.created**.
465 user_id: Publisher connection User ID, validated if set.
466 priority: The message priority (0 by default).
467 """
468 cmd = RabbitPublishCommand(
469 message,
470 routing_key=routing_key or RabbitQueue.validate(queue).routing(),
471 exchange=RabbitExchange.validate(exchange),
472 correlation_id=correlation_id or self.config.id_generator(),
473 app_id=self.config.app_id,
474 mandatory=mandatory,
475 immediate=immediate,
476 persist=persist,
477 headers=headers,
478 content_type=content_type,
479 content_encoding=content_encoding,
480 expiration=expiration,
481 message_id=message_id,
482 message_type=message_type,
483 timestamp=timestamp,
484 user_id=user_id,
485 timeout=timeout,
486 priority=priority,
487 _publish_type=PublishType.REQUEST,
488 )
490 msg: RabbitMessage = await super()._basic_request(cmd, producer=self._producer)
491 return msg
493 async def declare_queue(self, queue: "RabbitQueue") -> "RobustQueue":
494 """Declares queue object in **RabbitMQ**."""
495 declarer: RabbitDeclarer = self.config.declarer
496 return await declarer.declare_queue(queue)
498 async def declare_exchange(self, exchange: "RabbitExchange") -> "RobustExchange":
499 """Declares exchange object in **RabbitMQ**."""
500 declarer: RabbitDeclarer = self.config.declarer
501 return await declarer.declare_exchange(exchange)
503 @override
504 async def ping(self, timeout: float | None) -> bool:
505 sleep_time = (timeout or 10) / 10
507 with anyio.move_on_after(timeout) as cancel_scope:
508 if self._connection is None: 508 ↛ 509line 508 didn't jump to line 509 because the condition on line 508 was never true
509 return False
511 while True:
512 if cancel_scope.cancel_called: 512 ↛ 513line 512 didn't jump to line 513 because the condition on line 512 was never true
513 return False
515 if self._connection.connected.is_set(): 515 ↛ 518line 515 didn't jump to line 518 because the condition on line 515 was always true
516 return True
518 await anyio.sleep(sleep_time)
520 return False