diff --git a/src/transport/smtp/async_transport.rs b/src/transport/smtp/async_transport.rs index 08f5ebc..a64d598 100644 --- a/src/transport/smtp/async_transport.rs +++ b/src/transport/smtp/async_transport.rs @@ -147,11 +147,11 @@ where /// [`AsyncSmtpTransport::starttls_relay`](#method.starttls_relay) instead, /// if possible. pub fn builder_dangerous>(server: T) -> AsyncSmtpTransportBuilder { - let new = SmtpInfo { + let info = SmtpInfo { server: server.into(), ..Default::default() }; - AsyncSmtpTransportBuilder { info: new } + AsyncSmtpTransportBuilder { info } } } @@ -237,6 +237,7 @@ impl AsyncSmtpTransportBuilder { info: self.info, marker_: PhantomData, }; + AsyncSmtpTransport { inner: client } } } diff --git a/src/transport/smtp/pool.rs b/src/transport/smtp/pool.rs deleted file mode 100644 index 3c495a8..0000000 --- a/src/transport/smtp/pool.rs +++ /dev/null @@ -1,111 +0,0 @@ -use std::time::Duration; - -use crate::transport::smtp::{client::SmtpConnection, error, error::Error, SmtpClient}; - -use r2d2::{CustomizeConnection, ManageConnection, Pool}; - -/// Configuration for a connection pool -#[derive(Debug, Clone)] -#[allow(missing_copy_implementations)] -#[cfg_attr(docsrs, doc(cfg(feature = "r2d2")))] -pub struct PoolConfig { - min_idle: u32, - max_size: u32, - connection_timeout: Duration, - idle_timeout: Duration, -} - -impl PoolConfig { - /// Create a new pool configuration with default values - pub fn new() -> Self { - Self::default() - } - - /// Minimum number of idle connections - /// - /// Defaults to `0` - pub fn min_idle(mut self, min_idle: u32) -> Self { - self.min_idle = min_idle; - self - } - - /// Maximum number of pooled connections - /// - /// Defaults to `10` - pub fn max_size(mut self, max_size: u32) -> Self { - self.max_size = max_size; - self - } - - /// Connection timeout - /// - /// Defaults to `30 seconds` - pub fn connection_timeout(mut self, connection_timeout: Duration) -> Self { - self.connection_timeout = connection_timeout; - self - } - - /// Connection idle timeout - /// - /// Defaults to `60 seconds` - pub fn idle_timeout(mut self, idle_timeout: Duration) -> Self { - self.idle_timeout = idle_timeout; - self - } - - pub(crate) fn build>( - &self, - client: C, - ) -> Pool { - Pool::builder() - .min_idle(Some(self.min_idle)) - .max_size(self.max_size) - .connection_timeout(self.connection_timeout) - .idle_timeout(Some(self.idle_timeout)) - .connection_customizer(Box::new(SmtpConnectionQuitter)) - .build_unchecked(client) - } -} - -impl Default for PoolConfig { - fn default() -> Self { - Self { - min_idle: 0, - max_size: 10, - connection_timeout: Duration::from_secs(30), - idle_timeout: Duration::from_secs(60), - } - } -} - -impl ManageConnection for SmtpClient { - type Connection = SmtpConnection; - type Error = Error; - - fn connect(&self) -> Result { - self.connection() - } - - fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Error> { - if conn.test_connected() { - return Ok(()); - } - Err(error::network("is not connected anymore")) - } - - fn has_broken(&self, conn: &mut Self::Connection) -> bool { - conn.has_broken() - } -} - -#[derive(Copy, Clone, Debug)] -struct SmtpConnectionQuitter; - -impl CustomizeConnection for SmtpConnectionQuitter { - fn on_release(&self, conn: SmtpConnection) { - let mut conn = conn; - if !conn.has_broken() { - let _quit = conn.quit(); - } - } -} diff --git a/src/transport/smtp/pool/mod.rs b/src/transport/smtp/pool/mod.rs new file mode 100644 index 0000000..474348f --- /dev/null +++ b/src/transport/smtp/pool/mod.rs @@ -0,0 +1,67 @@ +use std::time::Duration; + +#[cfg(feature = "r2d2")] +pub mod sync_impl; + +/// Configuration for a connection pool +#[derive(Debug, Clone)] +#[allow(missing_copy_implementations)] +#[cfg_attr(docsrs, doc(cfg(any(feature = "r2d2", feature = "pool"))))] +pub struct PoolConfig { + min_idle: u32, + max_size: u32, + connection_timeout: Duration, + idle_timeout: Duration, +} + +impl PoolConfig { + /// Create a new pool configuration with default values + pub fn new() -> Self { + Self::default() + } + + /// Minimum number of idle connections + /// + /// Defaults to `0` + pub fn min_idle(mut self, min_idle: u32) -> Self { + self.min_idle = min_idle; + self + } + + /// Maximum number of pooled connections + /// + /// Defaults to `10` + pub fn max_size(mut self, max_size: u32) -> Self { + self.max_size = max_size; + self + } + + /// Connection timeout + /// + /// Defaults to `30 seconds` + #[doc(hidden)] + #[deprecated(note = "The Connection timeout is already configured on the SMTP transport")] + pub fn connection_timeout(mut self, connection_timeout: Duration) -> Self { + self.connection_timeout = connection_timeout; + self + } + + /// Connection idle timeout + /// + /// Defaults to `60 seconds` + pub fn idle_timeout(mut self, idle_timeout: Duration) -> Self { + self.idle_timeout = idle_timeout; + self + } +} + +impl Default for PoolConfig { + fn default() -> Self { + Self { + min_idle: 0, + max_size: 10, + connection_timeout: Duration::from_secs(30), + idle_timeout: Duration::from_secs(60), + } + } +} diff --git a/src/transport/smtp/pool/sync_impl.rs b/src/transport/smtp/pool/sync_impl.rs new file mode 100644 index 0000000..7a3dd44 --- /dev/null +++ b/src/transport/smtp/pool/sync_impl.rs @@ -0,0 +1,51 @@ +use r2d2::{CustomizeConnection, ManageConnection, Pool}; + +use super::PoolConfig; +use crate::transport::smtp::{client::SmtpConnection, error, error::Error, SmtpClient}; + +impl PoolConfig { + pub(crate) fn build>( + &self, + client: C, + ) -> Pool { + Pool::builder() + .min_idle(Some(self.min_idle)) + .max_size(self.max_size) + .connection_timeout(self.connection_timeout) + .idle_timeout(Some(self.idle_timeout)) + .connection_customizer(Box::new(SmtpConnectionQuitter)) + .build_unchecked(client) + } +} + +impl ManageConnection for SmtpClient { + type Connection = SmtpConnection; + type Error = Error; + + fn connect(&self) -> Result { + self.connection() + } + + fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Error> { + if conn.test_connected() { + return Ok(()); + } + Err(error::network("is not connected anymore")) + } + + fn has_broken(&self, conn: &mut Self::Connection) -> bool { + conn.has_broken() + } +} + +#[derive(Copy, Clone, Debug)] +struct SmtpConnectionQuitter; + +impl CustomizeConnection for SmtpConnectionQuitter { + fn on_release(&self, conn: SmtpConnection) { + let mut conn = conn; + if !conn.has_broken() { + let _quit = conn.quit(); + } + } +}