Skip to content

Commit d51a77b

Browse files
committed
Update lru package to 3.0.0 and set peersCacheLength to 1K as default
1 parent 5843b7d commit d51a77b

File tree

4 files changed

+6
-10
lines changed

4 files changed

+6
-10
lines changed

lib/server/swarm.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var randomIterate = require('random-iterate')
88
// need to support when overriding Server.createSwarm() and Server.getSwarm()
99
function Swarm (infoHash, server) {
1010
this.peers = new LRU({
11-
max: server.peersCacheLength || 10000,
11+
max: server.peersCacheLength || 1000,
1212
maxAge: server.peersCacheTtl || 900 // 900s = 15 minutes
1313
})
1414
this.complete = 0
@@ -115,16 +115,12 @@ Swarm.prototype._onAnnounceUpdate = function (params, peer) {
115115

116116
Swarm.prototype._getPeers = function (numwant, ownPeerId, isWebRTC) {
117117
var peers = []
118-
var ite = randomIterate(Object.keys(this.peers.cache))
118+
var ite = randomIterate(this.peers.keys)
119119
var peerId
120120
while ((peerId = ite()) && peers.length < numwant) {
121-
// Check manually if the peer is active
122-
if (this.peers.maxAge && (Date.now() - this.peers.cache[peerId].modified) > this.peers.maxAge) {
123-
peers.remove(peerId)
124-
continue
125-
}
126121
// Don't mark the peer as most recently used on announce
127122
var peer = this.peers.peek(peerId)
123+
if (!peer) continue
128124
if (isWebRTC && peer.peerId === ownPeerId) continue // don't send peer to itself
129125
if ((isWebRTC && peer.type !== 'ws') || (!isWebRTC && peer.type === 'ws')) continue // send proper peer type
130126
peers.push(peer)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"hat": "0.0.3",
2828
"inherits": "^2.0.1",
2929
"ip": "^1.0.1",
30-
"lru": "^2.0.1",
30+
"lru": "^3.0.0",
3131
"minimist": "^1.1.1",
3232
"once": "^1.3.0",
3333
"random-iterate": "^1.0.1",

server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ function Server (opts) {
156156
if (req.method === 'GET' && (req.url === '/stats' || req.url === '/stats.json')) {
157157
infoHashes.forEach(function (infoHash) {
158158
var peers = self.torrents[infoHash].peers
159-
var keys = Object.keys(peers.cache)
159+
var keys = peers.keys
160160
if (keys.length > 0) activeTorrents++
161161

162162
keys.forEach(function (peerId) {

test/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function serverTest (t, serverType, serverFamily) {
5858
t.equal(Object.keys(server.torrents).length, 1)
5959
t.equal(swarm.complete, 0)
6060
t.equal(swarm.incomplete, 1)
61-
t.equal(Object.keys(swarm.peers.cache).length, 1)
61+
t.equal(swarm.peers.length, 1)
6262

6363
var id = serverType === 'ws'
6464
? peerId.toString('hex')

0 commit comments

Comments
 (0)