From ce9fe104b37aae091950d69b329d035a4b9c6931 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 13 Jun 2016 00:04:34 -0700 Subject: [PATCH] add connect timeout. --- lib/bcoin/peer.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 6acc7219..91841f47 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -97,6 +97,7 @@ function Peer(pool, options) { this.waiting = 0; this.syncSent = false; this.loader = options.loader || false; + this._connectTimeout = null; this.challenge = null; this.lastPong = -1; @@ -158,6 +159,10 @@ Peer.prototype._init = function init() { self.ts = utils.now(); self.connected = true; self.emit('connect'); + if (self._connectTimeout != null) { + clearTimeout(self._connectTimeout); + self._connectTimeout = null; + } self._onconnect(); }); @@ -246,6 +251,8 @@ Peer.prototype._onconnect = function _onconnect() { if (self.loader && self.pool.synced) self.sendMempool(); + bcoin.debug('Received verack (%s).', self.hostname); + // Finally we can let the pool know // that this peer is ready to go. self.emit('ack'); @@ -290,6 +297,7 @@ Peer.prototype._onconnect = function _onconnect() { */ Peer.prototype.createSocket = function createSocket(port, host) { + var self = this; var hostname = IP.hostname(host, port); var socket, proxy, net; @@ -311,6 +319,10 @@ Peer.prototype.createSocket = function createSocket(port, host) { bcoin.debug('Connected to %s.', hostname); }); + this._connectTimeout = setTimeout(function() { + self.destroy(); + }, 10000); + return socket; };