Skip to main content

Crate esox_objectpool

Crate esox_objectpool 

Source
Expand description

§EsoxSolutions.ObjectPool (Rust Port)

High-performance, thread-safe object pool for Rust with async support, metrics, health monitoring, and advanced features.

§Features

  • Thread-safe object pooling with lock-free operations
  • Automatic return of objects via RAII (Drop trait)
  • Async support with timeout and jittered retry
  • Queryable pools for finding objects matching predicates
  • Dynamic pools with factory methods
  • Health monitoring and metrics (including Prometheus export)
  • Pool warm-up/pre-population
  • Eviction/TTL support
  • Circuit breaker pattern
  • #[must_use] on all observability methods

§Quick Start

use esox_objectpool::ObjectPool;

let pool = ObjectPool::new(vec![1, 2, 3], Default::default());
{
    let obj = pool.get_object().unwrap();
    println!("Got: {}", *obj);
    // Object automatically returned when `obj` goes out of scope
}
println!("capacity={} available={}", pool.capacity(), pool.available_count());

§Working with PooledObject<T>

PooledObject<T> is a smart pointer that returns its contents to the pool when dropped. It implements Deref, DerefMut, AsRef<T>, and AsMut<T> so it works transparently wherever &T or &mut T are expected.

Three explicit accessor methods are available:

MethodReturnsPool effect
get()&TNone — returned on drop
get_mut()&mut TNone — returned on drop
into_detached()T (owned)Permanently removed
use esox_objectpool::ObjectPool;

let pool = ObjectPool::new(vec![0], Default::default());
let mut obj = pool.get_object().unwrap();

*obj.get_mut() = 99;           // mutate — stays in pool
assert_eq!(*obj.get(), 99);    // borrow — stays in pool
drop(obj);                     // returned to pool as normal

// Permanently take ownership (reduces pool capacity by 1):
let obj2 = pool.get_object().unwrap();
let value = obj2.into_detached();
assert_eq!(value, 99);

Note: unwrap() on PooledObject is deprecated since 1.1.0. Use into_detached() instead.

§Observing Pool State

All three pool types expose the same set of observability methods:

use esox_objectpool::{ObjectPool, PoolConfiguration};

let pool = ObjectPool::new(vec![1, 2, 3],
    PoolConfiguration::new().with_max_pool_size(10));

println!("capacity={}", pool.capacity());          // 10
println!("available={}", pool.available_count());  // 3
println!("active={}", pool.active_count());        // 0

let metrics = pool.get_metrics();
println!("retrieved={}", metrics.total_retrieved);

let health = pool.get_health_status();
println!("healthy={} utilization={:.0}%",
    health.is_healthy, health.utilization * 100.0);

§API Changes in 1.1.1

  • ObjectPool and PooledObject now implement AsRef<T> and AsMut<T>.
  • ObjectPool::capacity() method added.
  • Jitter support in async operations.
  • All observability methods are annotated with #[must_use].
  • unwrap() on PooledObject is deprecated, use into_detached() instead.

Structs§

CircuitBreaker
Circuit breaker for protecting against cascading failures
DynamicObjectPool
Dynamic object pool - creates objects on demand
HealthStatus
Health status of an object pool
MetricsExporter
Metrics exporter for Prometheus format
ObjectPool
Thread-safe object pool with fixed set of objects
PoolConfiguration
Configuration for object pool behavior
PoolMetrics
Metrics data for a pool
PooledObject
A pooled object that automatically returns to the pool when dropped
QueryableObjectPool
Queryable object pool - find objects matching a predicate

Enums§

CircuitBreakerState
Circuit breaker state
EvictionPolicy
Eviction policy for pool objects
PoolError

Type Aliases§

PoolResult