-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from raveljs/feature/84
Using new node-redis reconnection stuff properly now. Better logging.
- Loading branch information
Showing
5 changed files
with
69 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,45 @@ | ||
'use strict'; | ||
|
||
const ApplicationError = require('./application_error'); | ||
const redis = require('redis'); | ||
const events = require('events'); | ||
|
||
function retryStrategy(ravelInstance) { | ||
return function(options) { | ||
if (options.error.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}.`); | ||
} else if (options.attempt > ravelInstance.get('redis max retries')) { | ||
ravelInstance.log.error(`Lost connection to redis: ${options.error.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.`); | ||
} else { | ||
const time = Math.pow(options.attempt, 2)*100; | ||
ravelInstance.log.error(`Lost connection to redis: ${options.error.code}. Reconnecting in ${time} milliseconds.`); | ||
// reconnect after | ||
return time; | ||
} | ||
}; | ||
}; | ||
|
||
/** | ||
* Abstraction for redis-like data store | ||
* Handles reconnections gracefully - something that node-redis claims to do but doesn't, actually. | ||
* | ||
* With help from https://www.exratione.com/2013/01/nodejs-connections-will-end-close-and-otherwise-blow-up/ | ||
*/ | ||
module.exports = function(ravelInstance) { | ||
|
||
const client = {}; | ||
|
||
const kvstore = new events.EventEmitter(); | ||
|
||
function replace(first) { | ||
if (first !== true) { | ||
kvstore.emit('replace'); | ||
} | ||
if (client.end) { | ||
client.closing = true; | ||
client.end(); | ||
} | ||
const newClient = redis.createClient(ravelInstance.get('redis port'), ravelInstance.get('redis host'), { | ||
'no_ready_check': true | ||
const client = redis.createClient( | ||
ravelInstance.get('redis port'), | ||
ravelInstance.get('redis host'), | ||
{ | ||
'no_ready_check': true, | ||
'retry_strategy': retryStrategy(ravelInstance) | ||
}); | ||
if (ravelInstance.get('redis password')) { | ||
newClient.auth(ravelInstance.get('redis password')); | ||
} | ||
newClient.once('end', replace); | ||
newClient.once('error', replace); | ||
//copy over redis methods to client reference | ||
//TODO add key prefixes | ||
Object.assign(client, newClient); | ||
//TODO handle pub/sub | ||
} | ||
replace(true); | ||
if (ravelInstance.get('redis password')) { | ||
client.auth(ravelInstance.get('redis password')); | ||
}; | ||
|
||
return client; | ||
}; | ||
|
||
module.exports.retryStrategy = retryStrategy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "ravel", | ||
"version": "0.11.8", | ||
"version": "0.11.9", | ||
"author": "Sean McIntyre <[email protected]>", | ||
"description": "Ravel Rapid Application Development Framework", | ||
"keywords": [ | ||
|
@@ -70,14 +70,13 @@ | |
"sinon":"1.17.3", | ||
"sinon-chai":"2.8.0", | ||
"supertest":"1.2.0", | ||
"eslint": "1.10.3", | ||
"eslint": "2.8.0", | ||
|
||
"codeclimate-test-reporter": "0.3.1", | ||
"del": "2.2.0", | ||
"gulp": "3.9.1", | ||
"gulp-env": "0.4.0", | ||
"istanbul": "0.4.3", | ||
"isparta": "4.0.0", | ||
"gulp-istanbul": "0.10.4", | ||
"gulp-eslint": "2.0.0", | ||
"gulp-load-plugins": "1.2.2", | ||
|
@@ -88,7 +87,7 @@ | |
"gulp-sourcemaps": "1.6.0", | ||
"gulp-babel":"6.1.2", | ||
"babel":"6.5.2", | ||
"babel-eslint": "5.0.1", | ||
"babel-eslint": "6.0.4", | ||
"babel-plugin-transform-decorators-legacy": "1.3.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters