pub struct ConnectionPool<T>(/* private fields */);Expand description
A thread-safe, blocking connection pool. Cheaply clonable (clones share one pool).
Implementations§
Source§impl<T> ConnectionPool<T>
impl<T> ConnectionPool<T>
Sourcepub fn new(
factory: impl ConnectionFactory<T> + 'static,
config: PoolConfig,
options: Option<PoolOptions<T>>,
) -> Result<Self, PoolError>
pub fn new( factory: impl ConnectionFactory<T> + 'static, config: PoolConfig, options: Option<PoolOptions<T>>, ) -> Result<Self, PoolError>
Builds a pool, validating config.
Sourcepub fn open(&self) -> Result<(), PoolError>
pub fn open(&self) -> Result<(), PoolError>
Warms up min_size connections and starts the background maintenance thread. Optional:
without it the pool is purely lazy. Idempotent.
Sourcepub fn close(&self, timeout: Option<f64>)
pub fn close(&self, timeout: Option<f64>)
Closes idle connections now; checked-out ones close when released. Blocked acquire
callers get a PoolError::Closed. Idempotent.
timeout (None = don’t wait) drains gracefully: wait up to that many seconds for
in-use connections to return before giving up (they still close on release). 0 returns
at once.
Sourcepub fn acquire(&self, timeout: Timeout) -> Result<T, PoolError>
pub fn acquire(&self, timeout: Timeout) -> Result<T, PoolError>
Gets a connection. Timeout::Unset uses config.acquire_timeout; Timeout::Infinite
waits forever; Timeout::Of(0.0) never waits.
Sourcepub fn release(&self, conn: T) -> Result<(), PoolError>
pub fn release(&self, conn: T) -> Result<(), PoolError>
Returns a connection to the pool for reuse.
Sourcepub fn discard(&self, conn: T) -> Result<(), PoolError>
pub fn discard(&self, conn: T) -> Result<(), PoolError>
Returns a connection but forces it to be closed instead of reused.
Sourcepub fn release_with_error(
&self,
conn: T,
error: &(dyn Error + 'static),
) -> Result<(), PoolError>
pub fn release_with_error( &self, conn: T, error: &(dyn Error + 'static), ) -> Result<(), PoolError>
Returns a connection, signalling that the caller’s work raised error; the pool
discards the connection if the factory deems the error fatal or validation fails.
Sourcepub fn with_connection<R>(
&self,
timeout: Timeout,
f: impl FnOnce(&T) -> R,
) -> Result<R, PoolError>
pub fn with_connection<R>( &self, timeout: Timeout, f: impl FnOnce(&T) -> R, ) -> Result<R, PoolError>
Borrows a connection for the duration of f, releasing it afterwards.
Sourcepub fn run_maintenance(&self)
pub fn run_maintenance(&self)
Performs one maintenance sweep (evict expired/over-idle, optionally validate idle, refill
to min_size, flag leaks). Runs periodically after open; public for tests
and cron-style use.