From d51a77b0287067c923973964e799d215e9fb986f Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Fri, 10 Jun 2016 10:00:19 +0200 Subject: [PATCH] Update lru package to 3.0.0 and set peersCacheLength to 1K as default --- lib/server/swarm.js | 10 +++------- package.json | 2 +- server.js | 2 +- test/server.js | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/server/swarm.js b/lib/server/swarm.js index 4bb9820..df12995 100644 --- a/lib/server/swarm.js +++ b/lib/server/swarm.js @@ -8,7 +8,7 @@ var randomIterate = require('random-iterate') // need to support when overriding Server.createSwarm() and Server.getSwarm() function Swarm (infoHash, server) { this.peers = new LRU({ - max: server.peersCacheLength || 10000, + max: server.peersCacheLength || 1000, maxAge: server.peersCacheTtl || 900 // 900s = 15 minutes }) this.complete = 0 @@ -115,16 +115,12 @@ Swarm.prototype._onAnnounceUpdate = function (params, peer) { Swarm.prototype._getPeers = function (numwant, ownPeerId, isWebRTC) { var peers = [] - var ite = randomIterate(Object.keys(this.peers.cache)) + var ite = randomIterate(this.peers.keys) var peerId while ((peerId = ite()) && peers.length < numwant) { - // Check manually if the peer is active - if (this.peers.maxAge && (Date.now() - this.peers.cache[peerId].modified) > this.peers.maxAge) { - peers.remove(peerId) - continue - } // Don't mark the peer as most recently used on announce var peer = this.peers.peek(peerId) + if (!peer) continue if (isWebRTC && peer.peerId === ownPeerId) continue // don't send peer to itself if ((isWebRTC && peer.type !== 'ws') || (!isWebRTC && peer.type === 'ws')) continue // send proper peer type peers.push(peer) diff --git a/package.json b/package.json index 1334922..c4bb5f7 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "hat": "0.0.3", "inherits": "^2.0.1", "ip": "^1.0.1", - "lru": "^2.0.1", + "lru": "^3.0.0", "minimist": "^1.1.1", "once": "^1.3.0", "random-iterate": "^1.0.1", diff --git a/server.js b/server.js index 4fc9e1e..6b56a9e 100644 --- a/server.js +++ b/server.js @@ -156,7 +156,7 @@ function Server (opts) { if (req.method === 'GET' && (req.url === '/stats' || req.url === '/stats.json')) { infoHashes.forEach(function (infoHash) { var peers = self.torrents[infoHash].peers - var keys = Object.keys(peers.cache) + var keys = peers.keys if (keys.length > 0) activeTorrents++ keys.forEach(function (peerId) { diff --git a/test/server.js b/test/server.js index 706cb04..b3a7b16 100644 --- a/test/server.js +++ b/test/server.js @@ -58,7 +58,7 @@ function serverTest (t, serverType, serverFamily) { t.equal(Object.keys(server.torrents).length, 1) t.equal(swarm.complete, 0) t.equal(swarm.incomplete, 1) - t.equal(Object.keys(swarm.peers.cache).length, 1) + t.equal(swarm.peers.length, 1) var id = serverType === 'ws' ? peerId.toString('hex')