queuerious_lapin/channel_factory.rs
1//! Channel factory abstraction for durable AMQP channel management.
2//!
3//! Provides the [`ChannelFactory`] trait, which allows metrics collectors and
4//! command executors to obtain fresh AMQP channels on demand — enabling
5//! automatic recovery after connection drops.
6
7use async_trait::async_trait;
8
9/// Provides AMQP channels on demand, supporting reconnection.
10///
11/// Implement this trait to give the metrics collector and command executor
12/// the ability to recover from channel/connection failures. When a channel
13/// becomes stale (e.g. after a RabbitMQ restart), the collector will call
14/// `create_channel()` again to obtain a fresh one.
15///
16/// # Example
17///
18/// ```no_run
19/// use async_trait::async_trait;
20/// use queuerious_lapin::ChannelFactory;
21///
22/// struct MyConnectionPool { /* ... */ }
23///
24/// #[async_trait]
25/// impl ChannelFactory for MyConnectionPool {
26/// async fn create_channel(&self) -> Result<lapin::Channel, lapin::Error> {
27/// // Return a channel from your managed connection
28/// todo!()
29/// }
30/// }
31/// ```
32#[async_trait]
33pub trait ChannelFactory: Send + Sync + 'static {
34 /// Create a new AMQP channel.
35 ///
36 /// Implementations should handle reconnection internally if the
37 /// underlying connection has dropped.
38 async fn create_channel(&self) -> Result<lapin::Channel, lapin::Error>;
39}
40
41/// A simple factory that creates channels from a static [`lapin::Connection`].
42///
43/// Does **not** handle reconnection — if the connection drops, channel
44/// creation will fail until the connection is re-established externally.
45///
46/// For applications with their own connection management (e.g. connection pools
47/// or reconnect loops), implement [`ChannelFactory`] directly to integrate
48/// with your reconnection logic.
49pub struct StaticConnectionFactory {
50 connection: lapin::Connection,
51}
52
53impl StaticConnectionFactory {
54 /// Create a factory backed by the given connection.
55 pub fn new(connection: lapin::Connection) -> Self {
56 Self { connection }
57 }
58}
59
60#[async_trait]
61impl ChannelFactory for StaticConnectionFactory {
62 async fn create_channel(&self) -> Result<lapin::Channel, lapin::Error> {
63 self.connection.create_channel().await
64 }
65}