From ec85f13d5a146535abaa7965c0d2d42f3f0d14e0 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 24 May 2021 16:30:24 +0100 Subject: [PATCH] Adding an initial connection timeout of 5 seconds which after it will retry again. --- packages/auth/src/redis/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/auth/src/redis/index.js b/packages/auth/src/redis/index.js index 0a21966301..a3cb6385e6 100644 --- a/packages/auth/src/redis/index.js +++ b/packages/auth/src/redis/index.js @@ -4,6 +4,7 @@ const Redis = env.isTest() ? require("ioredis-mock") : require("ioredis") const { addDbPrefix, removeDbPrefix, getRedisOptions } = require("./utils") const RETRY_PERIOD_MS = 2000 +const STARTUP_TIMEOUT_MS = 5000 const CLUSTERED = false // for testing just generate the client once @@ -15,7 +16,10 @@ let CLIENT = env.isTest() ? new Redis(getRedisOptions()) : null * will return the ioredis client which will be ready to use. */ function init() { + let timeout function errorOccurred(err) { + // always clear this on error + clearTimeout(timeout) CONNECTED = false console.error("Redis connection failed - " + err) setTimeout(() => { @@ -26,6 +30,14 @@ function init() { if (env.isTest() || (CLIENT && CONNECTED)) { return } + // start the timer - only allowed 5 seconds to connect + timeout = setTimeout(() => { + if (!CONNECTED) { + errorOccurred() + } + }, STARTUP_TIMEOUT_MS) + + // disconnect any lingering client if (CLIENT) { CLIENT.disconnect() } @@ -35,6 +47,7 @@ function init() { } else { CLIENT = new Redis(opts) } + // attach handlers CLIENT.on("end", err => { errorOccurred(err) }) @@ -42,6 +55,7 @@ function init() { errorOccurred(err) }) CLIENT.on("connect", () => { + clearTimeout(timeout) CONNECTED = true }) }