Skip to main content

PgStoreConfig

Struct PgStoreConfig 

Source
pub struct PgStoreConfig { /* private fields */ }
Expand description

How a PgStores opens and shapes its connection pool.

The defaults are meant for a service that handles turns: a pool small enough that PostgreSQL is not the thing that falls over first, an acquire timeout short enough that a saturated pool surfaces as Unavailable instead of a hung request, and connections recycled often enough that a rolling database upgrade drains cleanly.

use std::time::Duration;

use turnframe_store_postgres::PgStoreConfig;

let config = PgStoreConfig::new()
    .max_connections(16)
    .statement_timeout(Duration::from_secs(5))
    .schema("turnframe")?;
assert_eq!(config.schema_name(), Some("turnframe"));

Implementations§

Source§

impl PgStoreConfig

Source

pub const DEFAULT_MAX_CONNECTIONS: u32 = 10

Upper bound on pooled connections. PostgreSQL serves every connection with a backend process, so this is a budget shared with every other service on the same cluster.

Source

pub const DEFAULT_MIN_CONNECTIONS: u32 = 1

Connections kept open while idle, so a quiet period does not make the next turn pay for a handshake.

Source

pub const DEFAULT_ACQUIRE_TIMEOUT: Duration

How long a caller waits for a connection before the store reports Unavailable.

Source

pub const DEFAULT_IDLE_TIMEOUT: Duration

How long an unused connection is kept.

Source

pub const DEFAULT_MAX_LIFETIME: Duration

How long any connection is kept, however busy. Recycling bounds the damage of a leaked server-side state and lets a rolling upgrade drain.

Source

pub fn new() -> Self

The defaults described above, on the public schema, with no statement timeout of the adapter’s own.

Source

pub fn max_connections(self, connections: u32) -> Self

Sets the maximum number of pooled connections.

Source

pub fn min_connections(self, connections: u32) -> Self

Sets the number of connections kept open while idle.

Source

pub fn acquire_timeout(self, timeout: Duration) -> Self

Sets how long a caller waits for a connection from the pool.

This is not a statement timeout: it bounds the wait for a connection, not the work done once one is held. See Self::statement_timeout.

Source

pub fn idle_timeout(self, timeout: Option<Duration>) -> Self

Sets how long an unused connection is kept; None keeps it forever.

Source

pub fn max_lifetime(self, lifetime: Option<Duration>) -> Self

Sets how long any connection is kept; None keeps it forever.

Source

pub fn statement_timeout(self, timeout: Duration) -> Self

Sets statement_timeout on every connection this pool opens.

§The advisory

A statement timeout is the only thing that bounds a query the database decides to run slowly, and without one a single lock wait can hold a pooled connection until the pool is empty and every turn fails. Set one.

Set it with these three consequences in mind.

It cancels statements, not transactions. PostgreSQL raises query_canceled on the statement that ran too long; this adapter reports Timeout and abandons the transaction, so a cancelled statement inside CommitStore::commit discards the whole bundle. That is the correct outcome — a partially written bundle is an invariant violation — but it means the timeout must be generous enough for the largest bundle a turn produces, not for the median statement.

Timeout is not a signal to retry. The contract reads it as “the write may have landed”: the caller re-reads and resumes by idempotency key. A timeout set so tight that healthy commits trip it turns every one of them into a recovery.

The outbox claim is the exception to keep short. claim_due takes row locks with SKIP LOCKED, so it never waits on another dispatcher; if it is slow, something else is wrong and cutting it off is right.

A few seconds suits a service handling turns. Leave it None to inherit whatever the role or the server sets, which is the better choice when the database is administered separately.

Source

pub fn inherit_statement_timeout(self) -> Self

Clears the adapter’s statement timeout, inheriting the server’s.

Source

pub fn schema(self, schema: impl Into<String>) -> Result<Self, ConfigError>

Puts every table in a dedicated schema instead of the connection’s default search_path.

PgStores::migrate creates the schema when it is missing, so pointing a fresh deployment at an empty database is one call. Tests use it to give each run a private namespace inside one database.

§Errors
  • ConfigError::InvalidSchemaName when the name is not a plain unquoted PostgreSQL identifier. The name is interpolated into CREATE SCHEMA and into search_path, neither of which can take a bind parameter, so it is validated here rather than escaped later.
Source

pub fn schema_name(&self) -> Option<&str>

The configured schema, if any.

Source

pub fn statement_timeout_value(&self) -> Option<Duration>

The configured statement timeout, if any.

Source

pub fn connection_bounds(&self) -> (u32, u32)

The configured pool bounds, as (min, max).

Trait Implementations§

Source§

impl Clone for PgStoreConfig

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PgStoreConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PgStoreConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for PgStoreConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Names the settings, never a connection string: this value is safe to log.

Source§

impl Eq for PgStoreConfig

Source§

impl PartialEq for PgStoreConfig

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for PgStoreConfig

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more