From bbc6bd4b3f493170c13ad3314924cbf1f379eca4 Mon Sep 17 00:00:00 2001 From: Mohammad Ali Date: Mon, 24 Feb 2025 03:48:10 +0200 Subject: [PATCH] fix: LiveQueryServer crashes using cacheAdapter on disconnect from Redis 4 server (#9616) --- spec/RedisPubSub.spec.js | 5 ++++- src/Adapters/Cache/RedisCacheAdapter.js | 2 +- src/Adapters/PubSub/RedisPubSub.js | 15 +++++++++++++-- src/middlewares.js | 6 +++++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/spec/RedisPubSub.spec.js b/spec/RedisPubSub.spec.js index b68448ef51..868e590740 100644 --- a/spec/RedisPubSub.spec.js +++ b/spec/RedisPubSub.spec.js @@ -3,7 +3,10 @@ const RedisPubSub = require('../lib/Adapters/PubSub/RedisPubSub').RedisPubSub; describe('RedisPubSub', function () { beforeEach(function (done) { // Mock redis - const createClient = jasmine.createSpy('createClient'); + const createClient = jasmine.createSpy('createClient').and.returnValue({ + connect: jasmine.createSpy('connect').and.resolveTo(), + on: jasmine.createSpy('on'), + }); jasmine.mockLibrary('redis', 'createClient', createClient); done(); }); diff --git a/src/Adapters/Cache/RedisCacheAdapter.js b/src/Adapters/Cache/RedisCacheAdapter.js index a0345a1101..779bd573e4 100644 --- a/src/Adapters/Cache/RedisCacheAdapter.js +++ b/src/Adapters/Cache/RedisCacheAdapter.js @@ -27,7 +27,7 @@ export class RedisCacheAdapter { if (this.client.isOpen) { return; } - return this.client.connect(); + return await this.client.connect(); } async handleShutdown() { diff --git a/src/Adapters/PubSub/RedisPubSub.js b/src/Adapters/PubSub/RedisPubSub.js index cc2b3a9792..65671592c6 100644 --- a/src/Adapters/PubSub/RedisPubSub.js +++ b/src/Adapters/PubSub/RedisPubSub.js @@ -1,13 +1,24 @@ import { createClient } from 'redis'; +import { logger } from '../../logger'; function createPublisher({ redisURL, redisOptions = {} }): any { redisOptions.no_ready_check = true; - return createClient({ url: redisURL, ...redisOptions }); + const client = createClient({ url: redisURL, ...redisOptions }); + client.on('error', err => { logger.error('RedisPubSub Publisher client error', { error: err }) }); + client.on('connect', () => {}); + client.on('reconnecting', () => {}); + client.on('ready', () => {}); + return client; } function createSubscriber({ redisURL, redisOptions = {} }): any { redisOptions.no_ready_check = true; - return createClient({ url: redisURL, ...redisOptions }); + const client = createClient({ url: redisURL, ...redisOptions }); + client.on('error', err => { logger.error('RedisPubSub Subscriber client error', { error: err }) }); + client.on('connect', () => {}); + client.on('reconnecting', () => {}); + client.on('ready', () => {}); + return client; } const RedisPubSub = { diff --git a/src/middlewares.js b/src/middlewares.js index 57838b1a69..5d4504a769 100644 --- a/src/middlewares.js +++ b/src/middlewares.js @@ -534,9 +534,14 @@ export const addRateLimit = (route, config, cloud) => { store: null, }; if (route.redisUrl) { + const log = config?.loggerController || defaultLogger; const client = createClient({ url: route.redisUrl, }); + client.on('error', err => { log.error('Middlewares addRateLimit Redis client error', { error: err }) }); + client.on('connect', () => {}); + client.on('reconnecting', () => {}); + client.on('ready', () => {}); redisStore.connectionPromise = async () => { if (client.isOpen) { return; @@ -544,7 +549,6 @@ export const addRateLimit = (route, config, cloud) => { try { await client.connect(); } catch (e) { - const log = config?.loggerController || defaultLogger; log.error(`Could not connect to redisURL in rate limit: ${e}`); } };