971c37c
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use config::redis::{RedisConfig, RedisMode};
use redis::aio::{ConnectionManager, MultiplexedConnection};
use redis::cluster_async::ClusterConnection;
use redis::{ConnectionAddr, ConnectionInfo, Value};
use crate::error::ExportError;
#[derive(Clone)]
pub enum RedisConnection {
Standalone(ConnectionManager),
Cluster(ClusterConnection),
}
impl RedisConnection {
pub fn is_cluster(&self) -> bool {
matches!(self, RedisConnection::Cluster(_))
}
}
#[derive(Clone)]
pub struct AppRedis {
config: RedisConfig,
/// One multiplexed connection per pool slot; every slot is an independent
/// connection (not a clone of another slot) so requests spread over
/// multiple sockets.
connections: Vec<RedisConnection>,
/// Round-robin cursor shared across clones of this handle.
cursor: Arc<AtomicUsize>,
/// Connection parameters (auth, tls) of the first configured node,
/// reused to open per-node connections for cluster-wide operations.
node_info: Option<ConnectionInfo>,
}
impl AppRedis {
pub async fn connect(config: RedisConfig) -> Result<Self, ExportError> {
let url = config.validate().map_err(ExportError::InvalidConfig)?;
let pool_size = config.pool_size().max(1);
let (connections, node_info) = match config.mode() {
RedisMode::Cluster => {
let nodes = url
.split(',')
.map(str::trim)
.filter(|node| !node.is_empty())
.map(String::from)
.collect::<Vec<_>>();
let client =
redis::cluster::ClusterClient::new(nodes).map_err(ExportError::from)?;
let node_info = connection_info(url)?;
let mut connections = Vec::with_capacity(pool_size);
for _ in 0..pool_size {
let connection = client
.get_async_connection()
.await
.map_err(ExportError::from)?;
connections.push(RedisConnection::Cluster(connection));
}
(connections, Some(node_info))
}
RedisMode::Standalone | RedisMode::Sentinel | RedisMode::ValkeyStandalone => {
let mut connections = Vec::with_capacity(pool_size);
for _ in 0..pool_size {
let client = redis::Client::open(url).map_err(ExportError::from)?;
let manager = ConnectionManager::new(client)
.await
.map_err(ExportError::from)?;
connections.push(RedisConnection::Standalone(manager));
}
let node_info = connection_info(url)?;
(connections, Some(node_info))
}
};
Ok(Self {
config,
connections,
cursor: Arc::new(AtomicUsize::new(0)),
node_info,
})
}
pub fn config(&self) -> &RedisConfig {
&self.config
}
pub fn mode(&self) -> RedisMode {
self.config.mode()
}
pub fn is_cluster(&self) -> bool {
self.config.mode() == RedisMode::Cluster
}
pub fn connection(&self) -> &RedisConnection {
&self.connections[0]
}
pub fn connection_clone(&self) -> RedisConnection {
let index = self.cursor.fetch_add(1, Ordering::Relaxed) % self.connections.len();
self.connections[index].clone()
}
pub async fn publish(&self, channel: &str, payload: &str) -> Result<(), ExportError> {
let mut conn = self.connection_clone();
let result: Result<i64, redis::RedisError> = match &mut conn {
RedisConnection::Standalone(c) => {
redis::cmd("PUBLISH")
.arg(channel)
.arg(payload)
.query_async(c)
.await
}
RedisConnection::Cluster(c) => {
redis::cmd("PUBLISH")
.arg(channel)
.arg(payload)
.query_async(c)
.await
}
};
result.map(|_| ()).map_err(ExportError::from)
}
/// `SET key 1 NX EX ttl`; returns false when the key already exists
/// (already consumed / inside an active window) or redis fails.
pub async fn set_nx_ex(&self, key: &str, ttl_secs: u64) -> bool {
let mut conn = self.connection_clone();
let result: Result<Option<String>, redis::RedisError> = match &mut conn {
RedisConnection::Standalone(c) => {
redis::cmd("SET")
.arg(key)
.arg("1")
.arg("NX")
.arg("EX")
.arg(ttl_secs.max(1))
.query_async(c)
.await
}
RedisConnection::Cluster(c) => {
redis::cmd("SET")
.arg(key)
.arg("1")
.arg("NX")
.arg("EX")
.arg(ttl_secs.max(1))
.query_async(c)
.await
}
};
matches!(result, Ok(Some(_)))
}
/// `INCR key` with a window TTL set on first increment; returns the count.
pub async fn incr_window(&self, key: &str, window_secs: u64) -> i64 {
let mut conn = self.connection_clone();
let result: Result<i64, redis::RedisError> = match &mut conn {
RedisConnection::Standalone(c) => {
let count: i64 = redis::cmd("INCR")
.arg(key)
.query_async(c)
.await
.unwrap_or(0);
if count == 1 {
let _: Result<redis::Value, redis::RedisError> = redis::cmd("EXPIRE")
.arg(key)
.arg(window_secs)
.query_async(c)
.await;
}
Ok(count)
}
RedisConnection::Cluster(c) => {
let count: i64 = redis::cmd("INCR")
.arg(key)
.query_async(c)
.await
.unwrap_or(0);
if count == 1 {
let _: Result<redis::Value, redis::RedisError> = redis::cmd("EXPIRE")
.arg(key)
.arg(window_secs)
.query_async(c)
.await;
}
Ok(count)
}
};
result.unwrap_or(0)
}
/// Rolling-window quota check; `max == 0` disables the quota.
pub async fn quota_allow(&self, key: &str, max: i64, window_secs: u64) -> bool {
if max <= 0 {
return true;
}
self.incr_window(key, window_secs).await <= max
}
/// Opens a multiplexed connection to every master node of the cluster.
///
/// Masters are discovered at call time via `CLUSTER SLOTS`, so this keeps
/// working after topology changes. Returns an empty list for standalone
/// connections.
pub async fn cluster_master_connections(
&self,
) -> Result<Vec<MultiplexedConnection>, ExportError> {
let mut conn = self.connection_clone();
let RedisConnection::Cluster(cluster) = &mut conn else {
return Ok(Vec::new());
};
let slots: Value = redis::cmd("CLUSTER")
.arg("SLOTS")
.query_async(cluster)
.await
.map_err(ExportError::from)?;
let node_info = self.node_info.clone().ok_or_else(|| {
ExportError::InvalidConfig("cluster connection info is unavailable".into())
})?;
let mut seen = std::collections::HashSet::new();
let mut connections = Vec::new();
for (host, port) in cluster_master_addrs(&slots) {
if !seen.insert((host.clone(), port)) {
continue;
}
let addr = match node_info.addr() {
ConnectionAddr::Tcp(_, _) => ConnectionAddr::Tcp(host.clone(), port),
ConnectionAddr::TcpTls {
insecure,
tls_params,
..
} => ConnectionAddr::TcpTls {
host: host.clone(),
port,
insecure: *insecure,
tls_params: tls_params.clone(),
},
ConnectionAddr::Unix(_) => {
return Err(ExportError::InvalidConfig(
"unix sockets are not supported for cluster connections".into(),
));
}
_ => {
return Err(ExportError::InvalidConfig(
"unsupported connection address type for cluster connections".into(),
));
}
};
let mut info = node_info.clone();
info = info.set_addr(addr);
let client = redis::Client::open(info).map_err(ExportError::from)?;
// BRPOP 之类的阻塞命令会持有连接直到超时;必须关闭响应超时,
// 否则默认超时会让阻塞读被误判为 timeout 错误。
let conn_config = redis::AsyncConnectionConfig::new()
.set_response_timeout(None)
.set_connection_timeout(Some(self.config.connection_timeout()));
let connection = client
.get_multiplexed_async_connection_with_config(&conn_config)
.await
.map_err(ExportError::from)?;
connections.push(connection);
}
Ok(connections)
}
/// Returns `(slot_start, slot_end, master_index)` triples where `master_index`
/// matches the position of the owning connection in [`cluster_master_connections`].
///
/// Empty for standalone connections. The ordering mirrors the dedup logic used
/// when collecting master addresses, so callers can map a slot to the right
/// pre-established connection without reconnecting.
pub async fn cluster_slot_ranges(&self) -> Result<Vec<(i64, i64, usize)>, ExportError> {
let mut conn = self.connection_clone();
let RedisConnection::Cluster(cluster) = &mut conn else {
return Ok(Vec::new());
};
let slots: Value = redis::cmd("CLUSTER")
.arg("SLOTS")
.query_async(cluster)
.await
.map_err(ExportError::from)?;
Ok(cluster_slot_ranges(&slots))
}
}
/// Extracts the host/port of every master from a `CLUSTER SLOTS` response.
///
/// Each slot group is `[start, end, [ip, port, node_id, ...], replicas...]`,
/// where the first entry of the node list is the master.
fn cluster_master_addrs(slots: &Value) -> Vec<(String, u16)> {
let Value::Array(groups) = slots else {
return Vec::new();
};
let mut masters = Vec::new();
for group in groups {
let Value::Array(fields) = group else {
continue;
};
let Some(Value::Array(nodes)) = fields.get(2) else {
continue;
};
let (Some(Value::BulkString(host)), Some(Value::Int(port))) = (nodes.first(), nodes.get(1))
else {
continue;
};
masters.push((String::from_utf8_lossy(host).into_owned(), *port as u16));
}
masters
}
/// Builds the connection info of the first configured node, used as a template
/// (scheme, auth, tls settings) for connections to other cluster nodes.
fn connection_info(urls: &str) -> Result<ConnectionInfo, ExportError> {
let first = urls
.split(',')
.map(str::trim)
.find(|node| !node.is_empty())
.ok_or_else(|| ExportError::InvalidConfig("redis url contains no nodes".into()))?;
Ok(redis::Client::open(first)?.get_connection_info().clone())
}
macro_rules! dispatch {
($conn:expr, |$c:ident| $body:expr) => {
match &mut $conn {
RedisConnection::Standalone($c) => $body,
RedisConnection::Cluster($c) => $body,
}
};
}
pub async fn get_string(redis: &AppRedis, key: &str) -> Result<Option<String>, redis::RedisError> {
let mut conn = redis.connection_clone();
dispatch!(conn, |c| redis::cmd("GET").arg(key).query_async(c).await)
}
pub async fn set_string(
redis: &AppRedis,
key: &str,
value: &str,
ttl_secs: u64,
) -> Result<(), redis::RedisError> {
let mut conn = redis.connection_clone();
dispatch!(conn, |c| {
redis::cmd("SET")
.arg(key)
.arg(value)
.arg("EX")
.arg(ttl_secs)
.exec_async(c)
.await
})
}
pub async fn set_nx_ex(
redis: &AppRedis,
key: &str,
value: &str,
ttl_secs: u64,
) -> Result<bool, redis::RedisError> {
let mut conn = redis.connection_clone();
dispatch!(conn, |c| {
redis::cmd("SET")
.arg(key)
.arg(value)
.arg("NX")
.arg("EX")
.arg(ttl_secs)
.query_async(c)
.await
})
}
pub async fn delete(redis: &AppRedis, key: &str) -> Result<(), redis::RedisError> {
let mut conn = redis.connection_clone();
dispatch!(conn, |c| redis::cmd("DEL").arg(key).exec_async(c).await)
}
pub async fn exists(redis: &AppRedis, key: &str) -> Result<bool, redis::RedisError> {
let mut conn = redis.connection_clone();
dispatch!(conn, |c| redis::cmd("EXISTS").arg(key).query_async(c).await)
}
pub async fn getdel_string(
redis: &AppRedis,
key: &str,
) -> Result<Option<String>, redis::RedisError> {
let mut conn = redis.connection_clone();
dispatch!(conn, |c| redis::cmd("GETDEL").arg(key).query_async(c).await)
}
const INCR_EXPIRE_SCRIPT: &str = r#"local c = redis.call('INCR', KEYS[1])
if c == 1 then
redis.call('EXPIRE', KEYS[1], ARGV[1])
end
return c"#;
pub async fn incr_expire(
redis: &AppRedis,
key: &str,
ttl_secs: u64,
) -> Result<i64, redis::RedisError> {
let mut conn = redis.connection_clone();
dispatch!(conn, |c| {
redis::cmd("EVAL")
.arg(INCR_EXPIRE_SCRIPT)
.arg(1)
.arg(key)
.arg(ttl_secs)
.query_async(c)
.await
})
}
/// Extracts `(start, end, master_index)` for every slot range.
///
/// `master_index` counts unique master addresses in first-seen order, matching
/// [`AppRedis::cluster_master_connections`].
fn cluster_slot_ranges(slots: &Value) -> Vec<(i64, i64, usize)> {
let Value::Array(groups) = slots else {
return Vec::new();
};
let mut seen: std::collections::HashMap<(String, u16), usize> =
std::collections::HashMap::new();
let mut ranges = Vec::new();
for group in groups {
let Value::Array(fields) = group else {
continue;
};
let (Some(Value::Int(start)), Some(Value::Int(end))) = (fields.first(), fields.get(1))
else {
continue;
};
let Some(Value::Array(nodes)) = fields.get(2) else {
continue;
};
let (Some(Value::BulkString(host)), Some(Value::Int(port))) = (nodes.first(), nodes.get(1))
else {
continue;
};
let addr = (String::from_utf8_lossy(host).to_string(), *port as u16);
let next_index = seen.len();
let index = *seen.entry(addr).or_insert(next_index);
ranges.push((*start, *end, index));
}
ranges
}