Skip to content

Commit

Permalink
improve(RedisUtils): Log on lost redis connection (#1784)
Browse files Browse the repository at this point in the history
To aid debugging.
  • Loading branch information
pxrl authored Aug 26, 2024
1 parent aa1ec0d commit 1457eed
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/utils/RedisUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,19 @@ const redisClients: { [url: string]: RedisClient } = {};
export async function getRedis(logger?: winston.Logger, url = REDIS_URL): Promise<RedisClient | undefined> {
if (!redisClients[url]) {
let redisClient: _RedisClient | undefined = undefined;
const reconnectStrategy = (retries: number): number | Error => {
// Just implement the default reconnection strategy:
// https://github.com/redis/node-redis/blob/HEAD/docs/client-configuration.md#reconnect-strategy
const delay = Math.min(retries * 50, 500);
logger?.debug({
at: "RedisUtils",
message: `Lost redis connection, retrying in ${delay} ms.`,
});
return delay;
};

try {
redisClient = createClient({ url });
redisClient = createClient({ url, socket: { reconnectStrategy } });
await redisClient.connect();
logger?.debug({
at: "RedisUtils#getRedis",
Expand Down

0 comments on commit 1457eed

Please sign in to comment.