Skip to main content

Pool

Trait Pool 

Source
pub trait Pool: Send + Sync {
    type Resource: Send;
    type Error: Error + Send + Sync + 'static;

    // Required methods
    fn acquire<'a>(
        &'a self,
        cx: &'a Cx,
    ) -> PoolFuture<'a, Result<PooledResource<Self::Resource>, Self::Error>>;
    fn try_acquire(&self) -> Option<PooledResource<Self::Resource>>;
    fn stats(&self) -> PoolStats;
    fn close(&self) -> PoolFuture<'_, ()>;

    // Provided method
    fn health_check<'a>(
        &'a self,
        _resource: &'a Self::Resource,
    ) -> PoolFuture<'a, bool> { ... }
}
Expand description

Trait for resource pools with cancel-safe acquisition.

Required Associated Types§

Source

type Resource: Send

The type of resource managed by this pool.

Source

type Error: Error + Send + Sync + 'static

Error type for acquisition failures.

Required Methods§

Source

fn acquire<'a>( &'a self, cx: &'a Cx, ) -> PoolFuture<'a, Result<PooledResource<Self::Resource>, Self::Error>>

Acquire a resource from the pool.

This may block if no resources are available and the pool is at capacity. The acquire respects the Cx deadline.

§Cancel-Safety
  • Cancelled while waiting: no resource is leaked.
  • Cancelled after acquisition: the PooledResource returns on drop.
Source

fn try_acquire(&self) -> Option<PooledResource<Self::Resource>>

Try to acquire without waiting.

Returns None if no resource is immediately available.

Source

fn stats(&self) -> PoolStats

Get current pool statistics.

Source

fn close(&self) -> PoolFuture<'_, ()>

Close the pool, rejecting new acquisitions.

Provided Methods§

Source

fn health_check<'a>( &'a self, _resource: &'a Self::Resource, ) -> PoolFuture<'a, bool>

Check if a resource is still healthy/usable.

Called before returning an idle resource from the pool. If this returns false, the resource is discarded and another is tried (or a new one is created).

The default implementation assumes all resources are healthy.

§Example
async fn health_check(&self, resource: &TcpStream) -> bool {
    // Try a quick ping
    resource.peer_addr().is_ok()
}

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<R, F> Pool for GenericPool<R, F>
where R: Send + 'static, F: AsyncResourceFactory<Resource = R>,