pub struct PoolConfig {
pub max_connections: usize,
pub min_connections: usize,
pub connect_timeout: Duration,
pub idle_timeout: Option<Duration>,
pub max_lifetime: Option<Duration>,
pub health_check: HealthCheckStrategy,
pub acquire_timeout: Duration,
pub after_connect: Option<ConnectCallback>,
pub before_acquire: Option<AcquireCallback>,
pub after_release: Option<ReleaseCallback>,
}Expand description
Configuration for the connection pool.
Supports lifecycle callbacks for connection setup, validation, and cleanup.
All callbacks are optional and default to None.
Fields§
§max_connections: usize§min_connections: usize§connect_timeout: Duration§idle_timeout: Option<Duration>§max_lifetime: Option<Duration>§health_check: HealthCheckStrategy§acquire_timeout: Duration§after_connect: Option<ConnectCallback>§before_acquire: Option<AcquireCallback>§after_release: Option<ReleaseCallback>Implementations§
Source§impl PoolConfig
impl PoolConfig
pub fn new() -> Self
Sourcepub fn max_connections(self, n: usize) -> Self
pub fn max_connections(self, n: usize) -> Self
Maximum number of connections in the pool.
Default: 2 * number of CPUs.
Sourcepub fn min_connections(self, n: usize) -> Self
pub fn min_connections(self, n: usize) -> Self
Minimum number of idle connections to maintain.
The pool will create connections in the background to maintain this minimum. Default: 1.
Sourcepub fn connect_timeout(self, timeout: Duration) -> Self
pub fn connect_timeout(self, timeout: Duration) -> Self
Timeout for establishing new connections.
Default: 10 seconds.
Sourcepub fn idle_timeout(self, timeout: Option<Duration>) -> Self
pub fn idle_timeout(self, timeout: Option<Duration>) -> Self
Maximum time a connection can sit idle before being closed.
Set to None to disable idle timeout. Default: 600 seconds.
Sourcepub fn max_lifetime(self, lifetime: Option<Duration>) -> Self
pub fn max_lifetime(self, lifetime: Option<Duration>) -> Self
Maximum total lifetime of a connection before it’s recycled.
Set to None to disable max lifetime. Default: 3600 seconds.
Sourcepub fn health_check(self, strategy: HealthCheckStrategy) -> Self
pub fn health_check(self, strategy: HealthCheckStrategy) -> Self
Strategy for checking connection health on checkout.
Default: Fast (flag-based, no query).
Sourcepub fn acquire_timeout(self, timeout: Duration) -> Self
pub fn acquire_timeout(self, timeout: Duration) -> Self
Timeout for acquiring a connection from the pool.
If the pool is full and no connection becomes available within this duration, an error is returned. Default: 30 seconds.
Sourcepub fn after_connect<F>(self, callback: F) -> Self
pub fn after_connect<F>(self, callback: F) -> Self
Set a callback that runs once per newly created connection.
Called after TCP + TLS + auth completes, before the connection enters
the pool. Use for session setup like SET search_path.
If the callback returns an error, the connection is discarded and the pool retries with a new connection.
§Example
PoolConfig::new()
.after_connect(|conn| Box::pin(async move {
conn.execute("SET search_path TO myapp", &[]).await?;
Ok(())
}));Sourcepub fn before_acquire<F>(self, callback: F) -> Self
pub fn before_acquire<F>(self, callback: F) -> Self
Set a callback that runs before returning a connection from the pool.
Called after health check passes. Return false to reject the
connection — it will be discarded and the pool tries the next idle
connection or creates a new one.
§Example
PoolConfig::new()
.before_acquire(|conn| Box::pin(async move {
Ok(!conn.is_broken())
}));Sourcepub fn after_release<F>(self, callback: F) -> Self
pub fn after_release<F>(self, callback: F) -> Self
Set a callback that runs when a connection returns to the pool.
Called before the connection enters the idle queue. Return false
to discard the connection instead of returning it.
§Example
PoolConfig::new()
.after_release(|conn| Box::pin(async move {
Ok(true) // always return to pool
}));Trait Implementations§
Source§impl Clone for PoolConfig
impl Clone for PoolConfig
Source§fn clone(&self) -> PoolConfig
fn clone(&self) -> PoolConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more