diff --git a/src/database/mod.rs b/src/database/mod.rs index 1a9556c7..ea00f742 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -37,6 +37,8 @@ pub struct ConnectOptions { /// Maximum idle time for a particular connection to prevent /// network resource exhaustion pub(crate) idle_timeout: Option, + /// Set the maximum lifetime of individual connections + pub(crate) max_lifetime: Option, /// 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 { + self.max_lifetime + } + /// Enable SQLx statement logging (default true) pub fn sqlx_logging(&mut self, value: bool) -> &mut Self { self.sqlx_logging = value;