diff --git a/lib/server/swarm.js b/lib/server/swarm.js index 738005f..0b39918 100644 --- a/lib/server/swarm.js +++ b/lib/server/swarm.js @@ -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) {