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§
- Connection
Lease - 一次“借用“。drop 时连接会归还给池子。
- Pooled
Connection - 池中实际持有的连接。
- Sentinel
Connection Manager - bb8 的
bb8::ManageConnection实现。 - Sentinel
Pool - Sentinel 感知的异步连接池。
- Sentinel
Pool Config - Sentinel 连接池配置。
Enums§
- Sentinel
Pool Error - 连接池操作的统一错误类型。
- Server
Role - 节点角色:master 或 replica。
Type Aliases§
- Result
- 本 crate 的统一
Result别名。