Skip to content

Commit 5d9d0d3

Browse files
James Allenluin
James Allen
authored andcommitted
fix(cluster): ensure node exists before being redirected via an ASK (#341)
1 parent 68c4ccc commit 5d9d0d3

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/cluster/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ Cluster.prototype.sendCommand = function (command, stream, node) {
424424
},
425425
ask: function (slot, key) {
426426
debug('command %s is required to ask %s:%s', command.name, key);
427+
var splitKey = key.split(':');
428+
_this.connectionPool.findOrCreate({ host: splitKey[0], port: Number(splitKey[1]) });
427429
tryConnection(false, key);
428430
},
429431
tryagain: partialTry,

test/functional/cluster.js

+32
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,38 @@ describe('cluster', function () {
452452
cluster.get('foo');
453453
});
454454
});
455+
456+
it('should be able to redirect a command to a unknown node', function (done) {
457+
var asked = false;
458+
var slotTable = [
459+
[0, 16383, ['127.0.0.1', 30002]]
460+
];
461+
var node1 = new MockServer(30001, function (argv) {
462+
if (argv[0] === 'get' && argv[1] === 'foo') {
463+
expect(asked).to.eql(true);
464+
return "bar"
465+
} else if (argv[0] === 'asking') {
466+
asked = true;
467+
}
468+
});
469+
var node2 = new MockServer(30002, function (argv) {
470+
if (argv[0] === 'cluster' && argv[1] === 'slots') {
471+
return slotTable;
472+
}
473+
if (argv[0] === 'get' && argv[1] === 'foo') {
474+
return new Error('ASK ' + calculateSlot('foo') + ' 127.0.0.1:30001');
475+
}
476+
});
477+
478+
var cluster = new Redis.Cluster([
479+
{ host: '127.0.0.1', port: '30002' }
480+
]);
481+
cluster.get('foo', function (err, res) {
482+
expect(res).to.eql('bar');
483+
cluster.disconnect();
484+
disconnect([node1, node2], done);
485+
});
486+
});
455487
});
456488

457489
describe('TRYAGAIN', function () {

0 commit comments

Comments
 (0)