pool: proper backoff
This commit is contained in:
parent
a672b955c9
commit
b425cb4639
@ -31,12 +31,16 @@ function Chain(options) {
|
||||
bloom: null,
|
||||
hashes: [],
|
||||
ts: [],
|
||||
heights: []
|
||||
heights: [],
|
||||
lastTs: 0
|
||||
};
|
||||
this.request = new utils.RequestCache();
|
||||
|
||||
this.fromJSON(preload);
|
||||
|
||||
// Last TS after preload, needed for fill percent
|
||||
this.index.lastTs = this.index.ts[this.index.ts.length - 1];
|
||||
|
||||
this.loading = false;
|
||||
this._init();
|
||||
}
|
||||
@ -335,10 +339,9 @@ Chain.prototype.isFull = function isFull() {
|
||||
};
|
||||
|
||||
Chain.prototype.fillPercent = function fillPercent() {
|
||||
var first = this.index.ts[0];
|
||||
var now = (+new Date() / 1000 - 40 * 60) - first;
|
||||
var last = this.index.ts[this.index.ts.length - 1] - first;
|
||||
return Math.min(0, Math.max(last / now, 1));
|
||||
var total = (+new Date() / 1000 - 40 * 60) - this.index.lastTs;
|
||||
var current = this.index.ts[this.index.ts.length - 1] - this.index.lastTs;
|
||||
return Math.min(0, Math.max(current / total, 1));
|
||||
};
|
||||
|
||||
Chain.prototype.hashesInRange = function hashesInRange(start, end, cb) {
|
||||
|
||||
@ -18,6 +18,10 @@ function Pool(options) {
|
||||
this.size = options.size || 32;
|
||||
this.parallel = options.parallel || 2000;
|
||||
this.redundancy = options.redundancy || 2;
|
||||
this.backoff = {
|
||||
delta: options.backoffDelta || 500,
|
||||
max: options.backoffMax || 5000
|
||||
};
|
||||
this.load = {
|
||||
timeout: options.loadTimeout || 3000,
|
||||
interval: options.loadInterval || 5000,
|
||||
@ -80,7 +84,7 @@ module.exports = Pool;
|
||||
Pool.prototype._init = function _init() {
|
||||
this._addLoader();
|
||||
for (var i = 0; i < this.size; i++)
|
||||
this._addPeer();
|
||||
this._addPeer(0);
|
||||
this._load();
|
||||
|
||||
var self = this;
|
||||
@ -228,14 +232,14 @@ Pool.prototype._load = function _load() {
|
||||
return true;
|
||||
};
|
||||
|
||||
Pool.prototype._addPeer = function _addPeer() {
|
||||
Pool.prototype._addPeer = function _addPeer(backoff) {
|
||||
if (this.destroyed)
|
||||
return;
|
||||
if (this.peers.block.length + this.peers.pending.length >= this.size)
|
||||
return;
|
||||
|
||||
var peer = new bcoin.peer(this, this.createConnection, {
|
||||
backoff: 750 * Math.random()
|
||||
backoff: backoff
|
||||
});
|
||||
|
||||
this.peers.pending.push(peer);
|
||||
@ -252,7 +256,7 @@ Pool.prototype._addPeer = function _addPeer() {
|
||||
self._removePeer(peer);
|
||||
if (self.destroyed)
|
||||
return;
|
||||
self._addPeer();
|
||||
self._addPeer(Math.max(backoff + self.backoff.delta, self.backoff.max));
|
||||
});
|
||||
|
||||
peer.once('ack', function() {
|
||||
@ -277,8 +281,12 @@ Pool.prototype._addPeer = function _addPeer() {
|
||||
});
|
||||
|
||||
peer.on('merkleblock', function(block) {
|
||||
// Reset backoff, peer seems to be responsive
|
||||
backoff = 0;
|
||||
|
||||
self._response(block);
|
||||
self.chain.add(block);
|
||||
self.emit('chain-progress', self.chain.fillPercent());
|
||||
self.emit('block', block);
|
||||
});
|
||||
|
||||
@ -416,6 +424,7 @@ Pool.prototype.search = function search(id, range) {
|
||||
|
||||
function done() {
|
||||
waiting--;
|
||||
assert(waiting >= 0);
|
||||
e.emit('progress', count - waiting, count);
|
||||
if (waiting === 0) {
|
||||
if (id)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user