pub struct Pool<M, W = Object<M>>{ /* private fields */ }Expand description
Generic object and connection pool.
This struct can be cloned and transferred across thread boundaries and uses reference counting for its internal state.
Implementations§
Source§impl<M, W> Pool<M, W>
impl<M, W> Pool<M, W>
Sourcepub fn builder(manager: M) -> PoolBuilder<M, W>
pub fn builder(manager: M) -> PoolBuilder<M, W>
Sourcepub async fn timeout_get(
&self,
timeouts: &Timeouts,
) -> Result<W, PoolError<<M as Manager>::Error>>
pub async fn timeout_get( &self, timeouts: &Timeouts, ) -> Result<W, PoolError<<M as Manager>::Error>>
Sourcepub fn resize(&self, max_size: usize)
pub fn resize(&self, max_size: usize)
Resize the pool. This change the max_size of the pool dropping
excess objects and/or making space for new ones.
If the pool is closed this method does nothing. The Pool::status method
always reports a max_size of 0 for closed pools.
Sourcepub fn retain(
&self,
predicate: impl FnMut(&<M as Manager>::Type, Metrics) -> bool,
) -> RetainResult<<M as Manager>::Type>
pub fn retain( &self, predicate: impl FnMut(&<M as Manager>::Type, Metrics) -> bool, ) -> RetainResult<<M as Manager>::Type>
Retains only the objects specified by the given function.
This function is typically used to remove objects from the pool based on their current state or metrics.
Caution: This function blocks the entire pool while it is running. Therefore the given function should not block.
The following example starts a background task that runs every 30 seconds and removes objects from the pool that haven’t been used for more than one minute.
let interval = Duration::from_secs(30);
let max_age = Duration::from_secs(60);
tokio::spawn(async move {
loop {
tokio::time::sleep(interval).await;
pool.retain(|_, metrics| metrics.last_used() < max_age);
}
});Sourcepub fn close(&self)
pub fn close(&self)
Closes this Pool.
All current and future tasks waiting for Objects will return
PoolError::Closed immediately.
This operation resizes the pool to 0.
Sourcepub fn weak(&self) -> WeakPool<M>
pub fn weak(&self) -> WeakPool<M>
Returns a WeakPool<T> of this Pool.