Close websockets when peers are evicted from LRU cache

Possibly fixes: https://github.com/feross/bittorrent-tracker/issues/196

Close websockets when peers are evicted from LRU cache, otherwise it's
possible for a peer object to be evicted from the LRU cache without the
socket being cleaned up. That will leak memory until the websocket is
closed by the remote client. It also messes up the stats.
This commit is contained in:
Feross Aboukhadijeh 2017-02-02 17:43:16 -08:00
parent 8f33b95f9f
commit 7075088848

View File

@ -7,12 +7,24 @@ var randomIterate = require('random-iterate')
// Regard this as the default implementation of an interface that you
// need to support when overriding Server.createSwarm() and Server.getSwarm()
function Swarm (infoHash, server) {
this.complete = 0
this.incomplete = 0
this.peers = new LRU({
max: server.peersCacheLength || 1000,
maxAge: server.peersCacheTtl || 20 * 60 * 1000 // 20 minutes
})
this.complete = 0
this.incomplete = 0
// When a peer is evicted from the LRU cache, if it's a websocket peer,
// close the websocket.
this.peers.on('evict', function (data) {
var peer = data.value
if (peer.socket) {
try {
peer.socket.close()
peer.socket = null
} catch (err) {}
}
})
}
Swarm.prototype.announce = function (params, cb) {