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 fn with_instrumentation(self, instr: Arc<dyn Instrumentation>) -> Self
pub fn with_instrumentation(self, instr: Arc<dyn Instrumentation>) -> Self
Install an Instrumentation impl. Replaces whatever was inherited
from Config::with_instrumentation. Affects this Pool handle and
any Pool::clone() made after this call; existing clones keep the
previous instrumentation.
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.
Trait Implementations§
Source§impl GenericClient for &Pool
GenericClient for &Pool — acquire one connection, run the query,
return it to the pool on drop.
impl GenericClient for &Pool
GenericClient for &Pool — acquire one connection, run the query,
return it to the pool on drop.
This is a direct impl rather than going through AsPool::with_conn
because the 'a-fixed closure signature in AsPool cannot be satisfied
for a locally-acquired PooledConnection in safe Rust (the borrow
checker cannot prove the local outlives 'a even though the async block
that owns it is itself 'a-bounded, and unsafe_code is forbidden
workspace-wide). The direct impl avoids that lifetime knot entirely.
Source§async fn query(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Vec<Row>>
async fn query( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Vec<Row>>
Source§async fn query_one(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Row>
async fn query_one( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Row>
Source§async fn query_opt(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>>
async fn query_opt( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Option<Row>>
Source§async fn execute(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<u64>
async fn execute( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<u64>
Source§async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>>
async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>>
Source§async fn query_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Vec<Row>>
async fn query_typed( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Vec<Row>>
Source§async fn query_typed_one(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Row>
async fn query_typed_one( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Row>
Source§async fn query_typed_opt(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Option<Row>>
async fn query_typed_opt( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Option<Row>>
Source§async fn execute_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<u64>
async fn execute_typed( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<u64>
Source§async fn execute_pipeline(
&mut self,
batch: PipelineBatch,
) -> Result<Vec<QueryResult>>
async fn execute_pipeline( &mut self, batch: PipelineBatch, ) -> Result<Vec<QueryResult>>
PipelineBatch in one round-trip.