Skip to main content

redis_sentinel_pool/
lib.rs

1//! # redis-sentinel-pool
2//!
3//! 基于 [`redis`] 1.2.1 的 `sentinel` 模块和 [`bb8`] 连接池构建的
4//! **Sentinel 感知的异步连接池**。
5//!
6//! ## 目标
7//!
8//! * **节点切换对业务透明**:master failover 发生时,业务侧不需要感知,
9//!   下一次 [`SentinelPool::get`] 就会拿到指向新 master 的连接。
10//! * **快速感知**:可选的后台 watcher 订阅 sentinel 的 `+switch-master` 事件,
11//!   收到通知后立即作废全池连接。
12//! * **零侵入**:暴露的 `MultiplexedConnection` 与 redis-rs 完全兼容,
13//!   所有 [`redis::AsyncCommands`] / `redis::cmd!` 等都可正常使用。
14//!
15//! ## 快速开始
16//!
17//! ```no_run
18//! use redis::AsyncCommands;
19//! use redis_sentinel_pool::{SentinelPool, SentinelPoolConfig};
20//!
21//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
22//! let cfg = SentinelPoolConfig::new(
23//!     vec![
24//!         "redis://127.0.0.1:26379",
25//!         "redis://127.0.0.1:26380",
26//!         "redis://127.0.0.1:26381",
27//!     ],
28//!     "mymaster",
29//! )
30//! .max_size(32);
31//!
32//! let pool = SentinelPool::new(cfg).await?;
33//!
34//! let mut conn = pool.get().await?;
35//! let _: () = conn.set("hello", "world").await?;
36//! let value: String = conn.get("hello").await?;
37//! println!("{value}");
38//! # Ok(()) }
39//! ```
40//!
41//! 想要"完全无脑写业务、不操心 failover",用 [`SentinelPool::execute`]:
42//!
43//! ```no_run
44//! use redis::AsyncCommands;
45//! use redis_sentinel_pool::{SentinelPool, SentinelPoolConfig};
46//!
47//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
48//! # let pool = SentinelPool::new(SentinelPoolConfig::new(vec!["redis://127.0.0.1:26379"], "mymaster")).await?;
49//! let result: Option<String> = pool
50//!     .execute(|mut conn| async move { conn.get("hello").await })
51//!     .await?;
52//! # Ok(()) }
53//! ```
54
55#![cfg_attr(docsrs, feature(doc_cfg))]
56#![warn(missing_docs)]
57
58mod config;
59mod error;
60mod manager;
61mod pool;
62mod watcher;
63
64pub use config::{SentinelPoolConfig, ServerRole};
65pub use error::{Result, SentinelPoolError};
66pub use manager::{PooledConnection, SentinelConnectionManager};
67pub use pool::{ConnectionLease, SentinelPool};
68
69// 重导出常用的 redis 类型,方便调用方少写一行 `use redis::...`。
70pub use redis;