From fe2ac4ac6795e75893e38e8ccffcd95e8df3e507 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 5 Dec 2015 01:01:04 -0800 Subject: [PATCH] cleanup peers that never get an answer attempt to fix https://github.com/feross/webtorrent/issues/502 --- lib/client/websocket-tracker.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index c5284f0..47845d6 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -18,6 +18,7 @@ var socketPool = {} var RECONNECT_VARIANCE = 30 * 1000 var RECONNECT_MINIMUM = 5 * 1000 +var OFFER_TIMEOUT = 60000 inherits(WebSocketTracker, Tracker) @@ -199,11 +200,15 @@ WebSocketTracker.prototype._onSocketData = function (data) { } if (data.answer && data.peer_id) { - peer = self.peers[common.binaryToHex(data.offer_id)] + var offerId = common.binaryToHex(data.offer_id) + peer = self.peers[offerId] if (peer) { peer.id = common.binaryToHex(data.peer_id) peer.signal(data.answer) self.client.emit('peer', peer) + + clearTimeout(peer.trackerTimeout) + self.peers[offerId] = null } else { debug('got unexpected answer: ' + JSON.stringify(data.answer)) } @@ -253,7 +258,6 @@ WebSocketTracker.prototype._generateOffers = function (numwant, cb) { var offers = [] debug('generating %s offers', numwant) - // TODO: cleanup dead peers and peers that never get a return offer, from self.peers for (var i = 0; i < numwant; ++i) { generateOffer() } @@ -274,6 +278,10 @@ WebSocketTracker.prototype._generateOffers = function (numwant, cb) { }) checkDone() }) + peer.trackerTimeout = setTimeout(function () { + peer.destroy() + self.peers[offerId] = null + }, OFFER_TIMEOUT) } function checkDone () {