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

View File

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