pub struct SentinelPool { /* private fields */ }Expand description
Sentinel 感知的异步连接池。
内部封装 bb8::Pool,并暴露常用的便捷方法。SentinelPool 是 Clone 的,
克隆开销极低(只克隆几个 Arc),适合在不同 task 之间随手传递。
Implementations§
Source§impl SentinelPool
impl SentinelPool
Sourcepub async fn new(config: SentinelPoolConfig) -> Result<Self>
pub async fn new(config: SentinelPoolConfig) -> Result<Self>
基于配置构建连接池。
该方法是 async 的,因为 bb8::Pool::build 在 min_idle > 0 时
会立刻预热相应数量的连接。
Sourcepub fn config(&self) -> &SentinelPoolConfig
pub fn config(&self) -> &SentinelPoolConfig
当前配置的只读视图。
Sourcepub fn inner_pool(&self) -> &Pool<SentinelConnectionManager>
pub fn inner_pool(&self) -> &Pool<SentinelConnectionManager>
暴露底层 bb8::Pool,用于细粒度操作(如 state() 监控)。
Sourcepub fn force_refresh(&self) -> u64
pub fn force_refresh(&self) -> u64
主动让池里所有连接立刻失效。
通常你不需要调用它 —— 后台 watcher 会自动处理。但例如在做演练、压测, 或者收到自己运维系统的信号时,可以手动调用以触发刷新。
Sourcepub async fn get(&self) -> Result<ConnectionLease<'_>>
pub async fn get(&self) -> Result<ConnectionLease<'_>>
从池中借一条到 当前 master 的连接。
返回的 ConnectionLease 在 drop 时自动归还连接。如果归还前底层
连接已经损坏,会被自动丢弃,下次借用时由 manager 重新建立。
Sourcepub async fn execute<F, Fut, T>(&self, op: F) -> Result<T>
pub async fn execute<F, Fut, T>(&self, op: F) -> Result<T>
在一条新借来的连接上执行闭包,并按配置自动重试。
适合“业务侧根本不想关心 failover“的场景。重试逻辑:
- 调用
SentinelPool::get拿连接; - 执行闭包,成功就返回;
- 失败且错误看起来像连接级错误(IO / 已断开 / READONLY 等)时,
主动
force_refresh后重试; - 重试次数耗尽返回
SentinelPoolError::RetryExhausted。
Trait Implementations§
Source§impl Clone for SentinelPool
impl Clone for SentinelPool
Source§fn clone(&self) -> SentinelPool
fn clone(&self) -> SentinelPool
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for SentinelPool
impl !RefUnwindSafe for SentinelPool
impl Send for SentinelPool
impl Sync for SentinelPool
impl Unpin for SentinelPool
impl UnsafeUnpin for SentinelPool
impl !UnwindSafe for SentinelPool
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