pool: repurpose regular peers.

This commit is contained in:
Christopher Jeffrey 2016-08-25 18:32:21 -07:00
parent b3c061f125
commit 9ed67e0695
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -602,7 +602,7 @@ Pool.prototype.stopInterval = function stopInterval() {
Pool.prototype.addLoader = function addLoader() {
var self = this;
var peer;
var peer, host;
if (!this.loaded)
return;
@ -612,8 +612,20 @@ Pool.prototype.addLoader = function addLoader() {
return;
}
host = this.getLoaderHost();
peer = this.peers.get(host);
if (peer) {
this.peers.repurpose(peer);
this.logger.info('Repurposed loader peer (%s).', peer.hostname);
utils.nextTick(function() {
self.emit('loader', peer);
});
return;
}
peer = this.createPeer({
host: this.getLoaderHost(),
host: host,
type: bcoin.peer.types.LOADER
});
@ -1930,6 +1942,15 @@ PeerList.prototype.remove = function remove(peer) {
}
};
PeerList.prototype.repurpose = function repurpose(peer) {
assert(peer.type === bcoin.peer.types.REGULAR);
peer.type = bcoin.peer.types.LOADER;
utils.binaryRemove(this.pending, peer, compare);
utils.binaryRemove(this.regular, peer, compare);
assert(!this.peer.load);
this.peers.load = peer;
};
PeerList.prototype.isFull = function isFull() {
return this.regular.length + this.pending.length >= this.pool.maxPeers - 1;
};