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§
Required Methods§
Sourcefn acquire<'a>(
&'a self,
cx: &'a Cx,
) -> PoolFuture<'a, Result<PooledResource<Self::Resource>, Self::Error>>
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
PooledResourcereturns on drop.
Sourcefn try_acquire(&self) -> Option<PooledResource<Self::Resource>>
fn try_acquire(&self) -> Option<PooledResource<Self::Resource>>
Try to acquire without waiting.
Returns None if no resource is immediately available.
Sourcefn close(&self) -> PoolFuture<'_, ()>
fn close(&self) -> PoolFuture<'_, ()>
Close the pool, rejecting new acquisitions.
Provided Methods§
Sourcefn health_check<'a>(
&'a self,
_resource: &'a Self::Resource,
) -> PoolFuture<'a, bool>
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".