Skip to content

Commit a2a30b8

Browse files
author
Braydon Fuller
committed
bitcoind: start tryAllClients with the current round-robin index
1 parent 6ac9125 commit a2a30b8

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lib/services/bitcoind.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,10 @@ Bitcoin.prototype._resetCaches = function() {
430430

431431
Bitcoin.prototype._tryAllClients = function(func, callback) {
432432
var self = this;
433-
var nodesIndex = 0;
433+
var nodesIndex = this.nodesIndex;
434434
var retry = function(done) {
435435
var client = self.nodes[nodesIndex].client;
436-
nodesIndex++;
436+
nodesIndex = (nodesIndex + 1) % self.nodes.length;
437437
func(client, done);
438438
};
439439
async.retry({times: this.nodes.length, interval: this.tryAllInterval || 1000}, retry, callback);

test/services/bitcoind.unit.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,37 @@ describe('Bitcoin Service', function() {
556556
done();
557557
});
558558
});
559+
it('will start using the current node index (round-robin)', function(done) {
560+
var bitcoind = new BitcoinService(baseConfig);
561+
bitcoind.tryAllInterval = 1;
562+
bitcoind.nodes.push({
563+
client: {
564+
getInfo: sinon.stub().callsArgWith(0, new Error('2'))
565+
}
566+
});
567+
bitcoind.nodes.push({
568+
client: {
569+
getInfo: sinon.stub().callsArgWith(0, new Error('3'))
570+
}
571+
});
572+
bitcoind.nodes.push({
573+
client: {
574+
getInfo: sinon.stub().callsArgWith(0, new Error('1'))
575+
}
576+
});
577+
bitcoind.nodesIndex = 2;
578+
bitcoind._tryAllClients(function(client, next) {
579+
client.getInfo(next);
580+
}, function(err) {
581+
err.should.be.instanceOf(Error);
582+
err.message.should.equal('3');
583+
bitcoind.nodes[0].client.getInfo.callCount.should.equal(1);
584+
bitcoind.nodes[1].client.getInfo.callCount.should.equal(1);
585+
bitcoind.nodes[2].client.getInfo.callCount.should.equal(1);
586+
bitcoind.nodesIndex.should.equal(2);
587+
done();
588+
});
589+
});
559590
it('will get error if all clients fail', function(done) {
560591
var bitcoind = new BitcoinService(baseConfig);
561592
bitcoind.tryAllInterval = 1;

0 commit comments

Comments
 (0)