fix: LiveQueryServer crashes using cacheAdapter on disconnect from Redis 4 server (#9616)

This commit is contained in:
Mohammad Ali
2025-02-24 03:48:10 +02:00
committed by GitHub
parent ef68fb1057
commit bbc6bd4b3f
4 changed files with 23 additions and 5 deletions

View File

@@ -3,7 +3,10 @@ const RedisPubSub = require('../lib/Adapters/PubSub/RedisPubSub').RedisPubSub;
describe('RedisPubSub', function () { describe('RedisPubSub', function () {
beforeEach(function (done) { beforeEach(function (done) {
// Mock redis // 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); jasmine.mockLibrary('redis', 'createClient', createClient);
done(); done();
}); });

View File

@@ -27,7 +27,7 @@ export class RedisCacheAdapter {
if (this.client.isOpen) { if (this.client.isOpen) {
return; return;
} }
return this.client.connect(); return await this.client.connect();
} }
async handleShutdown() { async handleShutdown() {

View File

@@ -1,13 +1,24 @@
import { createClient } from 'redis'; import { createClient } from 'redis';
import { logger } from '../../logger';
function createPublisher({ redisURL, redisOptions = {} }): any { function createPublisher({ redisURL, redisOptions = {} }): any {
redisOptions.no_ready_check = true; 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 { function createSubscriber({ redisURL, redisOptions = {} }): any {
redisOptions.no_ready_check = true; 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 = { const RedisPubSub = {

View File

@@ -534,9 +534,14 @@ export const addRateLimit = (route, config, cloud) => {
store: null, store: null,
}; };
if (route.redisUrl) { if (route.redisUrl) {
const log = config?.loggerController || defaultLogger;
const client = createClient({ const client = createClient({
url: route.redisUrl, 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 () => { redisStore.connectionPromise = async () => {
if (client.isOpen) { if (client.isOpen) {
return; return;
@@ -544,7 +549,6 @@ export const addRateLimit = (route, config, cloud) => {
try { try {
await client.connect(); await client.connect();
} catch (e) { } catch (e) {
const log = config?.loggerController || defaultLogger;
log.error(`Could not connect to redisURL in rate limit: ${e}`); log.error(`Could not connect to redisURL in rate limit: ${e}`);
} }
}; };