Struct deadpool::managed::Pool

source ·
pub struct Pool<M: Manager, W: From<Object<M>> = 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: Manager, W: From<Object<M>>> Pool<M, W>

source

pub fn builder(manager: M) -> PoolBuilder<M, W>

Instantiates a builder for a new Pool.

This is the only way to create a Pool instance.

source

pub async fn get(&self) -> Result<W, PoolError<M::Error>>

Retrieves an Object from this Pool or waits for one to become available.

Errors

See PoolError for details.

source

pub 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() })

Retrieves an Object from this Pool and doesn’t wait if there is currently no Object available and the maximum Pool size has been reached.

Errors

See PoolError for details.

source

pub async fn timeout_get( &self, timeouts: &Timeouts ) -> Result<W, PoolError<M::Error>>

Retrieves an Object from this Pool using a different timeout than the configured one.

Errors

See PoolError for details.

source

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.

source

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);
    }
});
source

pub fn timeouts(&self) -> Timeouts

Get current timeout configuration

source

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.

source

pub fn is_closed(&self) -> bool

Indicates whether this Pool has been closed.

source

pub fn status(&self) -> Status

Retrieves Status of this Pool.

source

pub fn manager(&self) -> &M

Returns Manager of this Pool.

Trait Implementations§

source§

impl<M: Manager, W: From<Object<M>>> Clone for Pool<M, W>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<M, W> Debug for Pool<M, W>where M: Debug + Manager, M::Type: Debug, W: From<Object<M>>,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.