|
| 1 | +//! Faktory support for the `bb8` connection pool. |
| 2 | +#![deny(missing_docs, missing_debug_implementations)] |
| 3 | + |
| 4 | +pub use bb8; |
| 5 | +pub use faktory; |
| 6 | + |
| 7 | +use faktory::{Client, Error}; |
| 8 | + |
| 9 | +#[cfg(any(feature = "native-tls", feature = "rustls"))] |
| 10 | +use url::Url; |
| 11 | + |
| 12 | +#[cfg(feature = "rustls")] |
| 13 | +use std::sync::Arc; |
| 14 | + |
| 15 | +/// TLS connector configuration. |
| 16 | +#[derive(Clone, Default)] |
| 17 | +pub enum TlsConnector { |
| 18 | + /// Plain TCP connection. |
| 19 | + #[default] |
| 20 | + NoTls, |
| 21 | + /// TLS using native-tls. |
| 22 | + #[cfg(feature = "native-tls")] |
| 23 | + NativeTls(native_tls::TlsConnector), |
| 24 | + /// TLS using rustls. |
| 25 | + #[cfg(feature = "rustls")] |
| 26 | + Rustls(tokio_rustls::TlsConnector), |
| 27 | +} |
| 28 | + |
| 29 | +impl std::fmt::Debug for TlsConnector { |
| 30 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 31 | + match self { |
| 32 | + Self::NoTls => f.debug_tuple("NoTls").finish(), |
| 33 | + #[cfg(feature = "native-tls")] |
| 34 | + Self::NativeTls(connector) => f.debug_tuple("NativeTls").field(connector).finish(), |
| 35 | + #[cfg(feature = "rustls")] |
| 36 | + Self::Rustls(_) => f.debug_tuple("Rustls").field(&"..").finish(), |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl TlsConnector { |
| 42 | + /// Create a native-tls connector with default settings. |
| 43 | + #[cfg(feature = "native-tls")] |
| 44 | + pub fn native_tls_default() -> Result<Self, native_tls::Error> { |
| 45 | + let connector = native_tls::TlsConnector::builder().build()?; |
| 46 | + Ok(Self::NativeTls(connector)) |
| 47 | + } |
| 48 | + |
| 49 | + /// Create a rustls connector with default settings (empty root store). |
| 50 | + /// |
| 51 | + /// Note: This uses an empty certificate store. For production use, |
| 52 | + /// consider using `rustls_default_with_native_certs()` or providing |
| 53 | + /// your own `ClientConfig`. |
| 54 | + #[cfg(feature = "rustls")] |
| 55 | + pub fn rustls_default() -> Self { |
| 56 | + let config = rustls::ClientConfig::builder() |
| 57 | + .with_root_certificates(rustls::RootCertStore::empty()) |
| 58 | + .with_no_client_auth(); |
| 59 | + Self::Rustls(tokio_rustls::TlsConnector::from(Arc::new(config))) |
| 60 | + } |
| 61 | + |
| 62 | + /// Create a rustls connector from a custom ClientConfig. |
| 63 | + #[cfg(feature = "rustls")] |
| 64 | + pub fn rustls_with_config(config: rustls::ClientConfig) -> Self { |
| 65 | + Self::Rustls(tokio_rustls::TlsConnector::from(Arc::new(config))) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// A `bb8::ManageConnection` for `faktory::Client`. |
| 70 | +#[derive(Debug, Clone)] |
| 71 | +pub struct FaktoryConnectionManager { |
| 72 | + url: String, |
| 73 | + tls: TlsConnector, |
| 74 | +} |
| 75 | + |
| 76 | +impl FaktoryConnectionManager { |
| 77 | + /// Create a new `FaktoryConnectionManager` with the specified URL. |
| 78 | + /// |
| 79 | + /// The URL should be in the format: `protocol://[:password@]hostname[:port]` |
| 80 | + /// |
| 81 | + /// # Examples |
| 82 | + /// |
| 83 | + /// ``` |
| 84 | + /// use bb8_faktory::FaktoryConnectionManager; |
| 85 | + /// |
| 86 | + /// let manager = FaktoryConnectionManager::new("tcp://localhost:7419"); |
| 87 | + /// ``` |
| 88 | + pub fn new(url: impl Into<String>) -> Self { |
| 89 | + Self { |
| 90 | + url: url.into(), |
| 91 | + tls: TlsConnector::default(), |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /// Create a new `FaktoryConnectionManager` using environment variables. |
| 96 | + /// |
| 97 | + /// This reads `FAKTORY_PROVIDER` to get the env var name (defaults to `FAKTORY_URL`), |
| 98 | + /// then reads that env var for the URL (defaults to `tcp://localhost:7419`). |
| 99 | + /// |
| 100 | + /// If a TLS feature is enabled, it will automatically use that TLS implementation |
| 101 | + /// with default settings. |
| 102 | + /// |
| 103 | + /// # Example |
| 104 | + /// |
| 105 | + /// ``` |
| 106 | + /// use bb8_faktory::FaktoryConnectionManager; |
| 107 | + /// |
| 108 | + /// let manager = FaktoryConnectionManager::from_env(); |
| 109 | + /// ``` |
| 110 | + pub fn from_env() -> Self { |
| 111 | + let url = std::env::var("FAKTORY_PROVIDER") |
| 112 | + .ok() |
| 113 | + .and_then(|provider| std::env::var(provider).ok()) |
| 114 | + .or_else(|| std::env::var("FAKTORY_URL").ok()) |
| 115 | + .unwrap_or_else(|| "tcp://localhost:7419".to_string()); |
| 116 | + |
| 117 | + #[cfg(feature = "rustls")] |
| 118 | + let tls = TlsConnector::rustls_default(); |
| 119 | + |
| 120 | + #[cfg(all(feature = "native-tls", not(feature = "rustls")))] |
| 121 | + let tls = TlsConnector::native_tls_default().expect("Failed to build native-tls connector"); |
| 122 | + |
| 123 | + #[cfg(not(any(feature = "native-tls", feature = "rustls")))] |
| 124 | + let tls = TlsConnector::NoTls; |
| 125 | + |
| 126 | + Self { url, tls } |
| 127 | + } |
| 128 | + |
| 129 | + /// Set the TLS connector for the connection. |
| 130 | + /// |
| 131 | + /// # Example |
| 132 | + /// |
| 133 | + /// ```ignore |
| 134 | + /// use bb8_faktory::{FaktoryConnectionManager, TlsConnector}; |
| 135 | + /// |
| 136 | + /// let manager = FaktoryConnectionManager::new("tcp+tls://localhost:7419") |
| 137 | + /// .with_tls(TlsConnector::native_tls_default().unwrap()); |
| 138 | + /// ``` |
| 139 | + pub fn with_tls(mut self, tls: TlsConnector) -> Self { |
| 140 | + self.tls = tls; |
| 141 | + self |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +impl bb8::ManageConnection for FaktoryConnectionManager { |
| 146 | + type Connection = Client; |
| 147 | + type Error = Error; |
| 148 | + |
| 149 | + async fn connect(&self) -> Result<Self::Connection, Self::Error> { |
| 150 | + match &self.tls { |
| 151 | + TlsConnector::NoTls => Client::connect_to(&self.url).await, |
| 152 | + #[cfg(feature = "native-tls")] |
| 153 | + TlsConnector::NativeTls(connector) => { |
| 154 | + let password = Url::parse(&self.url) |
| 155 | + .ok() |
| 156 | + .and_then(|url| url.password().map(|p| p.to_string())); |
| 157 | + let stream = faktory::native_tls::TlsStream::with_connector( |
| 158 | + connector.clone(), |
| 159 | + Some(self.url.as_str()), |
| 160 | + ) |
| 161 | + .await?; |
| 162 | + let buffered = tokio::io::BufStream::new(stream); |
| 163 | + Client::connect_with(buffered, password).await |
| 164 | + } |
| 165 | + #[cfg(feature = "rustls")] |
| 166 | + TlsConnector::Rustls(connector) => { |
| 167 | + let password = Url::parse(&self.url) |
| 168 | + .ok() |
| 169 | + .and_then(|url| url.password().map(|p| p.to_string())); |
| 170 | + let stream = faktory::rustls::TlsStream::with_connector( |
| 171 | + connector.clone(), |
| 172 | + Some(self.url.as_str()), |
| 173 | + ) |
| 174 | + .await?; |
| 175 | + let buffered = tokio::io::BufStream::new(stream); |
| 176 | + Client::connect_with(buffered, password).await |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + async fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> { |
| 182 | + conn.current_info().await.map(|_| ()) |
| 183 | + } |
| 184 | + |
| 185 | + fn has_broken(&self, _: &mut Self::Connection) -> bool { |
| 186 | + false |
| 187 | + } |
| 188 | +} |
0 commit comments