Skip to main content

ConnectionPool

Struct ConnectionPool 

Source
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>
where T: Clone + Eq + Hash + Send + Sync + 'static,

Source

pub fn new( factory: impl ConnectionFactory<T> + 'static, config: PoolConfig, options: Option<PoolOptions<T>>, ) -> Result<Self, PoolError>

Builds a pool, validating config.

Source

pub fn name(&self) -> &str

The pool’s name.

Source

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.

Source

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.

Source

pub fn is_closed(&self) -> bool

Whether close has been called.

Source

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.

Source

pub fn release(&self, conn: T) -> Result<(), PoolError>

Returns a connection to the pool for reuse.

Source

pub fn discard(&self, conn: T) -> Result<(), PoolError>

Returns a connection but forces it to be closed instead of reused.

Source

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.

Source

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.

Source

pub fn stats(&self) -> PoolStats

An immutable snapshot of pool counters and gauges.

Source

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.

Trait Implementations§

Source§

impl<T> Clone for ConnectionPool<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for ConnectionPool<T>

§

impl<T> !UnwindSafe for ConnectionPool<T>

§

impl<T> Freeze for ConnectionPool<T>
where Arc<Inner<T>>: Freeze,

§

impl<T> Send for ConnectionPool<T>
where Arc<Inner<T>>: Send,

§

impl<T> Sync for ConnectionPool<T>
where Arc<Inner<T>>: Sync,

§

impl<T> Unpin for ConnectionPool<T>
where Arc<Inner<T>>: Unpin,

§

impl<T> UnsafeUnpin for ConnectionPool<T>
where Arc<Inner<T>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.