managed
only.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
sourceimpl<M: Manager, W: From<Object<M>>> Pool<M, W>
impl<M: Manager, W: From<Object<M>>> Pool<M, W>
sourcepub fn builder(manager: M) -> PoolBuilder<M, W>
pub fn builder(manager: M) -> PoolBuilder<M, W>
sourcepub async fn try_get(&self) -> Result<W, PoolError<M::Error>>
👎 Deprecated since 0.9.3: The name of this method is highly misleading. Please use timeout_get instead. e.g.
pool.timeout_get(&Timeouts { wait: Some(Duration::ZERO), ..pool.timeouts() })
pub async fn try_get(&self) -> Result<W, PoolError<M::Error>>
The name of this method is highly misleading. Please use timeout_get instead. e.g.
pool.timeout_get(&Timeouts { wait: Some(Duration::ZERO), ..pool.timeouts() })
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, f: impl Fn(&M::Type, Metrics) -> bool)
pub fn retain(&self, f: impl Fn(&M::Type, Metrics) -> bool)
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 Object
s will return
PoolError::Closed
immediately.
This operation resizes the pool to 0.
Trait Implementations
Auto Trait Implementations
impl<M, W = Object<M>> !RefUnwindSafe for Pool<M, W>
impl<M, W> Send for Pool<M, W> where
<M as Manager>::Type: Send,
impl<M, W> Sync for Pool<M, W> where
<M as Manager>::Type: Send,
impl<M, W> Unpin for Pool<M, W>
impl<M, W = Object<M>> !UnwindSafe for Pool<M, W>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more