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 {