tin_redis_conn/
pool.rs

1use crate::ConnectionError;
2use crate::client::{RedisClient, RedisConfig};
3use crate::error::Result;
4use redis::aio::ConnectionManager;
5use std::time::Duration;
6
7/// Redis 连接池配置
8#[derive(Debug, Clone)]
9pub struct PoolConfig {
10    /// 连接超时时间
11    pub connection_timeout: Duration,
12    /// 重连间隔
13    pub retry_interval: Duration,
14    /// 最大重连次数
15    pub max_retries: u32,
16    /// 保持连接活跃
17    pub keep_alive: bool,
18}
19
20impl Default for PoolConfig {
21    fn default() -> Self {
22        Self {
23            connection_timeout: Duration::from_secs(30),
24            retry_interval: Duration::from_millis(100),
25            max_retries: 3,
26            keep_alive: true,
27        }
28    }
29}
30
31/// Redis 连接池 - 使用 redis connection-manager
32pub struct RedisPool;
33
34impl RedisPool {
35    /// 创建新的 Redis 连接管理器
36    ///
37    /// # Arguments
38    ///
39    /// * `config` - Redis 配置信息
40    /// * `_pool_config` - 连接池配置(暂时保留用于兼容性)
41    ///
42    /// # Returns
43    ///
44    /// 返回 ConnectionManager 实例或错误
45    pub async fn create(config: RedisConfig) -> Result<ConnectionManager> {
46        // 构建 Redis URL
47        let redis_url = RedisClient::build_redis_url(&config)?;
48
49        // 创建 Redis 客户端
50        let client = redis::Client::open(redis_url)
51            .map_err(|e| ConnectionError::PoolCreation(format!("Failed to create client: {e}")))?;
52
53        // 创建连接管理器
54        let manager = ConnectionManager::new(client).await.map_err(|e| {
55            ConnectionError::PoolCreation(format!("Failed to create connection manager: {e}"))
56        })?;
57
58        Ok(manager)
59    }
60}