refactor pool and peer.
This commit is contained in:
parent
b4bbba1ad9
commit
d346b36754
@ -546,6 +546,8 @@ Peer.prototype.setFeeRate = function setFeeRate(rate) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Peer.prototype.destroy = function destroy() {
|
Peer.prototype.destroy = function destroy() {
|
||||||
|
var i, j, hashes, hash, queue;
|
||||||
|
|
||||||
if (this.destroyed)
|
if (this.destroyed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -559,13 +561,15 @@ Peer.prototype.destroy = function destroy() {
|
|||||||
this.ping.timer = null;
|
this.ping.timer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.keys(this.requests.map).forEach(function(cmd) {
|
hashes = Object.keys(this.requests.map);
|
||||||
var queue = this.requests.map[cmd];
|
|
||||||
var i;
|
|
||||||
|
|
||||||
for (i = 0; i < queue.length; i++)
|
for (i = 0; i < hashes.length; i++) {
|
||||||
clearTimeout(queue[i].timer);
|
hash = hashes[i];
|
||||||
}, this);
|
queue = this.requests.map[cmd];
|
||||||
|
|
||||||
|
for (j = 0; j < queue.length; j++)
|
||||||
|
clearTimeout(queue[j].timer);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -80,7 +80,7 @@ var InvItem = bcoin.packets.InvItem;
|
|||||||
|
|
||||||
function Pool(options) {
|
function Pool(options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var seeds;
|
var i, seeds, hostname, seed;
|
||||||
|
|
||||||
if (!(this instanceof Pool))
|
if (!(this instanceof Pool))
|
||||||
return new Pool(options);
|
return new Pool(options);
|
||||||
@ -111,13 +111,16 @@ function Pool(options) {
|
|||||||
seeds.unshift(process.env.BCOIN_SEED);
|
seeds.unshift(process.env.BCOIN_SEED);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.seeds = seeds.map(function(hostname) {
|
this.seeds = [];
|
||||||
return NetworkAddress.fromHostname(hostname, self.network);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.hosts = [];
|
this.hosts = [];
|
||||||
this.hostMap = {};
|
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({
|
this.address = new NetworkAddress({
|
||||||
ts: utils.now() - (process.uptime() | 0),
|
ts: utils.now() - (process.uptime() | 0),
|
||||||
services: constants.LOCAL_SERVICES,
|
services: constants.LOCAL_SERVICES,
|
||||||
@ -1292,10 +1295,13 @@ Pool.prototype._addPeer = function _addPeer() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Pool.prototype._removePeer = function _removePeer(peer) {
|
Pool.prototype._removePeer = function _removePeer(peer) {
|
||||||
|
var i, hashes, hash, item;
|
||||||
|
|
||||||
utils.binaryRemove(this.peers.pending, peer, compare);
|
utils.binaryRemove(this.peers.pending, peer, compare);
|
||||||
utils.binaryRemove(this.peers.regular, peer, compare);
|
utils.binaryRemove(this.peers.regular, peer, compare);
|
||||||
utils.binaryRemove(this.peers.leeches, peer, compare);
|
utils.binaryRemove(this.peers.leeches, peer, compare);
|
||||||
utils.binaryRemove(this.peers.all, peer, compare);
|
utils.binaryRemove(this.peers.all, peer, compare);
|
||||||
|
|
||||||
delete this.peers.map[peer.host];
|
delete this.peers.map[peer.host];
|
||||||
|
|
||||||
if (this.peers.load === peer) {
|
if (this.peers.load === peer) {
|
||||||
@ -1303,11 +1309,14 @@ Pool.prototype._removePeer = function _removePeer(peer) {
|
|||||||
this.peers.load = null;
|
this.peers.load = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.keys(this.request.map).forEach(function(hash) {
|
hashes = Object.keys(this.request.map);
|
||||||
var item = this.request.map[hash];
|
|
||||||
|
for (i = 0; i < hashes.length; i++) {
|
||||||
|
hash = hashes[i];
|
||||||
|
item = this.request.map[hash];
|
||||||
if (item.peer === peer)
|
if (item.peer === peer)
|
||||||
item.finish(new Error('Peer closed.'));
|
item.finish(new Error('Peer closed.'));
|
||||||
}, this);
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1663,6 +1672,8 @@ Pool.prototype.setFeeRate = function setFeeRate(rate) {
|
|||||||
|
|
||||||
Pool.prototype.close =
|
Pool.prototype.close =
|
||||||
Pool.prototype.destroy = function destroy(callback) {
|
Pool.prototype.destroy = function destroy(callback) {
|
||||||
|
var i, items, peers, hashes, hash;
|
||||||
|
|
||||||
callback = utils.ensure(callback);
|
callback = utils.ensure(callback);
|
||||||
|
|
||||||
if (this.destroyed)
|
if (this.destroyed)
|
||||||
@ -1672,28 +1683,35 @@ Pool.prototype.destroy = function destroy(callback) {
|
|||||||
|
|
||||||
this.stopSync();
|
this.stopSync();
|
||||||
|
|
||||||
this.inv.items.slice().forEach(function(entry) {
|
items = this.inv.items.slice();
|
||||||
entry.finish();
|
|
||||||
});
|
|
||||||
|
|
||||||
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.request.map[hash].finish(new Error('Pool closed.'));
|
||||||
}, this);
|
}
|
||||||
|
|
||||||
if (this.peers.load)
|
if (this.peers.load)
|
||||||
this.peers.load.destroy();
|
this.peers.load.destroy();
|
||||||
|
|
||||||
this.peers.regular.slice().forEach(function(peer) {
|
peers = this.peers.regular.slice();
|
||||||
peer.destroy();
|
|
||||||
});
|
|
||||||
|
|
||||||
this.peers.pending.slice().forEach(function(peer) {
|
for (i = 0; i < peers.length; i++)
|
||||||
peer.destroy();
|
peers[i].destroy();
|
||||||
});
|
|
||||||
|
|
||||||
this.peers.leeches.slice().forEach(function(peer) {
|
peers = this.peers.pending.slice();
|
||||||
peer.destroy();
|
|
||||||
});
|
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);
|
this.unlisten(callback);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user