Skip to content

Commit 3e77113

Browse files
committed
immediate return when connection refused optional
1 parent c1cae2d commit 3e77113

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

sqlx-core/src/pool/inner.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,13 @@ impl<DB: Database> PoolInner<DB> {
369369
}
370370
}
371371

372+
// an IO error while connecting is assumed to be the system starting up
373+
Ok(Err(Error::Io(e))) if e.kind() == std::io::ErrorKind::ConnectionRefused => {
374+
if self.options.return_con_refused {
375+
return Err(Error::Io(e));
376+
}
377+
}
378+
372379
// We got a transient database error, retry.
373380
Ok(Err(Error::Database(error))) if error.is_transient_in_connect_phase() => (),
374381

sqlx-core/src/pool/options.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub struct PoolOptions<DB: Database> {
7777
pub(crate) max_connections: u32,
7878
pub(crate) acquire_time_level: LevelFilter,
7979
pub(crate) acquire_slow_level: LevelFilter,
80+
pub(crate) return_con_refused: bool,
8081
pub(crate) acquire_slow_threshold: Duration,
8182
pub(crate) acquire_timeout: Duration,
8283
pub(crate) min_connections: u32,
@@ -101,6 +102,7 @@ impl<DB: Database> Clone for PoolOptions<DB> {
101102
acquire_time_level: self.acquire_time_level,
102103
acquire_slow_threshold: self.acquire_slow_threshold,
103104
acquire_slow_level: self.acquire_slow_level,
105+
return_con_refused: self.return_con_refused,
104106
acquire_timeout: self.acquire_timeout,
105107
min_connections: self.min_connections,
106108
max_lifetime: self.max_lifetime,
@@ -154,6 +156,7 @@ impl<DB: Database> PoolOptions<DB> {
154156
acquire_time_level: LevelFilter::Off,
155157
// Default to warning, because an acquire timeout will be an error
156158
acquire_slow_level: LevelFilter::Warn,
159+
return_con_refused: false,
157160
// Fast enough to catch problems (e.g. a full pool); slow enough
158161
// to not flag typical time to add a new connection to a pool.
159162
acquire_slow_threshold: Duration::from_secs(2),
@@ -229,6 +232,13 @@ impl<DB: Database> PoolOptions<DB> {
229232
self
230233
}
231234

235+
/// immediately return connection refused errors instead of hanging
236+
/// until returning PoolTimedOut
237+
pub fn return_con_refused(mut self, value: bool) -> Self {
238+
self.return_con_refused = value;
239+
self
240+
}
241+
232242
/// Set a threshold for reporting excessive time taken to acquire a connection from
233243
/// the connection pool via [`Pool::acquire()`]. When the threshold is exceeded, a warning is logged.
234244
///

0 commit comments

Comments
 (0)