Skip to content

Commit

Permalink
Merge pull request #215 from raveljs/feature/214
Browse files Browse the repository at this point in the history
Proper fix for #214
  • Loading branch information
Ghnuberath authored Oct 19, 2017
2 parents cebfdc1 + 95de13c commit 3ae008d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
3 changes: 2 additions & 1 deletion documentation.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Ravel API
version: 0.22.1
version: 0.22.2
versions:
- 0.22.1
- 0.22.0
- 0.21.1
- 0.21.0
Expand Down
13 changes: 7 additions & 6 deletions lib/util/kvstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ const redis = require('redis');
*/
function retryStrategy (ravelInstance) {
return function (options) {
if (options.error && options.error.code === 'ECONNREFUSED') {
const code = options.error ? options.error.code : 'Reason Unknown';
if (code === 'ECONNREFUSED') {
// End reconnecting on a specific error and flush all commands with a individual error
ravelInstance.log.error(`Lost connection to redis: ${options.error.code}.`);
return new ApplicationError.General(`Lost connection to redis: ${options.error.code}.`);
ravelInstance.log.error(`Lost connection to redis: ${code}.`);
return new ApplicationError.General(`Lost connection to redis: ${code}.`);
} else if (options.attempt > ravelInstance.get('redis max retries')) {
ravelInstance.log.error(`Lost connection to redis: ${options.error.code}. Max retry attempts exceeded.`);
ravelInstance.log.error(`Lost connection to redis: ${code}. Max retry attempts exceeded.`);
// End reconnecting with built in error
return new ApplicationError.General(
`Lost connection to redis: ${options.error.code}. Max retry attempts reached.`);
`Lost connection to redis: ${code}. Max retry attempts reached.`);
} else {
const time = Math.pow(options.attempt, 2) * 100;
ravelInstance.log.error(`Lost connection to redis: ${options.error.code}. Reconnecting in ${time} milliseconds.`);
ravelInstance.log.error(`Lost connection to redis: ${code}. Reconnecting in ${time} milliseconds.`);
// reconnect after
return time;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ravel",
"version": "0.22.1",
"version": "0.22.2",
"author": "Sean McIntyre <[email protected]>",
"description": "Ravel Rapid Application Development Framework",
"engines": {
Expand Down
26 changes: 26 additions & 0 deletions test/util/test-kvstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ describe('Ravel', () => {
done();
});

it('should return an error when the maximum number of retries is exceeded without a reason', (done) => {
const retryStrategy = require('../../lib/util/kvstore').retryStrategy(Ravel);
expect(retryStrategy).to.be.a('function');
Ravel.set('redis max retries', 10);
Ravel[coreSymbols.parametersLoaded] = true;
const options = {
error: undefined,
attempt: Ravel.get('redis max retries') + 1
};
expect(retryStrategy(options)).to.be.an.instanceof(Ravel.ApplicationError.General);
done();
});

it('should return the time to the next reconnect if the number of retries does not exceed the maximum', (done) => {
const retryStrategy = require('../../lib/util/kvstore').retryStrategy(Ravel);
expect(retryStrategy).to.be.a('function');
Expand All @@ -75,6 +88,19 @@ describe('Ravel', () => {
expect(retryStrategy(options)).to.equal(100);
done();
});

it('should return the time to the next reconnect if the number of retries does not exceed the maximum, and there was no reason for the error', (done) => {
const retryStrategy = require('../../lib/util/kvstore').retryStrategy(Ravel);
expect(retryStrategy).to.be.a('function');
Ravel.set('redis max retries', 10);
Ravel[coreSymbols.parametersLoaded] = true;
const options = {
error: undefined,
attempt: 1
};
expect(retryStrategy(options)).to.equal(100);
done();
});
});
});
});

0 comments on commit 3ae008d

Please sign in to comment.