pub struct Pool<C: Connection> { /* private fields */ }Expand description
A connection pool for database connections.
The pool manages a collection of connections, reusing them across requests to avoid the overhead of establishing new connections.
§Type Parameters
C: The connection type, must implementConnection
§Cancellation
Pool operations respect cancellation via the Cx context:
acquirewill return early if cancellation is requested- Connections are properly cleaned up on cancellation
Implementations§
Source§impl<C: Connection> Pool<C>
impl<C: Connection> Pool<C>
Sourcepub fn new(config: PoolConfig) -> Self
pub fn new(config: PoolConfig) -> Self
Create a new connection pool with the given configuration.
Sourcepub fn config(&self) -> PoolConfig
pub fn config(&self) -> PoolConfig
Get the pool configuration.
Sourcepub fn at_capacity(&self) -> bool
pub fn at_capacity(&self) -> bool
Check if the pool is at capacity.
Sourcepub async fn acquire<F, Fut>(
&self,
cx: &Cx,
factory: F,
) -> Outcome<PooledConnection<C>, Error>
pub async fn acquire<F, Fut>( &self, cx: &Cx, factory: F, ) -> Outcome<PooledConnection<C>, Error>
Acquire a connection from the pool.
This method will:
- Return an idle connection if one is available
- Create a new connection if below capacity
- Wait for a connection to become available (up to timeout)
§Errors
Returns an error if:
- The pool is closed
- The acquire timeout is exceeded
- Cancellation is requested via the
Cxcontext - Connection validation fails (if
test_on_checkoutis enabled)
Sourcepub fn clear_idle(&self)
pub fn clear_idle(&self)
Close the pool, preventing new connections and closing all idle connections.
If the pool mutex is poisoned, this logs an error but still wakes waiters.
pub fn close(&self)
Sourcepub fn idle_count(&self) -> usize
pub fn idle_count(&self) -> usize
Get the number of idle connections.
Sourcepub fn active_count(&self) -> usize
pub fn active_count(&self) -> usize
Get the number of active connections.
Sourcepub fn total_count(&self) -> usize
pub fn total_count(&self) -> usize
Get the total number of connections.
Auto Trait Implementations§
impl<C> Freeze for Pool<C>
impl<C> RefUnwindSafe for Pool<C>
impl<C> Send for Pool<C>
impl<C> Sync for Pool<C>
impl<C> Unpin for Pool<C>
impl<C> UnsafeUnpin for Pool<C>
impl<C> UnwindSafe for Pool<C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).