Skip to main content

Crate redis_sentinel_pool

Crate redis_sentinel_pool 

Source
Expand description

§redis-sentinel-pool

基于 redis 1.2.1 的 sentinel 模块和 bb8 连接池构建的 Sentinel 感知的异步连接池

§目标

  • 节点切换对业务透明:master failover 发生时,业务侧不需要感知, 下一次 SentinelPool::get 就会拿到指向新 master 的连接。
  • 快速感知:可选的后台 watcher 订阅 sentinel 的 +switch-master 事件, 收到通知后立即作废全池连接。
  • 零侵入:暴露的 MultiplexedConnection 与 redis-rs 完全兼容, 所有 redis::AsyncCommands / redis::cmd! 等都可正常使用。

§快速开始

use redis::AsyncCommands;
use redis_sentinel_pool::{SentinelPool, SentinelPoolConfig};

let cfg = SentinelPoolConfig::new(
    vec![
        "redis://127.0.0.1:26379",
        "redis://127.0.0.1:26380",
        "redis://127.0.0.1:26381",
    ],
    "mymaster",
)
.max_size(32);

let pool = SentinelPool::new(cfg).await?;

let mut conn = pool.get().await?;
let _: () = conn.set("hello", "world").await?;
let value: String = conn.get("hello").await?;
println!("{value}");

想要“完全无脑写业务、不操心 failover“,用 SentinelPool::execute

use redis::AsyncCommands;
use redis_sentinel_pool::{SentinelPool, SentinelPoolConfig};

let result: Option<String> = pool
    .execute(|mut conn| async move { conn.get("hello").await })
    .await?;

Re-exports§

pub use redis;

Structs§

ConnectionLease
一次“借用“。drop 时连接会归还给池子。
PooledConnection
池中实际持有的连接。
SentinelConnectionManager
bb8 的 bb8::ManageConnection 实现。
SentinelPool
Sentinel 感知的异步连接池。
SentinelPoolConfig
Sentinel 连接池配置。

Enums§

SentinelPoolError
连接池操作的统一错误类型。
ServerRole
节点角色:master 或 replica。

Type Aliases§

Result
本 crate 的统一 Result 别名。