From 88dfedc5449057453cb9280d6078dcce52416378 Mon Sep 17 00:00:00 2001 From: Jokul Date: Sat, 4 Jul 2026 20:03:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Redis=20=E8=BF=9E=E6=8E=A5=E7=AD=89?= =?UTF-8?q?=E5=BE=85=E5=B0=B1=E7=BB=AA=E4=BF=A1=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - enableReadyCheck: true 等待 READY 信号 - 连接超时 12 秒 - 错误时正确断开连接 - 测试连接现在会返回具体错误信息 --- src/main/redis-service.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/main/redis-service.ts b/src/main/redis-service.ts index 79670b8..2abd7f5 100644 --- a/src/main/redis-service.ts +++ b/src/main/redis-service.ts @@ -24,9 +24,9 @@ function buildRedisOptions(opts: ConnectionOptions): RedisOptions { username: opts.username || undefined, password: opts.password || undefined, stringNumbers: true, - enableReadyCheck: false, + enableReadyCheck: true, family: 0, - connectTimeout: 30000, + connectTimeout: 10000, retryStrategy(times: number) { if (times > 3) return null return Math.min(times * 200, 1000) @@ -52,8 +52,27 @@ export async function connect(id: string, opts: ConnectionOptions): Promise { + const redis = new Redis(buildRedisOptions(opts)) + + redis.once('ready', () => { + clients.set(id, redis) + resolve() + }) + + redis.once('error', (err: Error) => { + redis.disconnect() + reject(err) + }) + + // Timeout + setTimeout(() => { + if (!clients.has(id)) { + redis.disconnect() + reject(new Error('Connection timeout')) + } + }, 12000) + }) } export async function disconnect(id: string): Promise {