From 1e027220c3931a13f3efd77cd6746c1fd7ed2da7 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 31 Jan 2016 00:57:34 -0800 Subject: [PATCH] pool: refactor. --- lib/bcoin/pool.js | 86 +++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index b3ab0d8c..d17e0bdb 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -1294,47 +1294,6 @@ Pool.prototype._request = function _request(peer, type, hash, options, cb) { }); }; -function LoadRequest(pool, peer, type, hash, cb) { - this.pool = pool; - this.peer = peer; - this.type = type; - this.hash = hash; - this.cb = []; - - if (cb) - this.cb.push(cb); - - this._finish = this.finish.bind(this); - - this.timeout = setTimeout(this._finish, this.pool.requestTimeout); - this.peer.on('close', this._finish); - - this.pool.request.active++; - - assert(!this.pool.request.map[this.hash]); - this.pool.request.map[this.hash] = this; -} - -LoadRequest.prototype.finish = function finish() { - var index; - - if (this.pool.request.map[this.hash]) { - delete this.pool.request.map[this.hash]; - this.pool.request.active--; - } - - index = this.peer._queue.indexOf(this); - if (index !== -1) - this.peer._queue.splice(index, 1); - - this.peer.removeListener('close', this._finish); - - if (this.timeout != null) { - clearTimeout(this.timeout); - delete this.timeout; - } -}; - Pool.prototype._response = function _response(hash) { var hash; @@ -1710,6 +1669,51 @@ Pool.prototype.isMisbehaving = function isMisbehaving(host) { return false; }; +/** + * LoadRequest + */ + +function LoadRequest(pool, peer, type, hash, cb) { + this.pool = pool; + this.peer = peer; + this.type = type; + this.hash = hash; + this.cb = []; + + if (cb) + this.cb.push(cb); + + this._finish = this.finish.bind(this); + + this.timeout = setTimeout(this._finish, this.pool.requestTimeout); + this.peer.on('close', this._finish); + + this.pool.request.active++; + + assert(!this.pool.request.map[this.hash]); + this.pool.request.map[this.hash] = this; +} + +LoadRequest.prototype.finish = function finish() { + var index; + + if (this.pool.request.map[this.hash]) { + delete this.pool.request.map[this.hash]; + this.pool.request.active--; + } + + index = this.peer._queue.indexOf(this); + if (index !== -1) + this.peer._queue.splice(index, 1); + + this.peer.removeListener('close', this._finish); + + if (this.timeout != null) { + clearTimeout(this.timeout); + delete this.timeout; + } +}; + /** * Expose */