diff --git a/sqlx-core/src/pool/inner.rs b/sqlx-core/src/pool/inner.rs index 4254aee325..58e0c1a7fa 100644 --- a/sqlx-core/src/pool/inner.rs +++ b/sqlx-core/src/pool/inner.rs @@ -370,7 +370,11 @@ impl PoolInner { } // an IO error while connecting is assumed to be the system starting up - Ok(Err(Error::Io(e))) if e.kind() == std::io::ErrorKind::ConnectionRefused => (), + Ok(Err(Error::Io(e))) if e.kind() == std::io::ErrorKind::ConnectionRefused => { + if self.options.return_con_refused { + return Err(Error::Io(e)); + } + } // We got a transient database error, retry. Ok(Err(Error::Database(error))) if error.is_transient_in_connect_phase() => (), diff --git a/sqlx-core/src/pool/options.rs b/sqlx-core/src/pool/options.rs index 3d048f1795..89477985ac 100644 --- a/sqlx-core/src/pool/options.rs +++ b/sqlx-core/src/pool/options.rs @@ -77,6 +77,7 @@ pub struct PoolOptions { pub(crate) max_connections: u32, pub(crate) acquire_time_level: LevelFilter, pub(crate) acquire_slow_level: LevelFilter, + pub(crate) return_con_refused: bool, pub(crate) acquire_slow_threshold: Duration, pub(crate) acquire_timeout: Duration, pub(crate) min_connections: u32, @@ -101,6 +102,7 @@ impl Clone for PoolOptions { acquire_time_level: self.acquire_time_level, acquire_slow_threshold: self.acquire_slow_threshold, acquire_slow_level: self.acquire_slow_level, + return_con_refused: self.return_con_refused, acquire_timeout: self.acquire_timeout, min_connections: self.min_connections, max_lifetime: self.max_lifetime, @@ -154,6 +156,7 @@ impl PoolOptions { acquire_time_level: LevelFilter::Off, // Default to warning, because an acquire timeout will be an error acquire_slow_level: LevelFilter::Warn, + return_con_refused: false, // Fast enough to catch problems (e.g. a full pool); slow enough // to not flag typical time to add a new connection to a pool. acquire_slow_threshold: Duration::from_secs(2), @@ -229,6 +232,13 @@ impl PoolOptions { self } + /// immediately return connection refused errors instead of hanging + /// until returning PoolTimedOut + pub fn return_con_refused(mut self, value: bool) -> Self { + self.return_con_refused = value; + self + } + /// Set a threshold for reporting excessive time taken to acquire a connection from /// the connection pool via [`Pool::acquire()`]. When the threshold is exceeded, a warning is logged. ///