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::PgPool;
use sqlx_pool_router::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::postgres::PgPoolOptions;
use sqlx_pool_router::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::PgPool;
use sqlx_pool_router::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::postgres::PgPoolOptions;
use sqlx_pool_router::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 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::PgPool;
use sqlx_pool_router::DbPools;
let pool = PgPool::connect("postgresql://localhost/db").await?;
let pools = DbPools::new(pool);
assert!(!pools.has_replica());Trait Implementations§
Auto Trait Implementations§
impl Freeze for DbPools
impl !RefUnwindSafe for DbPools
impl Send for DbPools
impl Sync for DbPools
impl Unpin for DbPools
impl !UnwindSafe 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
Mutably borrows from an owned value. Read more
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>
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 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>
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