From b425cb4639e7f69cf0f514fd59322c12881d37a2 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Wed, 14 May 2014 19:24:28 +0400 Subject: [PATCH] pool: proper backoff --- lib/bcoin/chain.js | 13 ++++++++----- lib/bcoin/pool.js | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index e42e207a..fdfb6f75 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -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) { diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index cf8f4687..94719700 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -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)