Configure acquire timeout for connection pool (#897)

* Reproduce "failed to acquire connection from pool" error

* Configure acquire_timeout

* Add tests

* Fixup

* Remove tests
This commit is contained in:
Billy Chan 2022-09-19 23:50:11 +08:00 committed by GitHub
parent 6f7529b0be
commit 968fc2e678
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -38,6 +38,8 @@ pub struct ConnectOptions {
/// Maximum idle time for a particular connection to prevent
/// network resource exhaustion
pub(crate) idle_timeout: Option<Duration>,
/// Set the maximum amount of time to spend waiting for acquiring a connection
pub(crate) acquire_timeout: Option<Duration>,
/// Set the maximum lifetime of individual connections
pub(crate) max_lifetime: Option<Duration>,
/// Enable SQLx statement logging
@ -107,6 +109,7 @@ impl ConnectOptions {
min_connections: None,
connect_timeout: None,
idle_timeout: None,
acquire_timeout: None,
max_lifetime: None,
sqlx_logging: true,
sqlx_logging_level: log::LevelFilter::Info,
@ -137,6 +140,9 @@ impl ConnectOptions {
if let Some(idle_timeout) = self.idle_timeout {
opt = opt.idle_timeout(Some(idle_timeout));
}
if let Some(acquire_timeout) = self.acquire_timeout {
opt = opt.acquire_timeout(acquire_timeout);
}
if let Some(max_lifetime) = self.max_lifetime {
opt = opt.max_lifetime(Some(max_lifetime));
}
@ -192,6 +198,17 @@ impl ConnectOptions {
self.idle_timeout
}
/// Set the maximum amount of time to spend waiting for acquiring a connection
pub fn acquire_timeout(&mut self, value: Duration) -> &mut Self {
self.acquire_timeout = Some(value);
self
}
/// Get the maximum amount of time to spend waiting for acquiring a connection
pub fn get_acquire_timeout(&self) -> Option<Duration> {
self.acquire_timeout
}
/// Set the maximum lifetime of individual connections
pub fn max_lifetime(&mut self, lifetime: Duration) -> &mut Self {
self.max_lifetime = Some(lifetime);