pub struct PgPool { /* private fields */ }Expand description
A single-threaded, worker-local connection pool.
Connections are stored in an idle FIFO queue. On checkout the oldest idle connection is returned; on return (guard drop) the connection is pushed to the back of the queue.
Implementations§
Source§impl PgPool
impl PgPool
Sourcepub fn new(config: PgConfig, size: usize) -> PgPool
pub fn new(config: PgConfig, size: usize) -> PgPool
Create a new pool with the given configuration and size. Connections are lazily initialized on first checkout.
Sourcepub fn with_config(config: PgConfig, pool_config: PgPoolConfig) -> PgPool
pub fn with_config(config: PgConfig, pool_config: PgPoolConfig) -> PgPool
Create a new pool with full configuration.
Sourcepub fn connect(config: PgConfig, size: usize) -> Result<PgPool, PgError>
pub fn connect(config: PgConfig, size: usize) -> Result<PgPool, PgError>
Create a pool and eagerly initialize size connections.
Sourcepub fn connect_with_config(
config: PgConfig,
pool_config: PgPoolConfig,
) -> Result<PgPool, PgError>
pub fn connect_with_config( config: PgConfig, pool_config: PgPoolConfig, ) -> Result<PgPool, PgError>
Create a pool with full config and eagerly initialize min_size connections.
Sourcepub fn try_get(&mut self) -> Result<ConnectionGuard<'_>, PgError>
pub fn try_get(&mut self) -> Result<ConnectionGuard<'_>, PgError>
Non-blocking attempt to get a connection.
Returns a ConnectionGuard wrapping the connection. When the guard
is dropped the connection is returned to the idle queue automatically.
Returns Err(PgError::PoolExhausted) if no connection is available and
the pool is at capacity.
Sourcepub fn get(&mut self) -> Result<ConnectionGuard<'_>, PgError>
pub fn get(&mut self) -> Result<ConnectionGuard<'_>, PgError>
Get a connection, waiting up to the configured checkout_timeout.
Internally calls try_checkout in a loop with a short sleep between
attempts. In a production event-loop the sleep would be replaced
by yielding to the scheduler.
Sourcepub fn reap(&mut self)
pub fn reap(&mut self)
Reap expired connections (call periodically from your event loop).
Removes connections that have exceeded max_lifetime or idle_timeout,
then ensures min_size idle connections exist.
Sourcepub fn pool_config(&self) -> &PgPoolConfig
pub fn pool_config(&self) -> &PgPoolConfig
Get the pool config.
Sourcepub fn set_max_size(&mut self, new_size: usize)
pub fn set_max_size(&mut self, new_size: usize)
Resize the pool at runtime.
If new_size is smaller than the current total, excess idle
connections are discarded immediately. Active (checked-out)
connections are not interrupted — the pool will converge to the
new size as they are returned.
Sourcepub fn idle_connections(&self) -> usize
pub fn idle_connections(&self) -> usize
Number of idle connections available for checkout.
Sourcepub fn active_connections(&self) -> usize
pub fn active_connections(&self) -> usize
Number of connections currently checked out.
Sourcepub fn total_connections(&self) -> usize
pub fn total_connections(&self) -> usize
Total connections (idle + active).