Skip to content

Commit 0769215

Browse files
authoredFeb 24, 2025
fix: LiveQueryServer crashes using cacheAdapter on disconnect from Redis 4 server (#9615)
1 parent 79204f7 commit 0769215

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed
 

Diff for: ‎spec/RedisPubSub.spec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ const RedisPubSub = require('../lib/Adapters/PubSub/RedisPubSub').RedisPubSub;
33
describe('RedisPubSub', function () {
44
beforeEach(function (done) {
55
// Mock redis
6-
const createClient = jasmine.createSpy('createClient');
6+
const createClient = jasmine.createSpy('createClient').and.returnValue({
7+
connect: jasmine.createSpy('connect').and.resolveTo(),
8+
on: jasmine.createSpy('on'),
9+
});
710
jasmine.mockLibrary('redis', 'createClient', createClient);
811
done();
912
});

Diff for: ‎src/Adapters/Cache/RedisCacheAdapter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class RedisCacheAdapter {
2727
if (this.client.isOpen) {
2828
return;
2929
}
30-
return this.client.connect();
30+
return await this.client.connect();
3131
}
3232

3333
async handleShutdown() {

Diff for: ‎src/Adapters/PubSub/RedisPubSub.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import { createClient } from 'redis';
2+
import { logger } from '../../logger';
23

34
function createPublisher({ redisURL, redisOptions = {} }): any {
45
redisOptions.no_ready_check = true;
5-
return createClient({ url: redisURL, ...redisOptions });
6+
const client = createClient({ url: redisURL, ...redisOptions });
7+
client.on('error', err => { logger.error('RedisPubSub Publisher client error', { error: err }) });
8+
client.on('connect', () => {});
9+
client.on('reconnecting', () => {});
10+
client.on('ready', () => {});
11+
return client;
612
}
713

814
function createSubscriber({ redisURL, redisOptions = {} }): any {
915
redisOptions.no_ready_check = true;
10-
return createClient({ url: redisURL, ...redisOptions });
16+
const client = createClient({ url: redisURL, ...redisOptions });
17+
client.on('error', err => { logger.error('RedisPubSub Subscriber client error', { error: err }) });
18+
client.on('connect', () => {});
19+
client.on('reconnecting', () => {});
20+
client.on('ready', () => {});
21+
return client;
1122
}
1223

1324
const RedisPubSub = {

Diff for: ‎src/middlewares.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -533,17 +533,21 @@ export const addRateLimit = (route, config, cloud) => {
533533
store: null,
534534
};
535535
if (route.redisUrl) {
536+
const log = config?.loggerController || defaultLogger;
536537
const client = createClient({
537538
url: route.redisUrl,
538539
});
540+
client.on('error', err => { log.error('Middlewares addRateLimit Redis client error', { error: err }) });
541+
client.on('connect', () => {});
542+
client.on('reconnecting', () => {});
543+
client.on('ready', () => {});
539544
redisStore.connectionPromise = async () => {
540545
if (client.isOpen) {
541546
return;
542547
}
543548
try {
544549
await client.connect();
545550
} catch (e) {
546-
const log = config?.loggerController || defaultLogger;
547551
log.error(`Could not connect to redisURL in rate limit: ${e}`);
548552
}
549553
};

0 commit comments

Comments
 (0)