Expand description
§pool-mod
Generic object and connection pooling for Rust.
pool-mod lends out reusable resources — database connections, HTTP clients,
worker handles, or anything else expensive to construct — and reclaims each
one automatically when the borrow ends. You describe the resource’s lifecycle
by implementing Manager (create, validate, recycle); the pool handles
sizing, blocking acquisition with timeouts, validation-on-borrow, and
idle/lifetime expiry.
The pool is runtime-agnostic: it carries no async-runtime dependency.
Pool::get blocks the calling thread until a resource is free. In an async
context, acquire on a blocking-friendly executor thread (for example
tokio::task::spawn_blocking); the returned Pooled guard is Send, so it
can be held across .await points.
§Overview
Manager— the trait you implement to create, validate, and recycle resources.Pool— the thread-safe, cheaply-cloneable pool handle.Builder— fluent configuration, reached throughPool::builder.PoolConfig— the underlying limits and lifecycle policy.Pooled— the RAII guard that returns its resource to the pool on drop.Status— a snapshot of pool occupancy.Error— the error type, generic over the manager’s own error.
§Example
use pool_mod::{Manager, Pool};
use std::convert::Infallible;
// Describe how to make, reset, and (optionally) validate the resource.
struct Widgets;
impl Manager for Widgets {
type Resource = String;
type Error = Infallible;
fn create(&self) -> Result<String, Infallible> {
Ok(String::with_capacity(1024))
}
fn recycle(&self, buf: &mut String) -> Result<(), Infallible> {
buf.clear(); // reuse the allocation, discard the contents
Ok(())
}
}
// A pool of at most eight widgets, two kept ready at all times.
let pool = Pool::builder(Widgets)
.max_size(8)
.min_idle(2)
.build()
.expect("configuration is valid");
// Borrow one; it returns to the pool when `widget` is dropped.
let mut widget = pool.get().expect("a widget is available");
widget.push_str("hello");
assert_eq!(widget.len(), 5);§Feature flags
std(default) — enables the pool. The pool relies onstdthreading, timing, and synchronization primitives. Withdefault-features = falsethe crate isno_stdand exposes onlyVERSION.
§License
Dual-licensed under Apache-2.0 OR MIT.
Modules§
- prelude
std - Convenient re-exports:
use pool_mod::prelude::*;.
Structs§
- Builder
std - A fluent builder for a
Pool. - Pool
std - A thread-safe pool of reusable resources.
- Pool
Config std - Tunable limits and lifecycle policy for a
Pool. - Pooled
std - A borrowed resource that returns itself to the pool when dropped.
- Status
std - A snapshot of how many resources a
Poolholds.
Enums§
Constants§
- VERSION
- Crate version string, populated by Cargo at build time.