pub struct ReplicaPool<C: Connection> { /* private fields */ }Expand description
A pool that routes reads to replicas and writes to a primary.
§Example
ⓘ
let primary = Pool::new(primary_config);
let replica1 = Pool::new(replica1_config);
let replica2 = Pool::new(replica2_config);
let pool = ReplicaPool::new(primary, vec![replica1, replica2]);
// Reads go to replicas (factory provided at acquire time)
let conn = pool.acquire_read(&cx, || connect_replica()).await?;
// Writes go to primary
let conn = pool.acquire_write(&cx, || connect_primary()).await?;Implementations§
Source§impl<C: Connection> ReplicaPool<C>
impl<C: Connection> ReplicaPool<C>
Sourcepub fn new(primary: Pool<C>, replicas: Vec<Pool<C>>) -> Self
pub fn new(primary: Pool<C>, replicas: Vec<Pool<C>>) -> Self
Create a new replica pool with round-robin strategy.
Sourcepub fn with_strategy(
primary: Pool<C>,
replicas: Vec<Pool<C>>,
strategy: ReplicaStrategy,
) -> Self
pub fn with_strategy( primary: Pool<C>, replicas: Vec<Pool<C>>, strategy: ReplicaStrategy, ) -> Self
Create a new replica pool with a specific strategy.
Sourcepub async fn acquire_read<F, Fut>(
&self,
cx: &Cx,
factory: F,
) -> Outcome<PooledConnection<C>, Error>
pub async fn acquire_read<F, Fut>( &self, cx: &Cx, factory: F, ) -> Outcome<PooledConnection<C>, Error>
Acquire a connection for read operations.
If replicas are available, selects one based on the configured strategy. Falls back to the primary if no replicas are configured.
Sourcepub async fn acquire_write<F, Fut>(
&self,
cx: &Cx,
factory: F,
) -> Outcome<PooledConnection<C>, Error>
pub async fn acquire_write<F, Fut>( &self, cx: &Cx, factory: F, ) -> Outcome<PooledConnection<C>, Error>
Acquire a connection for write operations (always uses primary).
Sourcepub async fn acquire_primary<F, Fut>(
&self,
cx: &Cx,
factory: F,
) -> Outcome<PooledConnection<C>, Error>
pub async fn acquire_primary<F, Fut>( &self, cx: &Cx, factory: F, ) -> Outcome<PooledConnection<C>, Error>
Acquire a connection from the primary (for read-after-write consistency).
Sourcepub fn replica_count(&self) -> usize
pub fn replica_count(&self) -> usize
Get the number of replicas.
Sourcepub fn strategy(&self) -> ReplicaStrategy
pub fn strategy(&self) -> ReplicaStrategy
Get the current strategy.
Trait Implementations§
Auto Trait Implementations§
impl<C> !Freeze for ReplicaPool<C>
impl<C> RefUnwindSafe for ReplicaPool<C>
impl<C> Send for ReplicaPool<C>
impl<C> Sync for ReplicaPool<C>
impl<C> Unpin for ReplicaPool<C>
impl<C> UnsafeUnpin for ReplicaPool<C>
impl<C> UnwindSafe for ReplicaPool<C>
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).