cleanup peers that never get an answer

attempt to fix https://github.com/feross/webtorrent/issues/502
This commit is contained in:
Feross Aboukhadijeh 2015-12-05 01:01:04 -08:00
parent e6382c9af6
commit fe2ac4ac67

View File

@ -18,6 +18,7 @@ var socketPool = {}
var RECONNECT_VARIANCE = 30 * 1000 var RECONNECT_VARIANCE = 30 * 1000
var RECONNECT_MINIMUM = 5 * 1000 var RECONNECT_MINIMUM = 5 * 1000
var OFFER_TIMEOUT = 60000
inherits(WebSocketTracker, Tracker) inherits(WebSocketTracker, Tracker)
@ -199,11 +200,15 @@ WebSocketTracker.prototype._onSocketData = function (data) {
} }
if (data.answer && data.peer_id) { 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) { if (peer) {
peer.id = common.binaryToHex(data.peer_id) peer.id = common.binaryToHex(data.peer_id)
peer.signal(data.answer) peer.signal(data.answer)
self.client.emit('peer', peer) self.client.emit('peer', peer)
clearTimeout(peer.trackerTimeout)
self.peers[offerId] = null
} else { } else {
debug('got unexpected answer: ' + JSON.stringify(data.answer)) debug('got unexpected answer: ' + JSON.stringify(data.answer))
} }
@ -253,7 +258,6 @@ WebSocketTracker.prototype._generateOffers = function (numwant, cb) {
var offers = [] var offers = []
debug('generating %s offers', numwant) 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) { for (var i = 0; i < numwant; ++i) {
generateOffer() generateOffer()
} }
@ -274,6 +278,10 @@ WebSocketTracker.prototype._generateOffers = function (numwant, cb) {
}) })
checkDone() checkDone()
}) })
peer.trackerTimeout = setTimeout(function () {
peer.destroy()
self.peers[offerId] = null
}, OFFER_TIMEOUT)
} }
function checkDone () { function checkDone () {