Rust

pool

Module

Pool and the Connections it lends.

Contents

use utopia_pools::Connection;
Rust
pub struct Connection<T>(Arc<Lent<T>>)

A resource checked out of a pool: an id unique in the process (<pool name>-<uniqid>), the resource, and a weak link to its pool.

Clones are the same connection. Handing it back to a pool that no longer exists does nothing, and the connection keeps its resource.

Also implements Clone, Debug, Deref.
use utopia_pools::Pool;
Rust
pub struct Pool<T, A>

A fixed-capacity pool of resources created on demand.

Pool::pop hands out an idle connection, creates one while fewer than size exist, or waits up to timeout for one to come back. A failed creation fails the pop with init's own error, and frees its slot: retry policy belongs in init. Hand connections back with Pool::reclaim (reuse) or Pool::destroy (discard), or run work through Pool::with, which does it for you.

Clones share one pool.

Rust
use utopia_pools::{Pool, Stack};

let pool = Pool::new(Stack::new(), "cache", 2, || async { Ok::<_, std::io::Error>("x".to_owned()) }, 0.0)?;
assert_eq!(pool.count(), 2);

let connection = pool.pop().await?;
assert_eq!(connection.resource(), "x");
assert_eq!(pool.count(), 1);

pool.reclaim(&connection);
assert!(pool.is_full());
Also implements Clone, Debug.