remove peer backoff system.

This commit is contained in:
Christopher Jeffrey 2016-01-30 02:21:00 -08:00
parent f59a1f887b
commit 8447790a84
2 changed files with 20 additions and 75 deletions

View File

@ -14,12 +14,6 @@ var assert = utils.assert;
var constants = bcoin.protocol.constants; var constants = bcoin.protocol.constants;
var network = bcoin.protocol.network; var network = bcoin.protocol.network;
// Browserify, I'm looking at you
try {
var NodeBuffer = require('buf' + 'fer').Buffer;
} catch (e) {
}
/** /**
* Peer * Peer
*/ */
@ -51,18 +45,9 @@ function Peer(pool, createConnection, options) {
this.challenge = null; this.challenge = null;
this.lastPong = 0; this.lastPong = 0;
if (this.options.backoff) { this.socket = createConnection.call(pool, this, options);
setTimeout(function() { if (!this.socket)
self.socket = createConnection(self, pool, options); throw new Error('No socket');
if (!self.socket)
throw new Error('No socket');
self.emit('socket');
}, this.options.backoff);
} else {
this.socket = createConnection(this, pool, options);
if (!this.socket)
throw new Error('No socket');
}
this._broadcast = { this._broadcast = {
timeout: this.options.broadcastTimeout || 30000, timeout: this.options.broadcastTimeout || 30000,
@ -90,10 +75,7 @@ function Peer(pool, createConnection, options) {
this.setMaxListeners(10000); this.setMaxListeners(10000);
if (this.socket) this._init();
this._init();
else
this.once('socket', this._init);
} }
inherits(Peer, EventEmitter); inherits(Peer, EventEmitter);
@ -254,9 +236,6 @@ Peer.prototype.destroy = function destroy() {
if (this.destroyed) if (this.destroyed)
return; return;
if (!this.socket)
return this.once('socket', this.destroy);
this.destroyed = true; this.destroyed = true;
this.socket.destroy(); this.socket.destroy();
this.socket = null; this.socket = null;
@ -275,33 +254,13 @@ Peer.prototype.destroy = function destroy() {
clearTimeout(this._request.queue[i].timer); clearTimeout(this._request.queue[i].timer);
}; };
// Private APIs
Peer.prototype._write = function write(chunk) { Peer.prototype._write = function write(chunk) {
var self = this; var self = this;
if (this.destroyed) if (this.destroyed)
return; return;
if (!this.socket) { this.socket.write(new Buffer(chunk));
if (!this._bchunks) {
this._bchunks = [];
this.once('socket', function() {
var chunks = self._bchunks;
delete self._bchunks;
chunks.forEach(function(chunk) {
self._write(chunk);
});
});
}
this._bchunks.push(chunk);
return;
}
if (NodeBuffer)
this.socket.write(new NodeBuffer(chunk));
else
this.socket.write(chunk);
}; };
Peer.prototype._error = function error(err) { Peer.prototype._error = function error(err) {

View File

@ -57,7 +57,6 @@ function Pool(options) {
this.destroyed = false; this.destroyed = false;
this.size = options.size || 32; this.size = options.size || 32;
this._createConnection = options.createConnection;
this._createSocket = options.createSocket; this._createSocket = options.createSocket;
if (!this.options.fullNode) { if (!this.options.fullNode) {
@ -78,11 +77,6 @@ function Pool(options) {
this.syncing = false; this.syncing = false;
this.synced = false; this.synced = false;
this.backoff = {
delta: options.backoffDelta || 500,
max: options.backoffMax || 5000
};
this.load = { this.load = {
timeout: options.loadTimeout || 30000, timeout: options.loadTimeout || 30000,
interval: options.loadInterval || 5000 interval: options.loadInterval || 5000
@ -176,7 +170,7 @@ Pool.prototype._init = function _init() {
this._addLoader(); this._addLoader();
for (i = 0; i < this.size; i++) for (i = 0; i < this.size; i++)
this._addPeer(0); this._addPeer();
} }
this.chain.on('block', function(block, peer) { this.chain.on('block', function(block, peer) {
@ -294,13 +288,10 @@ Pool.prototype._stopInterval = function _stopInterval() {
delete this._interval; delete this._interval;
}; };
Pool.prototype.createConnection = function createConnection(peer, pool, options) { Pool.prototype.createConnection = function createConnection(peer, options) {
var addr, net, socket; var addr, net, socket;
if (pool._createConnection) addr = this.getSeed(options.priority, true);
return pool._createConnection(peer, pool);
addr = pool.usableSeed(options.priority, true);
assert(addr); assert(addr);
assert(addr.host); assert(addr.host);
@ -308,8 +299,8 @@ Pool.prototype.createConnection = function createConnection(peer, pool, options)
peer.host = addr.host; peer.host = addr.host;
peer.port = addr.port; peer.port = addr.port;
if (pool._createSocket) { if (this._createSocket) {
socket = pool._createSocket(addr.port, addr.host); socket = this._createSocket(addr.port, addr.host);
} else { } else {
net = require('net'); net = require('net');
socket = net.connect(addr.port, addr.host); socket = net.connect(addr.port, addr.host);
@ -338,11 +329,9 @@ Pool.prototype._addLoader = function _addLoader() {
if (this.peers.load != null) if (this.peers.load != null)
return; return;
peer = this._createPeer(750 * Math.random(), true); peer = this._createPeer(true);
peer.once('socket', function() { utils.debug('Added loader peer: %s', peer.host);
utils.debug('Added loader peer: %s', peer.host);
});
this.peers.load = peer; this.peers.load = peer;
this.peers.all.push(peer); this.peers.all.push(peer);
@ -734,11 +723,10 @@ Pool.prototype.loadMempool = function loadMempool() {
}); });
}; };
Pool.prototype._createPeer = function _createPeer(backoff, priority) { Pool.prototype._createPeer = function _createPeer(priority) {
var self = this; var self = this;
var peer = new bcoin.peer(this, this.createConnection, { var peer = new bcoin.peer(this, this.createConnection, {
backoff: backoff,
startHeight: this.options.startHeight, startHeight: this.options.startHeight,
relay: this.options.relay, relay: this.options.relay,
priority: priority priority: priority
@ -815,7 +803,7 @@ Pool.prototype._createPeer = function _createPeer(backoff, priority) {
return peer; return peer;
}; };
Pool.prototype._addPeer = function _addPeer(backoff) { Pool.prototype._addPeer = function _addPeer() {
var self = this; var self = this;
var peer; var peer;
@ -825,12 +813,12 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
if (this.peers.block.length + this.peers.pending.length >= this.size) if (this.peers.block.length + this.peers.pending.length >= this.size)
return; return;
if (!this._createConnection && !this.usableSeed()) { if (!this.getSeed()) {
setTimeout(this._addPeer.bind(this, 0), this.backoff.max); setTimeout(this._addPeer.bind(this), 5000);
return; return;
} }
peer = this._createPeer(backoff, false); peer = this._createPeer(false);
this.peers.pending.push(peer); this.peers.pending.push(peer);
this.peers.all.push(peer); this.peers.all.push(peer);
@ -839,7 +827,7 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
self._removePeer(peer); self._removePeer(peer);
if (self.destroyed) if (self.destroyed)
return; return;
self._addPeer(Math.min(backoff + self.backoff.delta, self.backoff.max)); self._addPeer();
}); });
peer.once('ack', function() { peer.once('ack', function() {
@ -874,12 +862,10 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
}); });
peer.on('merkleblock', function(block) { peer.on('merkleblock', function(block) {
backoff = 0;
self._handleBlock(block, peer); self._handleBlock(block, peer);
}); });
peer.on('block', function(block) { peer.on('block', function(block) {
backoff = 0;
self._handleBlock(block, peer); self._handleBlock(block, peer);
}); });
@ -1558,14 +1544,14 @@ Pool.prototype.getPeer = function getPeer(addr) {
} }
}; };
Pool.prototype.usableSeed = function usableSeed(priority, connecting) { Pool.prototype.getSeed = function getSeed(priority, connecting) {
var i, addr; var i, addr;
var original = this.originalSeeds; var original = this.originalSeeds;
var seeds = this.seeds; var seeds = this.seeds;
var all = original.concat(seeds); var all = original.concat(seeds);
// Hang back if we don't have a loader peer yet. // Hang back if we don't have a loader peer yet.
if (!connecting && !priority && (!this.peers.load || !this.peers.load.socket)) if (!connecting && !priority && !this.peers.load)
return; return;
// Randomize the non-original peers. // Randomize the non-original peers.