mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-05-14 00:39:37 +08:00
feat: connect redis with retry
This commit is contained in:
@@ -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'
|
// storage type 常量: 'localstorage' | 'database',默认 'localstorage'
|
||||||
const STORAGE_TYPE =
|
const STORAGE_TYPE =
|
||||||
@@ -405,13 +405,34 @@ function getRedisClient(): RedisClientType {
|
|||||||
if (!url) {
|
if (!url) {
|
||||||
throw new Error('REDIS_URL env variable not set');
|
throw new Error('REDIS_URL env variable not set');
|
||||||
}
|
}
|
||||||
client = createClient({ url });
|
|
||||||
|
|
||||||
// 提前连接,连接失败抛出错误便于定位
|
// 创建客户端,配置重连策略
|
||||||
client.connect().catch((err: unknown) => {
|
client = createClient({
|
||||||
console.error('Redis connection error:', err);
|
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;
|
(global as any)[globalKey] = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user