pub struct DbPools { /* private fields */ }Expand description
Database pool abstraction supporting read replicas.
Wraps primary and optional replica pools, providing methods for
explicit read/write routing while maintaining backwards compatibility
through Deref<Target = PgPool>.
§Examples
§Single Pool Configuration
use sqlx_pool_registry::sqlx::{self, PgPool};
use sqlx_pool_registry::DbPools;
let pool = PgPool::connect("postgresql://localhost/db").await?;
let pools = DbPools::new(pool);
// Both read() and write() return the same pool
assert!(!pools.has_replica());§Primary/Replica Configuration
use sqlx_pool_registry::sqlx::{self, postgres::PgPoolOptions};
use sqlx_pool_registry::DbPools;
let primary = PgPoolOptions::new()
.max_connections(5)
.connect("postgresql://primary/db")
.await?;
let replica = PgPoolOptions::new()
.max_connections(10)
.connect("postgresql://replica/db")
.await?;
let pools = DbPools::with_replica(primary, replica);
assert!(pools.has_replica());Implementations§
Source§impl DbPools
impl DbPools
Sourcepub fn new(primary: PgPool) -> Self
pub fn new(primary: PgPool) -> Self
Create a new DbPools with only a primary pool.
This is useful for development or when you don’t have a read replica configured. All read and write operations will route to the primary pool.
§Example
use sqlx_pool_registry::sqlx::{self, PgPool};
use sqlx_pool_registry::DbPools;
let pool = PgPool::connect("postgresql://localhost/db").await?;
let pools = DbPools::new(pool);Sourcepub fn with_replica(primary: PgPool, replica: PgPool) -> Self
pub fn with_replica(primary: PgPool, replica: PgPool) -> Self
Create a new DbPools with primary and replica pools.
Read operations will route to the replica pool for load distribution, while write operations always use the primary pool.
§Example
use sqlx_pool_registry::sqlx::{self, postgres::PgPoolOptions};
use sqlx_pool_registry::DbPools;
let primary = PgPoolOptions::new()
.max_connections(5)
.connect("postgresql://primary/db")
.await?;
let replica = PgPoolOptions::new()
.max_connections(10)
.connect("postgresql://replica/db")
.await?;
let pools = DbPools::with_replica(primary, replica);Sourcepub fn primary(&self) -> &PgPool
pub fn primary(&self) -> &PgPool
Get the primary pool.
Unlike write, this method describes the physical
pool topology rather than the operation being routed. Both methods return
the same pool for DbPools.
Sourcepub fn replica(&self) -> Option<&PgPool>
pub fn replica(&self) -> Option<&PgPool>
Get the configured replica pool, if one exists.
This method does not fall back to the primary pool. Use
read when a routable read pool is required.
Sourcepub fn has_replica(&self) -> bool
pub fn has_replica(&self) -> bool
Check if a replica pool is configured.
Returns true if a replica pool was provided via with_replica.
§Example
use sqlx_pool_registry::sqlx::{self, PgPool};
use sqlx_pool_registry::DbPools;
let pool = PgPool::connect("postgresql://localhost/db").await?;
let pools = DbPools::new(pool);
assert!(!pools.has_replica());Sourcepub async fn close(&self)
pub async fn close(&self)
Close all database connections.
Closes both primary and replica pools (if configured).
§Example
use sqlx_pool_registry::sqlx::{self, PgPool};
use sqlx_pool_registry::DbPools;
let pool = PgPool::connect("postgresql://localhost/db").await?;
let pools = DbPools::new(pool);
pools.close().await;Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for DbPools
impl !UnwindSafe for DbPools
impl Freeze for DbPools
impl Send for DbPools
impl Sync for DbPools
impl Unpin for DbPools
impl UnsafeUnpin for DbPools
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<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