Merge pull request #493 from SeaQL/max-lifetime-connection-opt

Add `max_lifetime` connection option
This commit is contained in:
Chris Tsang 2022-02-05 20:50:39 +08:00 committed by GitHub
commit e305c6669c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,6 +37,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 lifetime of individual connections
pub(crate) max_lifetime: Option<Duration>,
/// Enable SQLx statement logging
pub(crate) sqlx_logging: bool,
}
@ -100,6 +102,7 @@ impl ConnectOptions {
min_connections: None,
connect_timeout: None,
idle_timeout: None,
max_lifetime: None,
sqlx_logging: true,
}
}
@ -127,6 +130,9 @@ impl ConnectOptions {
if let Some(idle_timeout) = self.idle_timeout {
opt = opt.idle_timeout(Some(idle_timeout));
}
if let Some(max_lifetime) = self.max_lifetime {
opt = opt.max_lifetime(Some(max_lifetime));
}
opt
}
@ -179,6 +185,17 @@ impl ConnectOptions {
self.idle_timeout
}
/// Set the maximum lifetime of individual connections
pub fn max_lifetime(&mut self, lifetime: Duration) -> &mut Self {
self.max_lifetime = Some(lifetime);
self
}
/// Get the maximum lifetime of individual connections, if set
pub fn get_max_lifetime(&self) -> Option<Duration> {
self.max_lifetime
}
/// Enable SQLx statement logging (default true)
pub fn sqlx_logging(&mut self, value: bool) -> &mut Self {
self.sqlx_logging = value;