From c8db631f11908a3aeabe7a5cd4b9ab4452eedd41 Mon Sep 17 00:00:00 2001 From: shinya Date: Thu, 3 Jul 2025 18:46:46 +0800 Subject: [PATCH] feat: connect redis with retry --- src/lib/db.ts | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/lib/db.ts b/src/lib/db.ts index 6c05464..971ed4d 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -1,4 +1,4 @@ -/* eslint-disable no-console, @typescript-eslint/no-explicit-any */ +/* eslint-disable no-console, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */ // storage type 常量: 'localstorage' | 'database',默认 'localstorage' const STORAGE_TYPE = @@ -405,13 +405,34 @@ function getRedisClient(): RedisClientType { if (!url) { throw new Error('REDIS_URL env variable not set'); } - client = createClient({ url }); - // 提前连接,连接失败抛出错误便于定位 - client.connect().catch((err: unknown) => { - console.error('Redis connection error:', err); + // 创建客户端,配置重连策略 + client = createClient({ + url, + socket: { + // 5秒重连间隔 + reconnectStrategy: (retries: number) => { + console.log(`Redis reconnection attempt ${retries + 1}`); + return 5000; // 5秒后重试 + }, + connectTimeout: 10000, // 10秒连接超时 + }, }); + // 初始连接,带重试机制 + const connectWithRetry = async () => { + try { + await client!.connect(); + console.log('Redis connected successfully'); + } catch (err) { + console.error('Redis initial connection failed:', err); + console.log('Will retry in 5 seconds...'); + setTimeout(connectWithRetry, 5000); + } + }; + + connectWithRetry(); + (global as any)[globalKey] = client; }