Skip to content

Commit 91998e3

Browse files
committed
fix(CLUSTER): fix cluster not disconnected when called disconnect method (#281)
* fix(CLUSTER): fix cluster not disconnected when called disconnect method Related issue: #277
1 parent 055429b commit 91998e3

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

lib/cluster/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function Cluster(startupNodes, options) {
5757

5858
var _this = this;
5959
this.connectionPool.on('-node', function (redis) {
60-
if (_this.subscriber === redis) {
60+
if (_this.status !== 'disconnecting' && _this.subscriber === redis) {
6161
_this.selectSubscriber();
6262
}
6363
_this.emit('-node', redis);
@@ -193,6 +193,8 @@ Cluster.prototype.connect = function () {
193193
* @public
194194
*/
195195
Cluster.prototype.disconnect = function (reconnect) {
196+
this.setStatus('disconnecting');
197+
196198
if (!reconnect) {
197199
this.manuallyClosing = true;
198200
}

lib/redis.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,11 @@ Redis.prototype.disconnect = function (reconnect) {
307307
clearTimeout(this.reconnectTimeout);
308308
this.reconnectTimeout = null;
309309
}
310-
this.connector.disconnect();
310+
if (this.status === 'wait') {
311+
eventHandler.closeHandler(this)();
312+
} else {
313+
this.connector.disconnect();
314+
}
311315
};
312316

313317
/**

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "NODE_ENV=test mocha",
8-
"test:cov": "NODE_ENV=test DEBUG=ioredis:* node ./node_modules/istanbul/lib/cli.js cover --preserve-comments ./node_modules/mocha/bin/_mocha -- -R spec",
8+
"test:cov": "NODE_ENV=test node ./node_modules/istanbul/lib/cli.js cover --preserve-comments ./node_modules/mocha/bin/_mocha -- -R spec",
99
"generate-docs": "jsdoc2md lib/redis.js lib/cluster/index.js lib/commander.js > API.md",
1010
"build": "node tools/build > commands.js",
1111
"bench": "matcha benchmarks/*.js"

test/functional/cluster.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ describe('cluster', function () {
12191219
expect(cluster.nodes('master')).to.have.lengthOf(2);
12201220
expect(cluster.nodes('slave')).to.have.lengthOf(1);
12211221

1222-
cluster.on('-node', function () {
1222+
cluster.once('-node', function () {
12231223
expect(cluster.nodes()).to.have.lengthOf(2);
12241224
expect(cluster.nodes('all')).to.have.lengthOf(2);
12251225
expect(cluster.nodes('master')).to.have.lengthOf(1);

test/functional/lazy_connect.js

+8
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@ describe('lazy connect', function () {
2626
});
2727
});
2828
});
29+
30+
it('should be able to disconnect', function (done) {
31+
var redis = new Redis({ lazyConnect: true });
32+
redis.on('end', function () {
33+
done();
34+
});
35+
redis.disconnect();
36+
});
2937
});

0 commit comments

Comments
 (0)