pub struct Pool { /* private fields */ }Expand description
A connection pool for PostgreSQL.
Cheaply cloneable (internally Arc’d). Uses a semaphore to limit max connections and a mutex-protected deque for idle connection management. Designed for <0.5μs checkout latency.
§Lifecycle Callbacks
Three optional callbacks control connection lifecycle:
after_connect— runs once per new connection (session setup)before_acquire— runs before handing out a connection (validation)after_release— runs when a connection returns to the pool (cleanup)
§Example
use sentinel_driver::{Config, pool::{Pool, config::PoolConfig}};
use std::time::Duration;
let config = Config::parse("postgres://user:pass@localhost/db")?;
let pool = Pool::new(config, PoolConfig::new().max_connections(10));
let conn = pool.acquire().await?;
// use conn...
// conn is returned to pool on dropImplementations§
Source§impl Pool
impl Pool
Sourcepub fn new(config: Config, pool_config: PoolConfig) -> Self
pub fn new(config: Config, pool_config: PoolConfig) -> Self
Create a new connection pool. No connections are opened until acquire().
Sourcepub fn connect_lazy(config: Config, pool_config: PoolConfig) -> Self
pub fn connect_lazy(config: Config, pool_config: PoolConfig) -> Self
Create a pool that defers all connection establishment until the
first acquire() call.
This is identical to new() — both are lazy. Provided for API
compatibility with connection pools that eagerly open connections.
let config = Config::parse("postgres://user:pass@localhost/db")?;
let pool = Pool::connect_lazy(config, PoolConfig::new());
// No connections opened yet — first acquire() will connect.Sourcepub async fn acquire(&self) -> Result<PooledConnection>
pub async fn acquire(&self) -> Result<PooledConnection>
Acquire a connection from the pool.
If an idle connection is available, it’s returned immediately.
Otherwise, a new connection is created (up to max_connections).
If the pool is full, waits up to acquire_timeout.
Sourcepub async fn idle_count(&self) -> usize
pub async fn idle_count(&self) -> usize
Number of idle connections.
Sourcepub async fn total_count(&self) -> usize
pub async fn total_count(&self) -> usize
Total number of connections (idle + in use).
Sourcepub fn max_connections(&self) -> usize
pub fn max_connections(&self) -> usize
Maximum number of connections allowed.
Sourcepub async fn metrics(&self) -> PoolMetrics
pub async fn metrics(&self) -> PoolMetrics
Get a snapshot of pool metrics.