Skip to content

Commit

Permalink
fix(CLUSTER): fix cluster not disconnected when called disconnect met…
Browse files Browse the repository at this point in the history
…hod (#281)

* fix(CLUSTER): fix cluster not disconnected when called disconnect method

Related issue: #277
  • Loading branch information
luin committed Apr 10, 2016
1 parent 055429b commit 91998e3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/cluster/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function Cluster(startupNodes, options) {

var _this = this;
this.connectionPool.on('-node', function (redis) {
if (_this.subscriber === redis) {
if (_this.status !== 'disconnecting' && _this.subscriber === redis) {
_this.selectSubscriber();
}
_this.emit('-node', redis);
Expand Down Expand Up @@ -193,6 +193,8 @@ Cluster.prototype.connect = function () {
* @public
*/
Cluster.prototype.disconnect = function (reconnect) {
this.setStatus('disconnecting');

if (!reconnect) {
this.manuallyClosing = true;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,11 @@ Redis.prototype.disconnect = function (reconnect) {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = null;
}
this.connector.disconnect();
if (this.status === 'wait') {
eventHandler.closeHandler(this)();
} else {
this.connector.disconnect();
}
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "NODE_ENV=test mocha",
"test:cov": "NODE_ENV=test DEBUG=ioredis:* node ./node_modules/istanbul/lib/cli.js cover --preserve-comments ./node_modules/mocha/bin/_mocha -- -R spec",
"test:cov": "NODE_ENV=test node ./node_modules/istanbul/lib/cli.js cover --preserve-comments ./node_modules/mocha/bin/_mocha -- -R spec",
"generate-docs": "jsdoc2md lib/redis.js lib/cluster/index.js lib/commander.js > API.md",
"build": "node tools/build > commands.js",
"bench": "matcha benchmarks/*.js"
Expand Down
2 changes: 1 addition & 1 deletion test/functional/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ describe('cluster', function () {
expect(cluster.nodes('master')).to.have.lengthOf(2);
expect(cluster.nodes('slave')).to.have.lengthOf(1);

cluster.on('-node', function () {
cluster.once('-node', function () {
expect(cluster.nodes()).to.have.lengthOf(2);
expect(cluster.nodes('all')).to.have.lengthOf(2);
expect(cluster.nodes('master')).to.have.lengthOf(1);
Expand Down
8 changes: 8 additions & 0 deletions test/functional/lazy_connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ describe('lazy connect', function () {
});
});
});

it('should be able to disconnect', function (done) {
var redis = new Redis({ lazyConnect: true });
redis.on('end', function () {
done();
});
redis.disconnect();
});
});

0 comments on commit 91998e3

Please sign in to comment.