deadpool_amqprs/
lib.rs

1#![doc = include_str!("../README.md")]
2#![allow(clippy::module_name_repetitions)]
3
4pub mod config;
5
6pub use amqprs;
7use amqprs::connection::OpenConnectionArguments;
8use deadpool::managed;
9pub use deadpool::managed::reexports::*;
10use deadpool::managed::{RecycleError, RecycleResult};
11
12pub use config::{Config, ConfigError};
13
14deadpool::managed_reexports!(
15    "amqprs",
16    Manager,
17    managed::Object<Manager>,
18    amqprs::error::Error,
19    ConfigError
20);
21
22struct Placeholder;
23
24impl std::fmt::Debug for Placeholder {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        write!(f, "..")
27    }
28}
29
30/// Type alias for [`Object`] in case Object isn't straight foward enough.
31pub type Connection = Object;
32
33/// [`Manager`] for creating and recycling [`amqprs`] connections.
34pub struct Manager {
35    con_args: OpenConnectionArguments,
36}
37
38impl Manager {
39    /// Creates a new [`Manager`] from the given arguments.
40    #[must_use]
41    #[inline]
42    pub const fn new(con_args: OpenConnectionArguments) -> Self {
43        Self { con_args }
44    }
45}
46
47impl std::fmt::Debug for Manager {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        f.debug_struct("Manager")
50            .field("con_args", &Placeholder)
51            .finish()
52    }
53}
54
55impl managed::Manager for Manager {
56    /// Type of [`Object`]s that this [`Manager`] creates and recycles.
57    type Type = amqprs::connection::Connection;
58    /// Error that this [`Manager`] can return when creating and/or recycling
59    /// [`Object`]s.
60    type Error = amqprs::error::Error;
61
62    /// Creates a new instance of [`Manager::Type`].
63    async fn create(&self) -> Result<Self::Type, Self::Error> {
64        Self::Type::open(&self.con_args).await
65    }
66
67    /// Tries to recycle an instance of [`Manager::Type`].
68    ///
69    /// # Errors
70    ///
71    /// Returns [`Manager::Error`] if the instance couldn't be recycled.
72    async fn recycle(&self, conn: &mut Self::Type, _: &Metrics) -> RecycleResult<Self::Error> {
73        if conn.is_open() {
74            Ok(())
75        } else {
76            Err(RecycleError::message("Connection closed."))
77        }
78    }
79}