Skip to main content

PgPool

Struct PgPool 

Source
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

Source

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.

Source

pub fn with_config(config: PgConfig, pool_config: PgPoolConfig) -> PgPool

Create a new pool with full configuration.

Source

pub fn connect(config: PgConfig, size: usize) -> Result<PgPool, PgError>

Create a pool and eagerly initialize size connections.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn config(&self) -> &PgConfig

Get the pool configuration (PgConfig).

Source

pub fn pool_config(&self) -> &PgPoolConfig

Get the pool config.

Source

pub fn pool_size(&self) -> usize

Get the maximum pool size.

Source

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.

Source

pub fn idle_connections(&self) -> usize

Number of idle connections available for checkout.

Source

pub fn active_connections(&self) -> usize

Number of connections currently checked out.

Source

pub fn total_connections(&self) -> usize

Total connections (idle + active).

Source

pub fn stats(&self) -> &PoolStats

Get pool statistics.

Source

pub fn close_all(&mut self)

Close all idle connections in the pool.

Trait Implementations§

Source§

impl Executor for PgPool

Source§

fn execute(&mut self, query: &str, params: &[&dyn ToSql]) -> OrmResult<u64>

Executes a command (e.g., INSERT, UPDATE, DELETE) and returns the number of affected rows.
Source§

fn query(&mut self, query: &str, params: &[&dyn ToSql]) -> OrmResult<Vec<Row>>

Executes a query and returns the resulting rows.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.