Skip to main content

SentinelPool

Struct SentinelPool 

Source
pub struct SentinelPool { /* private fields */ }
Expand description

Sentinel 感知的异步连接池。

内部封装 bb8::Pool,并暴露常用的便捷方法。SentinelPoolClone 的, 克隆开销极低(只克隆几个 Arc),适合在不同 task 之间随手传递。

Implementations§

Source§

impl SentinelPool

Source

pub async fn new(config: SentinelPoolConfig) -> Result<Self>

基于配置构建连接池。

该方法是 async 的,因为 bb8::Pool::buildmin_idle > 0 时 会立刻预热相应数量的连接。

Source

pub fn config(&self) -> &SentinelPoolConfig

当前配置的只读视图。

Source

pub fn inner_pool(&self) -> &Pool<SentinelConnectionManager>

暴露底层 bb8::Pool,用于细粒度操作(如 state() 监控)。

Source

pub fn epoch(&self) -> u64

当前的 master epoch;每次 failover 后会递增。

Source

pub fn force_refresh(&self) -> u64

主动让池里所有连接立刻失效。

通常你不需要调用它 —— 后台 watcher 会自动处理。但例如在做演练、压测, 或者收到自己运维系统的信号时,可以手动调用以触发刷新。

Source

pub async fn get(&self) -> Result<ConnectionLease<'_>>

从池中借一条到 当前 master 的连接。

返回的 ConnectionLease 在 drop 时自动归还连接。如果归还前底层 连接已经损坏,会被自动丢弃,下次借用时由 manager 重新建立。

Source

pub async fn execute<F, Fut, T>(&self, op: F) -> Result<T>
where F: FnMut(MultiplexedConnection) -> Fut, Fut: Future<Output = Result<T, RedisError>>,

在一条新借来的连接上执行闭包,并按配置自动重试。

适合“业务侧根本不想关心 failover“的场景。重试逻辑:

  1. 调用 SentinelPool::get 拿连接;
  2. 执行闭包,成功就返回;
  3. 失败且错误看起来像连接级错误(IO / 已断开 / READONLY 等)时, 主动 force_refresh 后重试;
  4. 重试次数耗尽返回 SentinelPoolError::RetryExhausted
Source

pub async fn shutdown(self)

优雅关闭:停止后台 watcher 并等待退出。

池本身在最后一个 SentinelPool 实例被 drop 时由 bb8 内部释放。

Trait Implementations§

Source§

impl Clone for SentinelPool

Source§

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)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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: 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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