Skip to main content

ShardedPool

Struct ShardedPool 

Source
pub struct ShardedPool<C: Connection, S: ShardChooser> { /* private fields */ }
Expand description

A sharded connection pool that routes operations to the correct shard.

ShardedPool wraps multiple Pool instances, one per shard, and uses a ShardChooser to determine which shard to use for each operation.

§Example

// Create pools for each shard
let pool_0 = Pool::new(PoolConfig::new(10));
let pool_1 = Pool::new(PoolConfig::new(10));

// Create sharded pool with modulo chooser
let chooser = ModuloShardChooser::new(2);
let mut sharded = ShardedPool::new(chooser);
sharded.add_shard("shard_0", pool_0);
sharded.add_shard("shard_1", pool_1);

// Acquire connection from specific shard
let conn = sharded.acquire_for_model(&cx, &order, factory).await?;

Implementations§

Source§

impl<C: Connection, S: ShardChooser> ShardedPool<C, S>

Source

pub fn new(chooser: S) -> Self

Create a new sharded pool with the given shard chooser.

Source

pub fn add_shard(&mut self, name: impl Into<String>, pool: Pool<C>)

Add a shard to the pool.

§Arguments
  • name - The shard name (must match names returned by the chooser)
  • pool - The connection pool for this shard
Source

pub fn add_shard_with_config( &mut self, name: impl Into<String>, config: PoolConfig, )

Add a shard with a new pool created from the given config.

Source

pub fn chooser(&self) -> &S

Get a reference to the shard chooser.

Source

pub fn get_shard(&self, name: &str) -> Option<&Pool<C>>

Get a reference to a specific shard’s pool.

Source

pub fn shard_names(&self) -> Vec<String>

Get all shard names.

Source

pub fn shard_count(&self) -> usize

Get the number of shards.

Source

pub fn has_shard(&self, name: &str) -> bool

Check if a shard exists.

Source

pub fn choose_for_model<M: Model>(&self, model: &M) -> Result<String, Error>

Choose the shard for a model based on its shard key.

Returns the shard name. Use this when you need to know the shard without acquiring a connection.

Source

pub fn choose_for_query(&self, hints: &QueryHints) -> Vec<String>

Choose shards for a query based on hints.

Source

pub async fn acquire_for_model<M, F, Fut>( &self, cx: &Cx, model: &M, factory: F, ) -> Outcome<PooledConnection<C>, Error>
where M: Model, F: Fn() -> Fut, Fut: Future<Output = Outcome<C, Error>>,

Acquire a connection from the shard determined by the model’s shard key.

§Arguments
  • cx - The async context
  • model - The model instance (must have a shard key)
  • factory - Connection factory function
§Errors

Returns an error if:

  • The model has no shard key
  • The determined shard doesn’t exist
  • Connection acquisition fails
Source

pub async fn acquire_from_shard<F, Fut>( &self, cx: &Cx, shard_name: &str, factory: F, ) -> Outcome<PooledConnection<C>, Error>
where F: Fn() -> Fut, Fut: Future<Output = Outcome<C, Error>>,

Acquire a connection from a specific shard by name.

§Arguments
  • cx - The async context
  • shard_name - The name of the shard to acquire from
  • factory - Connection factory function
§Errors

Returns an error if:

  • The shard doesn’t exist
  • Connection acquisition fails
Source

pub async fn acquire_for_query<F, Fut>( &self, cx: &Cx, hints: &QueryHints, factory: F, ) -> Result<HashMap<String, PooledConnection<C>>, Error>
where F: Fn() -> Fut + Clone, Fut: Future<Output = Outcome<C, Error>>,

Acquire connections from multiple shards for scatter-gather queries.

Returns a map of shard name to pooled connection for each successfully acquired connection. Failed acquisitions are logged but don’t fail the entire operation.

§Arguments
  • cx - The async context
  • hints - Query routing hints
  • factory - Connection factory function
Source

pub fn close(&self)

Close all shards.

Source

pub fn is_closed(&self) -> bool

Check if all shards are closed.

Source

pub fn stats(&self) -> ShardedPoolStats

Get aggregate statistics across all shards.

Auto Trait Implementations§

§

impl<C, S> Freeze for ShardedPool<C, S>

§

impl<C, S> RefUnwindSafe for ShardedPool<C, S>
where S: RefUnwindSafe,

§

impl<C, S> Send for ShardedPool<C, S>

§

impl<C, S> Sync for ShardedPool<C, S>

§

impl<C, S> Unpin for ShardedPool<C, S>

§

impl<C, S> UnwindSafe for ShardedPool<C, S>
where S: RefUnwindSafe,

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<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: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<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