| 44 | */ |
| 45 | const createRedisConfig = () => ({ |
| 46 | ...REDIS_CONFIG, |
| 47 | // TLS配置 |
| 48 | ...(isTLS ? { |
| 49 | tls: { |
| 50 | rejectUnauthorized: true |
| 51 | } |
| 52 | } : {}), |
| 53 | |
| 54 | // 重试策略 |
| 55 | retryStrategy(times) { |
| 56 | if (times > REDIS_CONFIG.maxRetries) { |
| 57 | logger.error(`Redis连接重试次数超限: ${times}`, 'REDIS') |
| 58 | return null |
| 59 | } |
| 60 | |
| 61 | const delay = Math.min(100 * Math.pow(2, times), 3000) |
| 62 | logger.info(`Redis重试连接: ${times}, 延迟: ${delay}ms`, 'REDIS', '🔄') |
| 63 | return delay |
| 64 | }, |
| 65 | |
| 66 | // 错误重连策略 |
| 67 | reconnectOnError(err) { |
| 68 | const targetErrors = ['READONLY', 'ETIMEDOUT', 'ECONNRESET', 'EPIPE'] |
| 69 | return targetErrors.some(e => err.message.includes(e)) |
| 70 | } |
| 71 | }) |
| 72 | |
| 73 | /** |
| 74 | * 验证 Redis 命令通道是否可用 |
| 75 | * @param {object} client - Redis 客户端实例 |