Pools
Rust 1,057 lines (583 code)split
| Part | Code | Comments | Blank | Total | Files |
|---|---|---|---|---|---|
| Rust source | 583 | 373 | 101 | 1,057 | 4 |
| Rust tests | 80 | 1 | 6 | 87 | 1 |
Comments include doc comments. Rust tests are tests/ and #[cfg(test)] modules.
Install#
The crate lives in the Appwrite repository; depend on it from Git.
[dependencies]
utopia-pools = { git = "https://github.com/appwrite/appwrite" }Getting started#
Common tasks with Pools, each a complete program you can copy and run.
Opening a connection to a database or a cache costs a round trip or more, so a service keeps a few open and reuses them. A pool does the bookkeeping: it creates connections on demand up to a fixed size, lends them one caller at a time, takes them back, and makes callers wait when all are lent. The pool is async, so the examples run it on a Tokio runtime. This guide covers the common tasks.
Run work with a pooled connection
with borrows a connection, runs the work and returns the connection, also when the work fails. The pool creates connections with the init function you give it, the first time it needs each one.
use utopia_pools::{Pool, Stack};
let pool = Pool::new(Stack::new(), "db", 4, || async { Ok::<_, std::io::Error>("connection".to_owned()) }, 1.0)?;
let length = tokio::runtime::Builder::new_current_thread().enable_all().build()?.block_on(async {
pool.with(async |connection: &String| Ok::<_, utopia_pools::Error>(connection.len())).await
})?;
assert_eq!(length, 10);Borrow and return by hand
pop lends a connection until you hand it back: reclaim it to reuse it, or destroy it to discard it and free its place for a new one.
use utopia_pools::{Pool, Stack};
let pool = Pool::new(Stack::new(), "db", 2, || async { Ok::<_, std::io::Error>("connection".to_owned()) }, 0.0)?;
tokio::runtime::Builder::new_current_thread().enable_all().build()?.block_on(async {
let connection = pool.pop().await?;
assert_eq!(pool.count(), 1);
pool.reclaim(&connection);
let broken = pool.pop().await?;
pool.destroy(&broken);
assert!(pool.is_full());
Ok::<_, utopia_pools::Error>(())
})?;Wait for a connection
With the Channel adapter, a caller that finds every connection lent waits up to the pool's timeout for one to come back. When none does, pop fails.
use utopia_pools::{Channel, Pool};
let pool = Pool::new(Channel::new(), "db", 1, || async { Ok::<_, std::io::Error>("connection".to_owned()) }, 0.05)?;
tokio::runtime::Builder::new_current_thread().enable_all().build()?.block_on(async {
let held = pool.pop().await?;
let error = pool.pop().await.unwrap_err();
assert_eq!(error.to_string(), "Pool 'db' could not provide a connection within 0.05s (size 1, active 1, idle 0)");
pool.reclaim(&held);
Ok::<_, utopia_pools::Error>(())
})?;Recover and keep connections alive
What happens to a connection after failed work, and while it sits idle, is up to the resource: implement Resource. recover decides whether it can be reused after a failure (otherwise it is discarded), and tick runs its upkeep when the host calls maintain.
use std::sync::atomic::{AtomicUsize, Ordering};
use utopia_pools::{BoxError, Pool, Resource, Stack};
struct Connection {
pings: AtomicUsize,
}
impl Resource for Connection {
async fn recover(&self) -> bool {
true
}
async fn tick(&self) -> Result<(), BoxError> {
self.pings.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}
let pool = Pool::new(Stack::new(), "nats", 1, || async { Ok::<_, std::io::Error>(Connection { pings: AtomicUsize::new(0) }) }, 0.0)?;
tokio::runtime::Builder::new_current_thread().enable_all().build()?.block_on(async {
let connection = pool.pop().await?;
pool.reclaim(&connection);
pool.maintain().await;
assert_eq!(connection.resource().pings.load(Ordering::SeqCst), 1);
Ok::<_, utopia_pools::Error>(())
})?;Use several pools together
A Group keeps pools by name, to run work with a connection from each or to reclaim them all at the end of a request.
use utopia_pools::{Group, Pool, Stack};
let make = |name: &str, value: &'static str| Pool::new(Stack::new(), name, 1, move || async move { Ok::<_, std::io::Error>(value) }, 0.0);
let mut group = Group::new();
group.add(make("db", "postgres")?).add(make("cache", "redis")?);
let both = tokio::runtime::Builder::new_current_thread().enable_all().build()?.block_on(async {
group.with(&["db", "cache"], async |resources| Ok::<_, utopia_pools::Error>(resources.iter().map(|r| **r).collect::<Vec<_>>().join("+"))).await
})?;
assert_eq!(both, "postgres+redis");Reference#
Pools of resources that are costly to create and safe to reuse, such as database or cache connections.
A Pool creates resources on demand up to a fixed size, lends them as
Connections, and takes them back for reuse or discards them. Callers
that find every resource lent wait up to the pool's timeout. Idle
connections live in an [Adapter]: a Stack that never waits, or a
Channel whose callers wait for a connection to come back. A
Group keeps pools by name. Pools report wait and use times and their
connection counts to a telemetry backend.
What a pool does with a resource after failed work, and the upkeep of
idle resources, is up to the resource: implement Resource.
use utopia_pools::{Channel, Pool};
let pool = Pool::new(Channel::new(), "db", 4, || async { Ok::<_, std::io::Error>("connection".to_owned()) }, 1.0)?;
let length = pool.with(async |connection: &String| Ok::<_, utopia_pools::Error>(connection.len())).await?;
assert_eq!(length, 10);
assert!(pool.is_full());Modules#
Each module groups one concept of the library. Internal ones hold items the crate re-exports at its root.
API#
What utopia_pools exports at its root.