Update lru package to 3.0.0 and set peersCacheLength to 1K as default

This commit is contained in:
Yoann Ciabaud 2016-06-10 10:00:19 +02:00
parent 5843b7d2f6
commit d51a77b028
4 changed files with 6 additions and 10 deletions

View File

@ -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)

View File

@ -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",

View File

@ -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) {

View File

@ -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')