Expand description
Connection pools for Hyper database.
This module provides two pools that share a common configuration surface:
Pool— an async pool built ondeadpool, forasync/awaitapplications. Created viacreate_pool.ConnectionPool— a synchronous, r2d2-style pool with no Tokio dependency on its hot path, for blocking applications. Created viaSyncPoolConfig::build.
Both pools open connections lazily, serialize the first-connection
database-creation handshake, recycle connections via a configurable
RecycleStrategy / SyncRecycleStrategy, and enforce optional
lifetime / idle caps.
§Async example
use hyperdb_api::pool::{create_pool, PoolConfig};
use hyperdb_api::CreateMode;
#[tokio::main]
async fn main() -> hyperdb_api::Result<()> {
// Create a pool configuration
let config = PoolConfig::new("localhost:7483", "example.hyper")
.create_mode(CreateMode::CreateIfNotExists)
.max_size(16);
// Build the pool
let pool = create_pool(config)?;
// Get a connection from the pool
let conn = pool.get().await.map_err(|e| hyperdb_api::Error::internal(e.to_string()))?;
// Use the connection
conn.execute_command("SELECT 1").await?;
// Connection is returned to pool when dropped
Ok(())
}§Sync example
use hyperdb_api::pool::SyncPoolConfig;
use hyperdb_api::CreateMode;
use std::time::Duration;
let pool = SyncPoolConfig::new("localhost:7483", "example.hyper")
.create_mode(CreateMode::CreateIfNotExists)
.max_size(8)
.wait_timeout(Some(Duration::from_secs(5)))
.build();
let conn = pool.get()?;
conn.execute_command("SELECT 1")?;
// Connection returns to the pool when `conn` drops.§Tuning knobs (shared by both pools)
- Recycle strategy (
PoolConfig::recycle/SyncPoolConfig::recycle) controls the per-checkout health probe. Defaults toSelectOne(aSELECT 1round-trip). UsePingfor the connection’s native ping,Noneto skip the probe on hot paths, orCustom(..)for a bespoke check. max_lifetimecaps how long a physical connection may live before it is retired at checkout, regardless of health.idle_timeoutretires connections that have sat idle too long (down tomin_idle, which is kept warm).- Timeouts (
wait_timeout,create_timeout,recycle_timeout) bound how long an acquire may block. The async pool enforces all three via deadpool’s Tokio runtime; the sync pool enforceswait_timeoutnatively (seeSyncPoolConfigfor the create/recycle caveat).
§Lifecycle hooks (async pool only)
PoolConfig supports two async lifecycle hooks:
after_connectruns once on every newly-opened connection (useful forSET search_path, prepared-statement warmup, etc.)before_acquireruns every time a connection is checked out (useful for session reset, telemetry, custom health checks)
use hyperdb_api::pool::{create_pool, PoolConfig, RecycleStrategy};
use hyperdb_api::CreateMode;
let config = PoolConfig::new("localhost:7483", "example.hyper")
.create_mode(CreateMode::CreateIfNotExists)
.max_size(16)
.recycle(RecycleStrategy::None) // skip the per-checkout probe
.after_connect(|conn| Box::pin(async move {
conn.execute_command("SET search_path TO public").await?;
Ok(())
}));
let _pool = create_pool(config)?;Structs§
- Connection
Manager - Connection pool manager for
AsyncConnection. - Connection
Pool - A synchronous, r2d2-style pool of blocking
Connections. - Pool
Config - Configuration for the async connection pool.
- Pool
Status - A snapshot of
ConnectionPooloccupancy. - Sync
Pool Config - Configuration and builder for the synchronous
ConnectionPool. - Sync
Pooled Connection - A connection checked out of a synchronous
ConnectionPool.
Enums§
- Recycle
Strategy - Strategy used by the async
Poolto validate a connection when it is checked back out (recycled). - Sync
Recycle Strategy - Strategy used by the synchronous
ConnectionPoolto validate a connection when it is checked out.
Functions§
- create_
pool - Creates a new connection pool from configuration.
Type Aliases§
- After
Connect Hook - A hook that runs once on every newly-opened connection (after authentication and any database-creation handshake). Use it to set session variables, install statement caches, warm prepared statements, etc.
- Before
Acquire Hook - A hook that runs every time a connection is checked out of the pool, before it is handed to the caller. Use it for per-acquire health checks, session resets, or telemetry.
- Hook
Future - Future returned by pool lifecycle hooks.
- Pool
- A pool of async connections to a Hyper database.
- Pooled
Connection - A pooled connection wrapper.
- Recycle
Check - A user-supplied async per-checkout health check for
RecycleStrategy::Custom. - Sync
Recycle Check - A user-supplied synchronous per-checkout health check for
SyncRecycleStrategy::Custom.