Skip to main content

Crate pool_mod

Crate pool_mod 

Source
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 through Pool::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 on std threading, timing, and synchronization primitives. With default-features = false the crate is no_std and exposes only VERSION.

§License

Dual-licensed under Apache-2.0 OR MIT.

Modules§

preludestd
Convenient re-exports: use pool_mod::prelude::*;.

Structs§

Builderstd
A fluent builder for a Pool.
Poolstd
A thread-safe pool of reusable resources.
PoolConfigstd
Tunable limits and lifecycle policy for a Pool.
Pooledstd
A borrowed resource that returns itself to the pool when dropped.
Statusstd
A snapshot of how many resources a Pool holds.

Enums§

Errorstd
An error returned by a Pool operation.

Constants§

VERSION
Crate version string, populated by Cargo at build time.

Traits§

Managerstd
Creates and maintains the resources held by a Pool.