Skip to content

Commit 491546d

Browse files
committed
fix: don't add cluster.info to the failover queue before ready
That prevent multiple "ready" event being triggered after the cluster is connected.
1 parent 0d7cc7e commit 491546d

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/cluster/connection_pool.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ ConnectionPool.prototype.findOrCreate = function (node, readOnly) {
4646
redis = this.nodes.all[node.key];
4747
if (redis.options.readOnly !== readOnly) {
4848
redis.options.readOnly = readOnly;
49+
debug('Change role of %s to %s', node.key, readOnly ? 'slave' : 'master')
4950
redis[readOnly ? 'readonly' : 'readwrite']().catch(_.noop);
5051
if (readOnly) {
5152
delete this.nodes.master[node.key];
@@ -56,6 +57,7 @@ ConnectionPool.prototype.findOrCreate = function (node, readOnly) {
5657
}
5758
}
5859
} else {
60+
debug('Connecting to %s as %s', node.key, readOnly ? 'slave' : 'master');
5961
redis = new Redis(_.defaults({
6062
retryStrategy: null,
6163
readOnly: readOnly
@@ -122,7 +124,6 @@ ConnectionPool.prototype.reset = function (nodes) {
122124
});
123125
Object.keys(newNodes).forEach(function (key) {
124126
var node = newNodes[key];
125-
debug('Connecting to %s as %s', key, node.readOnly ? 'slave' : 'master');
126127
_this.findOrCreate(node, node.readOnly);
127128
});
128129
};

lib/cluster/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ Cluster.prototype.connect = function () {
150150
if (this.options.enableReadyCheck) {
151151
this._readyCheck(function (err, fail) {
152152
if (err || fail) {
153-
this.disconnect(true);
153+
debug('Ready check failed (%s). Reconnecting...', err || fail)
154+
if (this.status === 'connect') {
155+
this.disconnect(true);
156+
}
154157
} else {
155158
readyHandler.call(this);
156159
}
@@ -195,7 +198,9 @@ Cluster.prototype._handleCloseEvent = function () {
195198
this.reconnectTimeout = setTimeout(function () {
196199
this.reconnectTimeout = null;
197200
debug('Cluster is disconnected. Retrying after %dms', retryDelay);
198-
this.connect().catch(_.noop);
201+
this.connect().catch(function (err) {
202+
debug('Got error %s when reconnecting. Ignoring...', err);
203+
});
199204
}.bind(this), retryDelay);
200205
} else {
201206
this.setStatus('end');
@@ -218,6 +223,7 @@ Cluster.prototype.disconnect = function (reconnect) {
218223
if (this.reconnectTimeout) {
219224
clearTimeout(this.reconnectTimeout);
220225
this.reconnectTimeout = null;
226+
debug('Canceled reconnecting attempts');
221227
}
222228

223229
if (status === 'wait') {
@@ -570,7 +576,7 @@ Cluster.prototype.handleError = function (error, ttl, handlers) {
570576
timeout: this.options.retryDelayOnClusterDown,
571577
callback: this.refreshSlotsCache.bind(this)
572578
});
573-
} else if (error.message === utils.CONNECTION_CLOSED_ERROR_MSG && this.options.retryDelayOnFailover > 0) {
579+
} else if (error.message === utils.CONNECTION_CLOSED_ERROR_MSG && this.options.retryDelayOnFailover > 0 && this.status === 'ready') {
574580
this.delayQueue.push('failover', handlers.connectionClosed, {
575581
timeout: this.options.retryDelayOnFailover,
576582
callback: this.refreshSlotsCache.bind(this)

0 commit comments

Comments
 (0)