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>
impl<C: Connection, S: ShardChooser> ShardedPool<C, S>
Sourcepub fn add_shard(&mut self, name: impl Into<String>, pool: Pool<C>)
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
Sourcepub fn add_shard_with_config(
&mut self,
name: impl Into<String>,
config: PoolConfig,
)
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.
Sourcepub fn get_shard(&self, name: &str) -> Option<&Pool<C>>
pub fn get_shard(&self, name: &str) -> Option<&Pool<C>>
Get a reference to a specific shard’s pool.
Sourcepub fn shard_names(&self) -> Vec<String>
pub fn shard_names(&self) -> Vec<String>
Get all shard names.
Sourcepub fn shard_count(&self) -> usize
pub fn shard_count(&self) -> usize
Get the number of shards.
Sourcepub fn choose_for_model<M: Model>(&self, model: &M) -> Result<String, Error>
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.
Sourcepub fn choose_for_query(&self, hints: &QueryHints) -> Vec<String>
pub fn choose_for_query(&self, hints: &QueryHints) -> Vec<String>
Choose shards for a query based on hints.
Sourcepub async fn acquire_for_model<M, F, Fut>(
&self,
cx: &Cx,
model: &M,
factory: F,
) -> Outcome<PooledConnection<C>, Error>
pub async fn acquire_for_model<M, F, Fut>( &self, cx: &Cx, model: &M, factory: F, ) -> Outcome<PooledConnection<C>, Error>
Acquire a connection from the shard determined by the model’s shard key.
§Arguments
cx- The async contextmodel- 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
Sourcepub async fn acquire_from_shard<F, Fut>(
&self,
cx: &Cx,
shard_name: &str,
factory: F,
) -> Outcome<PooledConnection<C>, Error>
pub async fn acquire_from_shard<F, Fut>( &self, cx: &Cx, shard_name: &str, factory: F, ) -> Outcome<PooledConnection<C>, Error>
Sourcepub async fn acquire_for_query<F, Fut>(
&self,
cx: &Cx,
hints: &QueryHints,
factory: F,
) -> Result<HashMap<String, PooledConnection<C>>, Error>
pub async fn acquire_for_query<F, Fut>( &self, cx: &Cx, hints: &QueryHints, factory: F, ) -> Result<HashMap<String, PooledConnection<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 contexthints- Query routing hintsfactory- Connection factory function
Sourcepub fn stats(&self) -> ShardedPoolStats
pub fn stats(&self) -> ShardedPoolStats
Get aggregate statistics across all shards.