Skip to content

Commit

Permalink
fix: LiveQueryServer crashes using cacheAdapter on disconnect from Re…
Browse files Browse the repository at this point in the history
…dis 4 server (#9616)
  • Loading branch information
Chilldev authored Feb 24, 2025
1 parent ef68fb1 commit bbc6bd4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 4 additions & 1 deletion spec/RedisPubSub.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion src/Adapters/Cache/RedisCacheAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class RedisCacheAdapter {
if (this.client.isOpen) {
return;
}
return this.client.connect();
return await this.client.connect();
}

async handleShutdown() {
Expand Down
15 changes: 13 additions & 2 deletions src/Adapters/PubSub/RedisPubSub.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
6 changes: 5 additions & 1 deletion src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,17 +534,21 @@ 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;
}
try {
await client.connect();
} catch (e) {
const log = config?.loggerController || defaultLogger;
log.error(`Could not connect to redisURL in rate limit: ${e}`);
}
};
Expand Down

0 comments on commit bbc6bd4

Please sign in to comment.