Skip to main content

ResourcePool

Struct ResourcePool 

Source
pub struct ResourcePool<T> { /* private fields */ }
Expand description

A resource pool that manages the lifecycle of expensive resources

This pool provides thread-safe access to resources with automatic cleanup, health checking, and graceful degradation.

Implementations§

Source§

impl<T> ResourcePool<T>
where T: Send + 'static,

Source

pub fn new<F>( config: PoolConfig, factory: F, ) -> Result<Self, Box<dyn Error + Send + Sync>>
where F: Fn() -> Result<T, Box<dyn Error + Send + Sync>> + Send + Sync + 'static,

Create a new resource pool

§Arguments
  • config: Pool configuration
  • factory: Function to create new resources
§Example
use foundation_utils::resource::{ResourcePool, PoolConfig};

let pool = ResourcePool::new(
    PoolConfig::default(),
    || Ok("new_resource".to_string())
).unwrap();
Source

pub fn with_health_checker<F>(self, health_checker: F) -> Self
where F: Fn(&T) -> bool + Send + Sync + 'static,

Set a health checker function for resources

The health checker is called periodically to validate that pooled resources are still healthy.

Source

pub fn acquire(&self) -> Result<ResourceGuard<T>, Box<dyn Error + Send + Sync>>

Acquire a resource from the pool

This method will either return an existing resource from the pool or create a new one if the pool is not at capacity.

§Returns

A ResourceGuard that automatically returns the resource to the pool when dropped.

Source

pub fn stats(&self) -> PoolStats

Get pool statistics

Source

pub fn health_check(&self) -> Result<usize, Box<dyn Error + Send + Sync>>

Perform health check on all idle resources

Auto Trait Implementations§

§

impl<T> Freeze for ResourcePool<T>

§

impl<T> !RefUnwindSafe for ResourcePool<T>

§

impl<T> Send for ResourcePool<T>
where T: Send,

§

impl<T> Sync for ResourcePool<T>
where T: Send,

§

impl<T> Unpin for ResourcePool<T>

§

impl<T> UnsafeUnpin for ResourcePool<T>

§

impl<T> !UnwindSafe for ResourcePool<T>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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> IntoScoped for T

Source§

type Resource = T

Source§

fn into_scoped(self) -> <T as IntoScoped>::Resource

Convert into a scoped resource
Source§

fn scoped<F, R>(self, f: F) -> R
where Self: Sized, F: FnOnce(&Self::Resource) -> R,

Execute a scoped operation with this resource
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.