From e8cb2102a389e1d559a0fbb37b7e1a2af226915a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 29 Jan 2016 17:38:31 -0800 Subject: [PATCH] work --- lib/bcoin/peer.js | 6 ++++++ lib/bcoin/pool.js | 22 +++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 4f0f602a..fbcf425f 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -82,6 +82,10 @@ function Peer(pool, createConnection, options) { interval: this.options.pingInterval || 30000 }; + Peer.uid.iaddn(1); + + this.id = Peer.uid.toString(10); + this.setMaxListeners(10000); if (this.socket) @@ -92,6 +96,8 @@ function Peer(pool, createConnection, options) { inherits(Peer, EventEmitter); +Peer.uid = new bn(0); + Peer.prototype._init = function init() { var self = this; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index d8aea5af..c58e3544 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -1378,7 +1378,7 @@ Pool.prototype._scheduleRequests = function _scheduleRequests() { Pool.prototype._doRequests = function _doRequests() { var queue, above, items, below; var red, count, split, i, off, req, j; - var mapReq, peerItems, peers; + var mapReq, peerItems, peers, uniq; if (this.request.active >= this.parallel) return; @@ -1409,8 +1409,13 @@ Pool.prototype._doRequests = function _doRequests() { }); if (peerItems.length > 0) { - peers = utils.uniq(peerItems.map(function(item) { - return item.targetPeer; + uniq = {}; + peers = []; + peerItems.forEach(function(item) { + if (uniq[item.targetPeer.id]) + return; + uniq[item.targetPeer.id] = true; + peers.push(item.targetPeer); })); peers.forEach(function(peer) { var items = peerItems.filter(function(item) { @@ -1881,10 +1886,10 @@ LoadRequest.prototype.start = function start(peer) { this.active = true; this.pool.request.active++; - // this.timer = setTimeout(function() { - // self.timer = null; - // self.retry(); - // }, this.pool.requestTimeout); + this.timer = setTimeout(function() { + self.timer = null; + self.retry(); + }, this.pool.requestTimeout); this.peer = peer || this.targetPeer; this.peer.once('close', this.onclose); @@ -1900,6 +1905,7 @@ LoadRequest.compare = function compare(a, b) { }; LoadRequest.prototype.add = function add(noQueue) { + assert(!this.pool.request.map[this.hash]); this.pool.request.map[this.hash] = this; if (!noQueue) this.pool.request.queue.push(this); @@ -1938,6 +1944,8 @@ LoadRequest.prototype.finish = function finish(entity) { if (this.active) { this.clear(); + index = this.pool.request.queue.indexOf(this); + assert(index !== -1 || this.noQueue); } else { // It could be that request was never sent to the node, remove it from // queue and forget about it