Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading