Skip to content

sqlx-core pools optionally immediately return ConnectionRefused errors instead of hanging and returning PoolTimedOut #3929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion sqlx-core/src/pool/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,11 @@ impl<DB: Database> PoolInner<DB> {
}

// 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() => (),
Expand Down
10 changes: 10 additions & 0 deletions sqlx-core/src/pool/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub struct PoolOptions<DB: Database> {
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,
Expand All @@ -101,6 +102,7 @@ impl<DB: Database> Clone for PoolOptions<DB> {
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,
Expand Down Expand Up @@ -154,6 +156,7 @@ impl<DB: Database> PoolOptions<DB> {
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),
Expand Down Expand Up @@ -229,6 +232,13 @@ impl<DB: Database> PoolOptions<DB> {
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.
///
Expand Down