Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Apr 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});