pub struct ShardingRouter { /* private fields */ }Expand description
分片路由器
根据 ShardingStrategy 将 key 路由到对应的 shard。
v0.3.0 起支持 Enum/List/Directory/Composite 等新策略。
Implementations§
Source§impl ShardingRouter
impl ShardingRouter
Sourcepub fn new(strategy: ShardingStrategy, shards: Vec<&str>) -> Self
pub fn new(strategy: ShardingStrategy, shards: Vec<&str>) -> Self
创建路由器(兼容旧 API:传入策略与 shard 列表)
Sourcepub fn new_enum(
mapping: HashMap<String, String>,
default: Option<String>,
) -> Self
pub fn new_enum( mapping: HashMap<String, String>, default: Option<String>, ) -> Self
构造枚举分片路由器
Sourcepub fn new_list(
keys: HashSet<String>,
target: String,
default: Option<String>,
) -> Self
pub fn new_list( keys: HashSet<String>, target: String, default: Option<String>, ) -> Self
构造列表分片路由器
Sourcepub fn new_directory(table: HashMap<String, String>) -> Self
pub fn new_directory(table: HashMap<String, String>) -> Self
构造目录分片路由器
Sourcepub fn new_composite(
primary: ShardingStrategy,
primary_shards: Vec<String>,
secondary: ShardingStrategy,
secondary_shards: Vec<String>,
) -> Self
pub fn new_composite( primary: ShardingStrategy, primary_shards: Vec<String>, secondary: ShardingStrategy, secondary_shards: Vec<String>, ) -> Self
构造复合分片路由器
Sourcepub fn route(&self, key: &str) -> Result<&str, ShardingError>
pub fn route(&self, key: &str) -> Result<&str, ShardingError>
根据 key 路由到对应的 shard
§Errors
- Hash/Range/Date 策略下 shards 为空时返回
ShardingError::NoShardsConfigured - Enum/List/Directory 策略未匹配且无默认时返回
ShardingError::NoMappingForKey
Sourcepub fn route_by_data(
&self,
data: &dyn Any,
extractor: &dyn ShardKeyExtractor,
) -> Result<&str, ShardingError>
pub fn route_by_data( &self, data: &dyn Any, extractor: &dyn ShardKeyExtractor, ) -> Result<&str, ShardingError>
Sourcepub fn strategy(&self) -> ShardingStrategy
pub fn strategy(&self) -> ShardingStrategy
返回当前策略(克隆)
Sourcepub fn shard_count(&self) -> usize
pub fn shard_count(&self) -> usize
返回 shard 数量
Auto Trait Implementations§
impl Freeze for ShardingRouter
impl RefUnwindSafe for ShardingRouter
impl Send for ShardingRouter
impl Sync for ShardingRouter
impl Unpin for ShardingRouter
impl UnsafeUnpin for ShardingRouter
impl UnwindSafe for ShardingRouter
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