Skip to content

Commit

Permalink
feat: report error on Sentinel connection refused (#445) (#446)
Browse files Browse the repository at this point in the history
* Report error on Sentinel connection refused

* Removed duplicate line, oops

* Errors emitted instead of called back

* Spacing

* Fixed error reporting to event reporting
  • Loading branch information
SamBergeron authored and luin committed Apr 16, 2017
1 parent 539fe41 commit 286a5bc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/connectors/sentinel_connector.js
Expand Up @@ -28,7 +28,7 @@ SentinelConnector.prototype.check = function (info) {
return true;
};

SentinelConnector.prototype.connect = function (callback) {
SentinelConnector.prototype.connect = function (callback, eventEmitter) {
this.connecting = true;
this.retryAttempts = 0;

Expand All @@ -48,20 +48,24 @@ SentinelConnector.prototype.connect = function (callback) {
if (_this.currentPoint === _this.sentinels.length) {
_this.currentPoint = -1;

var error;
var retryDelay;
if (typeof _this.options.sentinelRetryStrategy === 'function') {
retryDelay = _this.options.sentinelRetryStrategy(++_this.retryAttempts);
}

if (typeof retryDelay !== 'number') {
debug('All sentinels are unreachable and retry is disabled, emitting error...');
var error = 'All sentinels are unreachable.';
error = 'All sentinels are unreachable.';
if (lastError) {
error += ' Last error: ' + lastError.message;
}
return callback(new Error(error));
}
debug('All sentinels are unreachable. Retrying from scratch after %d', retryDelay);
error = 'All sentinels are unreachable, retrying...';
setTimeout(connectToNext, retryDelay);
eventEmitter('error', new Error(error));
return;
}

Expand All @@ -76,11 +80,13 @@ SentinelConnector.prototype.connect = function (callback) {
callback(null, _this.stream);
} else if (err) {
debug('failed to connect to sentinel %s:%s because %s', endpoint.host, endpoint.port, err);
eventEmitter('sentinelError', new Error('failed to connect to sentinel '+endpoint.host+':'+endpoint.port+' because '+err));
lastError = err;
connectToNext();
} else {
debug('connected to sentinel %s:%s successfully, but got a invalid reply: %s',
endpoint.host, endpoint.port, resolved);
eventEmitter('sentinelError', new Error('connected to sentinel '+endpoint.host+':'+endpoint.port+' successfully, but got a invalid reply: '+resolved));
connectToNext();
}
});
Expand Down
2 changes: 2 additions & 0 deletions lib/redis.js
Expand Up @@ -311,6 +311,8 @@ Redis.prototype.connect = function (callback) {
};
_this.once(CONNECT_EVENT, connectionConnectHandler);
_this.once('close', connectionCloseHandler);
}, function(type, err) {
_this.silentEmit(type, err);
});
}.bind(this)).nodeify(callback);
};
Expand Down

0 comments on commit 286a5bc

Please sign in to comment.