refactor pool and peer.

This commit is contained in:
Christopher Jeffrey 2016-06-30 13:16:25 -07:00
parent b4bbba1ad9
commit d346b36754
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 50 additions and 28 deletions

View File

@ -546,6 +546,8 @@ Peer.prototype.setFeeRate = function setFeeRate(rate) {
*/
Peer.prototype.destroy = function destroy() {
var i, j, hashes, hash, queue;
if (this.destroyed)
return;
@ -559,13 +561,15 @@ Peer.prototype.destroy = function destroy() {
this.ping.timer = null;
}
Object.keys(this.requests.map).forEach(function(cmd) {
var queue = this.requests.map[cmd];
var i;
hashes = Object.keys(this.requests.map);
for (i = 0; i < queue.length; i++)
clearTimeout(queue[i].timer);
}, this);
for (i = 0; i < hashes.length; i++) {
hash = hashes[i];
queue = this.requests.map[cmd];
for (j = 0; j < queue.length; j++)
clearTimeout(queue[j].timer);
}
};
/**

View File

@ -80,7 +80,7 @@ var InvItem = bcoin.packets.InvItem;
function Pool(options) {
var self = this;
var seeds;
var i, seeds, hostname, seed;
if (!(this instanceof Pool))
return new Pool(options);
@ -111,13 +111,16 @@ function Pool(options) {
seeds.unshift(process.env.BCOIN_SEED);
}
this.seeds = seeds.map(function(hostname) {
return NetworkAddress.fromHostname(hostname, self.network);
});
this.seeds = [];
this.hosts = [];
this.hostMap = {};
for (i = 0; i < seeds.length; i++) {
hostname = seeds[i];
seed = NetworkAddress.fromHostname(hostname, this.network);
this.seeds.push(seed);
}
this.address = new NetworkAddress({
ts: utils.now() - (process.uptime() | 0),
services: constants.LOCAL_SERVICES,
@ -1292,10 +1295,13 @@ Pool.prototype._addPeer = function _addPeer() {
*/
Pool.prototype._removePeer = function _removePeer(peer) {
var i, hashes, hash, item;
utils.binaryRemove(this.peers.pending, peer, compare);
utils.binaryRemove(this.peers.regular, peer, compare);
utils.binaryRemove(this.peers.leeches, peer, compare);
utils.binaryRemove(this.peers.all, peer, compare);
delete this.peers.map[peer.host];
if (this.peers.load === peer) {
@ -1303,11 +1309,14 @@ Pool.prototype._removePeer = function _removePeer(peer) {
this.peers.load = null;
}
Object.keys(this.request.map).forEach(function(hash) {
var item = this.request.map[hash];
hashes = Object.keys(this.request.map);
for (i = 0; i < hashes.length; i++) {
hash = hashes[i];
item = this.request.map[hash];
if (item.peer === peer)
item.finish(new Error('Peer closed.'));
}, this);
}
};
/**
@ -1663,6 +1672,8 @@ Pool.prototype.setFeeRate = function setFeeRate(rate) {
Pool.prototype.close =
Pool.prototype.destroy = function destroy(callback) {
var i, items, peers, hashes, hash;
callback = utils.ensure(callback);
if (this.destroyed)
@ -1672,28 +1683,35 @@ Pool.prototype.destroy = function destroy(callback) {
this.stopSync();
this.inv.items.slice().forEach(function(entry) {
entry.finish();
});
items = this.inv.items.slice();
Object.keys(this.request.map).forEach(function(hash) {
for (i = 0; i < items.length; i++)
items[i].finish();
hashes = Object.keys(this.request.map);
for (i = 0; i < hashes.length; i++) {
hash = hashes[i];
this.request.map[hash].finish(new Error('Pool closed.'));
}, this);
}
if (this.peers.load)
this.peers.load.destroy();
this.peers.regular.slice().forEach(function(peer) {
peer.destroy();
});
peers = this.peers.regular.slice();
this.peers.pending.slice().forEach(function(peer) {
peer.destroy();
});
for (i = 0; i < peers.length; i++)
peers[i].destroy();
this.peers.leeches.slice().forEach(function(peer) {
peer.destroy();
});
peers = this.peers.pending.slice();
for (i = 0; i < peers.length; i++)
peers[i].destroy();
peers = this.peers.leeches.slice();
for (i = 0; i < peers.length; i++)
peers[i].destroy();
this.unlisten(callback);
};