Skip to content

Commit 0dcb768

Browse files
committed
fix(cluster): fix not connecting to the unknown nodes
1 parent a5b64ad commit 0dcb768

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

lib/cluster/connection_pool.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ConnectionPool.prototype.findOrCreate = function (node, readOnly) {
2323
node.port = node.port || 6379;
2424
node.host = node.host || '127.0.0.1';
2525
node.key = node.key || node.host + ':' + node.port;
26+
readOnly = Boolean(readOnly);
2627

2728
if (this.specifiedOptions[node.key]) {
2829
_.assign(node, this.specifiedOptions[node.key]);

lib/cluster/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ function Cluster(startupNodes, options) {
7171

7272
var _this = this;
7373
this.connectionPool.on('-node', function (redis) {
74-
_this.emit('-node');
7574
if (_this.subscriber === redis) {
7675
_this.selectSubscriber();
7776
}
77+
_this.emit('-node');
7878
});
7979
this.connectionPool.on('+node', function (redis) {
8080
_this.emit('+node', redis);
@@ -84,7 +84,6 @@ function Cluster(startupNodes, options) {
8484
});
8585

8686
this.slots = [];
87-
8887
this.retryAttempts = 0;
8988

9089
this.resetOfflineQueue();
@@ -430,6 +429,8 @@ Cluster.prototype.sendCommand = function (command, stream, node) {
430429
} else {
431430
_this.slots[slot] = [key];
432431
}
432+
var splitKey = key.split(':');
433+
_this.connectionPool.findOrCreate({ host: splitKey[0], port: Number(splitKey[1]) });
433434
tryConnection();
434435
_this.refreshSlotsCache();
435436
},

test/functional/cluster.js

+30
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,36 @@ describe('cluster', function () {
338338
});
339339
});
340340

341+
it('should be able to redirect a command to a unknown node', function (done) {
342+
var slotTable = [
343+
[0, 16383, ['127.0.0.1', 30001]]
344+
];
345+
var node1 = new MockServer(30001, function (argv) {
346+
if (argv[0] === 'cluster' && argv[1] === 'slots') {
347+
return slotTable;
348+
}
349+
if (argv[0] === 'get' && argv[1] === 'foo') {
350+
return new Error('MOVED ' + utils.calcSlot('foo') + ' 127.0.0.1:30002');
351+
}
352+
});
353+
var node2 = new MockServer(30002, function (argv) {
354+
if (argv[0] === 'cluster' && argv[1] === 'slots') {
355+
return slotTable;
356+
}
357+
if (argv[0] === 'get' && argv[1] === 'foo') {
358+
return 'bar';
359+
}
360+
});
361+
var cluster = new Redis.Cluster([
362+
{ host: '127.0.0.1', port: '30001' }
363+
], { lazyConnect: false });
364+
cluster.get('foo', function (err, res) {
365+
expect(res).to.eql('bar');
366+
cluster.disconnect();
367+
disconnect([node1, node2], done);
368+
});
369+
});
370+
341371
it('should auto redirect the command within a pipeline', function (done) {
342372
var moved = false;
343373
var times = 0;

0 commit comments

Comments
 (0)