resource
Resource management patterns
This module provides patterns for safe resource acquisition, management, and release. Includes resource pools, connection management, and other resource patterns common in distributed systems and agent architectures.
Key Features
Section titled “Key Features”- Resource Pools: Thread-safe resource pooling with automatic cleanup
- Resource Guards: RAII-based resource acquisition and release
- Lease Management: Time-based and usage-based resource leasing
- Health Checking: Automatic resource health validation
- Graceful Degradation: Fallback patterns when resources are unavailable
Traits
Section titled “Traits”ManagedResource
Section titled “ManagedResource”Trait for types that can be managed as resources
Associated Types
type Error—
Required / Provided Methods
fn create() -> Result<Self, <Self as ?>::Error>Create a new instance of this resource
fn is_healthy(&self) -> boolCheck if this resource is healthy
fn cleanup(self) -> Result<(), <Self as ?>::Error>Clean up this resource (called when resource is dropped)
fn managed(self) -> ResourceGuard<Self>Create a managed resource guard
Structs
Section titled “Structs”ResourceGuard
Section titled “ResourceGuard”Resource guard that manages the lifetime of a resource
This guard ensures that resources are properly returned to their pool or cleaned up when they’re no longer needed.
Methods
fn new<F>(resource: T, return_fn: F) -> Selfwhere F: FnOnce + Send + ?Create a new resource guard
Arguments
Section titled “Arguments”resource: The resource to managereturn_fn: Function to call when returning the resource
resource
Section titled “resource”fn resource(&self) -> &TGet a reference to the managed resource
resource_mut
Section titled “resource_mut”fn resource_mut(&mut self) -> &mut TGet a mutable reference to the managed resource
return_resource
Section titled “return_resource”fn return_resource(self) -> Result<(), Box<dyn Error + Send + Sync>>Manually return the resource (consumes the guard)
take_ownership
Section titled “take_ownership”fn take_ownership(self) -> TTake ownership of the resource without returning it to the pool
PoolConfig
Section titled “PoolConfig”Configuration for resource pools
Fields
| Field | Type | Description |
|---|---|---|
max_size | usize | Maximum number of resources in the pool |
min_size | usize | Minimum number of resources to keep in the pool |
acquire_timeout | Duration | Maximum time to wait for a resource to become available |
max_idle_time | Duration | Maximum idle time before a resource is considered expired |
health_check_interval | Duration | Interval for health checking idle resources |
ResourcePool
Section titled “ResourcePool”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.
Methods
fn new<F>(config: PoolConfig, factory: F) -> Result<Self, Box<dyn Error + Send + Sync>>where F: Fn + Send + Sync + ?Create a new resource pool
Arguments
Section titled “Arguments”config: Pool configurationfactory: Function to create new resources
Example
Section titled “Example”use foundation_utils::resource::{ResourcePool, PoolConfig};
let pool = ResourcePool::new( PoolConfig::default(), || Ok("new_resource".to_string())).unwrap();with_health_checker
Section titled “with_health_checker”fn with_health_checker<F>(self, health_checker: F) -> Selfwhere F: Fn + Send + Sync + ?Set a health checker function for resources
The health checker is called periodically to validate that pooled resources are still healthy.
acquire
Section titled “acquire”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
Section titled “Returns”A ResourceGuard that automatically returns the resource to the pool
when dropped.
fn stats(&self) -> PoolStatsGet pool statistics
health_check
Section titled “health_check”fn health_check(&self) -> Result<usize, Box<dyn Error + Send + Sync>>Perform health check on all idle resources
PoolStats
Section titled “PoolStats”Statistics about a resource pool
Fields
| Field | Type | Description |
|---|---|---|
total_resources | usize | Total number of resources (active + available) |
available_resources | usize | Number of available resources in the pool |
active_resources | usize | Number of actively used resources |
max_size | usize | Maximum pool size |
min_size | usize | Minimum pool size |
Methods
utilization
Section titled “utilization”fn utilization(&self) -> f64Get the pool utilization as a percentage (0.0 to 1.0)
is_at_capacity
Section titled “is_at_capacity”fn is_at_capacity(&self) -> boolCheck if the pool is at capacity
is_below_minimum
Section titled “is_below_minimum”fn is_below_minimum(&self) -> boolCheck if the pool is below minimum size
Functions
Section titled “Functions”managed_resource
Section titled “managed_resource”fn managed_resource<T, F>(resource: T, cleanup: F) -> ResourceGuard<T>where F: FnOnce + Send + ?Convenience function to create a simple resource guard
This function creates a guard that will call the cleanup function when the resource is no longer needed.