fix: Redis 连接等待就绪信号
- enableReadyCheck: true 等待 READY 信号 - 连接超时 12 秒 - 错误时正确断开连接 - 测试连接现在会返回具体错误信息
This commit is contained in:
@@ -24,9 +24,9 @@ function buildRedisOptions(opts: ConnectionOptions): RedisOptions {
|
|||||||
username: opts.username || undefined,
|
username: opts.username || undefined,
|
||||||
password: opts.password || undefined,
|
password: opts.password || undefined,
|
||||||
stringNumbers: true,
|
stringNumbers: true,
|
||||||
enableReadyCheck: false,
|
enableReadyCheck: true,
|
||||||
family: 0,
|
family: 0,
|
||||||
connectTimeout: 30000,
|
connectTimeout: 10000,
|
||||||
retryStrategy(times: number) {
|
retryStrategy(times: number) {
|
||||||
if (times > 3) return null
|
if (times > 3) return null
|
||||||
return Math.min(times * 200, 1000)
|
return Math.min(times * 200, 1000)
|
||||||
@@ -52,8 +52,27 @@ export async function connect(id: string, opts: ConnectionOptions): Promise<void
|
|||||||
await disconnect(id)
|
await disconnect(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
const redis = new Redis(buildRedisOptions(opts))
|
const redis = new Redis(buildRedisOptions(opts))
|
||||||
|
|
||||||
|
redis.once('ready', () => {
|
||||||
clients.set(id, redis)
|
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<void> {
|
export async function disconnect(id: string): Promise<void> {
|
||||||
|
|||||||
Reference in New Issue
Block a user