Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit c24d65a

Browse files
committed
fix: handle redis connection errors
Instead of throwing an error I've implemented retry_strategy and an interval to check if redis is (again) available. If not the service will return uncached response.
1 parent b0dc8de commit c24d65a

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/hooks/redis.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export function before(options) { // eslint-disable-line no-unused-vars
1515

1616
return new Promise(resolve => {
1717
const client = hook.app.get('redisClient');
18+
19+
if (!client) {
20+
resolve(hook);
21+
}
22+
1823
const path = parsePath(hook, options);
1924

2025
client.get(path, (err, reply) => {
@@ -49,6 +54,11 @@ export function after(options) { // eslint-disable-line no-unused-vars
4954
if (!hook.result.cache.cached) {
5055
const duration = hook.result.cache.duration || options.defaultDuration;
5156
const client = hook.app.get('redisClient');
57+
58+
if (!client) {
59+
resolve(hook);
60+
}
61+
5262
const path = parsePath(hook, options);
5363

5464
// adding a cache object

src/redisClient.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
import redis from 'redis';
2+
import chalk from 'chalk';
23

34
export default function redisClient() { // eslint-disable-line no-unused-vars
4-
this.set('redisClient', redis.createClient(this.get('redis')));
5+
const app = this;;
6+
const cacheOptions = app.get('redisCache') || {};
7+
const retryInterval = cacheOptions.retryInterval || 10000;
8+
const redisOptions = Object.assign({}, this.get('redis'), {
9+
retry_strategy: function (options) { // eslint-disable-line camelcase
10+
app.set('redisClient', undefined);
11+
if (cacheOptions.env !== 'test') {
12+
console.log(`${chalk.yellow('[redis]')} not connected`);
13+
}
14+
return retryInterval;
15+
}
16+
});
17+
const client = redis.createClient(redisOptions);
18+
19+
client.on('ready', () => {
20+
app.set('redisClient', client);
21+
if (cacheOptions.env !== 'test') {
22+
console.log(`${chalk.green('[redis]')} connected`);
23+
}
24+
});
525
return this;
626
}

test/client.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect } from 'chai';
2+
import RedisClient from '../src/redisClient';
3+
4+
const app = {
5+
get: function(key) {
6+
return this[key];
7+
},
8+
set: function(key, val) {
9+
this[key] = val;
10+
},
11+
configure: function(fn) {
12+
fn.call(this);
13+
}
14+
};
15+
16+
describe('redisClient', () => {
17+
it('should not exist if redis not connected', (done) => {
18+
app.set('redis', { port: 1234 }); // force connection error
19+
app.configure(RedisClient);
20+
setTimeout(function () {
21+
expect(app.get('redisClient')).to.not.exist;
22+
done();
23+
}, 500);
24+
});
25+
it('should be available if redis connected', (done) => {
26+
app.set('redis', { port: 6379 }); // restore default
27+
app.configure(RedisClient);
28+
setTimeout(function () {
29+
expect(app.get('redisClient')).to.exist;
30+
done();
31+
}, 500);
32+
});
33+
});

0 commit comments

Comments
 (0)