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
impl PgStoreConfig
Sourcepub const DEFAULT_MAX_CONNECTIONS: u32 = 10
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.
Sourcepub const DEFAULT_MIN_CONNECTIONS: u32 = 1
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.
Sourcepub const DEFAULT_ACQUIRE_TIMEOUT: Duration
pub const DEFAULT_ACQUIRE_TIMEOUT: Duration
How long a caller waits for a connection before the store reports
Unavailable.
Sourcepub const DEFAULT_IDLE_TIMEOUT: Duration
pub const DEFAULT_IDLE_TIMEOUT: Duration
How long an unused connection is kept.
Sourcepub const DEFAULT_MAX_LIFETIME: Duration
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.
Sourcepub fn new() -> Self
pub fn new() -> Self
The defaults described above, on the public schema, with no statement
timeout of the adapter’s own.
Sourcepub fn max_connections(self, connections: u32) -> Self
pub fn max_connections(self, connections: u32) -> Self
Sets the maximum number of pooled connections.
Sourcepub fn min_connections(self, connections: u32) -> Self
pub fn min_connections(self, connections: u32) -> Self
Sets the number of connections kept open while idle.
Sourcepub fn acquire_timeout(self, timeout: Duration) -> Self
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.
Sourcepub fn idle_timeout(self, timeout: Option<Duration>) -> Self
pub fn idle_timeout(self, timeout: Option<Duration>) -> Self
Sets how long an unused connection is kept; None keeps it forever.
Sourcepub fn max_lifetime(self, lifetime: Option<Duration>) -> Self
pub fn max_lifetime(self, lifetime: Option<Duration>) -> Self
Sets how long any connection is kept; None keeps it forever.
Sourcepub fn statement_timeout(self, timeout: Duration) -> Self
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.
Sourcepub fn inherit_statement_timeout(self) -> Self
pub fn inherit_statement_timeout(self) -> Self
Clears the adapter’s statement timeout, inheriting the server’s.
Sourcepub fn schema(self, schema: impl Into<String>) -> Result<Self, ConfigError>
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::InvalidSchemaNamewhen the name is not a plain unquoted PostgreSQL identifier. The name is interpolated intoCREATE SCHEMAand intosearch_path, neither of which can take a bind parameter, so it is validated here rather than escaped later.
Sourcepub fn schema_name(&self) -> Option<&str>
pub fn schema_name(&self) -> Option<&str>
The configured schema, if any.
Sourcepub fn statement_timeout_value(&self) -> Option<Duration>
pub fn statement_timeout_value(&self) -> Option<Duration>
The configured statement timeout, if any.
Sourcepub fn connection_bounds(&self) -> (u32, u32)
pub fn connection_bounds(&self) -> (u32, u32)
The configured pool bounds, as (min, max).
Trait Implementations§
Source§impl Clone for PgStoreConfig
impl Clone for PgStoreConfig
Source§impl Debug for PgStoreConfig
impl Debug for PgStoreConfig
Source§impl Default for PgStoreConfig
impl Default for PgStoreConfig
Source§impl Display for PgStoreConfig
impl Display for PgStoreConfig
impl Eq for PgStoreConfig
Source§impl PartialEq for PgStoreConfig
impl PartialEq for PgStoreConfig
impl StructuralPartialEq for PgStoreConfig
Auto Trait Implementations§
impl Freeze for PgStoreConfig
impl RefUnwindSafe for PgStoreConfig
impl Send for PgStoreConfig
impl Sync for PgStoreConfig
impl Unpin for PgStoreConfig
impl UnsafeUnpin for PgStoreConfig
impl UnwindSafe for PgStoreConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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