I'm tinkering my API on node.js with caching, supporting graceful degradation. While testing app with redis container being disabled, node-redis is trying to reconnect every five seconds. The problem is that reconnection attempts can not be set. It seems like it can be set only in ioredis library. So, every five seconds I see these amount of exception on my terminal. Of course I can just skip logging part, but my app still processing those errors, and filtering this type of exception will only add tasks number. I want just one-three attempts, not a hundreds.
And also "ready" event is not what I personally expect, why would it be emitted while not opened connection?
static async init() {
if (!process.env.REDIS_HOST || !process.env.REDIS_PORT || !process.env.REDIS_TTL) {
Logger.error('Redis configuration missing - running without cache');
return;
}
try {
this.#client = await createClient({
socket: {
host: process.env.REDIS_HOST,
port: Number(process.env.REDIS_PORT),
connectTimeout: 1000,
reconnectStrategy: 5000
},
disableOfflineQueue: false,
});
this.#attachHandlers();
await this.#client.connect();
} catch (err) {
Logger.warn(`Redis init failed: ${err}. Cache disabled`);
}
}
static #attachHandlers() {
this.#client.on("error", (err) => {
console.log(performance.now());
Logger.warn(`Redis error: ${err.message}`)
});
this.#client.on("end", () => Logger.info(`Redis connection has been closed`));
this.#client.on("connection", () => Logger.info(`Initiating a connection to the Redis`));
this.#client.on("reconnecting", () => Logger.info(`Reconnecting to the Redis`));
this.#client.on("ready", () => Logger.info(`Redis connection established`));
}
- Tried to set
connectTimeoutbut even with 100 milliseconds window, tens of exceptions were emitted - Property
reconnectStrategyis only used for setting timeout for particular window, idk how to use any how - Checked official docs, useless at that point