fix: Improve error handling when redis connection is lost

This commit is contained in:
Tom Moor
2020-10-20 07:41:17 -07:00
parent 4103f53f2a
commit 15337b5bdf
2 changed files with 19 additions and 2 deletions

View File

@ -27,6 +27,15 @@ io.adapter(
})
);
io.of("/").adapter.on("error", (err) => {
if (err.name === "MaxRetriesPerRequestError") {
console.error(`Redis error: ${err.message}. Shutting down now.`);
throw err;
} else {
console.error(`Redis error: ${err.message}`);
}
});
SocketAuth(io, {
authenticate: async (socket, data, callback) => {
const { token } = data;

View File

@ -1,8 +1,16 @@
// @flow
import Redis from "ioredis";
const client = new Redis(process.env.REDIS_URL);
const subscriber = new Redis(process.env.REDIS_URL);
const options = {
maxRetriesPerRequest: 20,
retryStrategy(times) {
console.warn(`Retrying redis connection: attempt ${times}`);
return Math.min(times * 100, 3000);
},
};
const client = new Redis(process.env.REDIS_URL, options);
const subscriber = new Redis(process.env.REDIS_URL, options);
// More than the default of 10 listeners is expected for the amount of queues
// we're running. Increase the max here to prevent a warning in the console: