redis-sentinel-pool 0.1.0

An async Redis Sentinel-aware connection pool built on top of redis-rs and bb8, with transparent master failover.
Documentation
//! 错误类型定义。
//!
//! 本模块统一封装 Sentinel 连接池可能产生的所有错误,便于上层使用 `?`
//! 操作符或 `match` 进行分支处理。

use thiserror::Error;

/// 连接池操作的统一错误类型。
#[derive(Debug, Error)]
pub enum SentinelPoolError {
    /// 底层 redis-rs 抛出的错误(含 Sentinel 解析失败、命令执行失败等)。
    #[error("redis error: {0}")]
    Redis(#[from] redis::RedisError),

    /// bb8 连接池本身的错误(例如等待超时、池已关闭等)。
    #[error("connection pool error: {0}")]
    Pool(String),

    /// 配置参数非法。
    #[error("invalid configuration: {0}")]
    Config(String),

    /// 在多次重试后命令仍然失败。
    #[error("operation failed after {attempts} attempts: {source}")]
    RetryExhausted {
        /// 已经尝试的次数。
        attempts: u32,
        /// 最后一次失败的原因。
        #[source]
        source: redis::RedisError,
    },

    /// 后台 watcher 因为不可恢复的错误已经退出。
    #[error("sentinel watcher stopped: {0}")]
    WatcherStopped(String),
}

/// 本 crate 的统一 `Result` 别名。
pub type Result<T> = std::result::Result<T, SentinelPoolError>;

impl<E> From<bb8::RunError<E>> for SentinelPoolError
where
    E: Into<SentinelPoolError> + std::fmt::Display,
{
    fn from(value: bb8::RunError<E>) -> Self {
        match value {
            bb8::RunError::User(e) => e.into(),
            bb8::RunError::TimedOut => SentinelPoolError::Pool("timed out waiting for connection".into()),
        }
    }
}