Prune old peers in server with lru based cache. Issue #4

This commit is contained in:
Yoann Ciabaud 2016-06-08 23:57:01 +02:00
parent 92a0710026
commit ca522c0c4b
4 changed files with 32 additions and 19 deletions

View File

@ -1,12 +1,16 @@
module.exports = Swarm module.exports = Swarm
var debug = require('debug')('bittorrent-tracker') var debug = require('debug')('bittorrent-tracker')
var LRU = require('lru')
var randomIterate = require('random-iterate') var randomIterate = require('random-iterate')
// Regard this as the default implementation of an interface that you // Regard this as the default implementation of an interface that you
// need to support when overriding Server.createSwarm() and Server.getSwarm() // need to support when overriding Server.createSwarm() and Server.getSwarm()
function Swarm (infoHash, server) { function Swarm (infoHash, server) {
this.peers = {} this.peers = new LRU({
max: server.peersCacheLength || 10000,
maxAge: server.peersCacheTtl || 900 // 900s = 15 minutes
})
this.complete = 0 this.complete = 0
this.incomplete = 0 this.incomplete = 0
} }
@ -14,7 +18,8 @@ function Swarm (infoHash, server) {
Swarm.prototype.announce = function (params, cb) { Swarm.prototype.announce = function (params, cb) {
var self = this var self = this
var id = params.type === 'ws' ? params.peer_id : params.addr var id = params.type === 'ws' ? params.peer_id : params.addr
var peer = self.peers[id] // Mark the source peer as recently used in cache
var peer = self.peers.get(id)
if (params.event === 'started') { if (params.event === 'started') {
self._onAnnounceStarted(params, peer) self._onAnnounceStarted(params, peer)
@ -51,14 +56,14 @@ Swarm.prototype._onAnnounceStarted = function (params, peer) {
if (params.left === 0) this.complete += 1 if (params.left === 0) this.complete += 1
else this.incomplete += 1 else this.incomplete += 1
var id = params.type === 'ws' ? params.peer_id : params.addr var id = params.type === 'ws' ? params.peer_id : params.addr
peer = this.peers[id] = { peer = this.peers.set(id, {
type: params.type, type: params.type,
complete: params.left === 0, complete: params.left === 0,
peerId: params.peer_id, // as hex peerId: params.peer_id, // as hex
ip: params.ip, ip: params.ip,
port: params.port, port: params.port,
socket: params.socket // only websocket socket: params.socket // only websocket
} })
} }
Swarm.prototype._onAnnounceStopped = function (params, peer) { Swarm.prototype._onAnnounceStopped = function (params, peer) {
@ -70,7 +75,7 @@ Swarm.prototype._onAnnounceStopped = function (params, peer) {
if (peer.complete) this.complete -= 1 if (peer.complete) this.complete -= 1
else this.incomplete -= 1 else this.incomplete -= 1
var id = params.type === 'ws' ? params.peer_id : params.addr var id = params.type === 'ws' ? params.peer_id : params.addr
delete this.peers[id] this.peers.remove(id)
} }
Swarm.prototype._onAnnounceCompleted = function (params, peer) { Swarm.prototype._onAnnounceCompleted = function (params, peer) {
@ -103,10 +108,11 @@ Swarm.prototype._onAnnounceUpdate = function (params, peer) {
Swarm.prototype._getPeers = function (numwant, ownPeerId, isWebRTC) { Swarm.prototype._getPeers = function (numwant, ownPeerId, isWebRTC) {
var peers = [] var peers = []
var ite = randomIterate(Object.keys(this.peers)) var ite = randomIterate(Object.keys(this.peers.cache))
var peerId var peerId
while ((peerId = ite()) && peers.length < numwant) { while ((peerId = ite()) && peers.length < numwant) {
var peer = this.peers[peerId] // Don't mark the peer as most recently used on announce
var peer = this.peers.peek(peerId)
if (isWebRTC && peer.peerId === ownPeerId) continue // don't send peer to itself 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 if ((isWebRTC && peer.type !== 'ws') || (!isWebRTC && peer.type === 'ws')) continue // send proper peer type
peers.push(peer) peers.push(peer)

View File

@ -27,6 +27,7 @@
"hat": "0.0.3", "hat": "0.0.3",
"inherits": "^2.0.1", "inherits": "^2.0.1",
"ip": "^1.0.1", "ip": "^1.0.1",
"lru": "^2.0.1",
"minimist": "^1.1.1", "minimist": "^1.1.1",
"once": "^1.3.0", "once": "^1.3.0",
"random-iterate": "^1.0.1", "random-iterate": "^1.0.1",

View File

@ -50,6 +50,9 @@ function Server (opts) {
self._trustProxy = !!opts.trustProxy self._trustProxy = !!opts.trustProxy
if (typeof opts.filter === 'function') self._filter = opts.filter if (typeof opts.filter === 'function') self._filter = opts.filter
self.peersCacheLength = opts.peersCacheLength
self.peersCacheTtl = opts.peersCacheTtl
self._listenCalled = false self._listenCalled = false
self.listening = false self.listening = false
self.destroyed = false self.destroyed = false
@ -153,7 +156,7 @@ function Server (opts) {
if (req.method === 'GET' && (req.url === '/stats' || req.url === '/stats.json')) { if (req.method === 'GET' && (req.url === '/stats' || req.url === '/stats.json')) {
infoHashes.forEach(function (infoHash) { infoHashes.forEach(function (infoHash) {
var peers = self.torrents[infoHash].peers var peers = self.torrents[infoHash].peers
var keys = Object.keys(peers) var keys = Object.keys(peers.cache)
if (keys.length > 0) activeTorrents++ if (keys.length > 0) activeTorrents++
keys.forEach(function (peerId) { keys.forEach(function (peerId) {
@ -165,7 +168,8 @@ function Server (opts) {
leecher: false leecher: false
} }
} }
var peer = peers[peerId] // Don't mark the peer as most recently used for stats
var peer = peers.peek(peerId)
if (peer.ip.indexOf(':') >= 0) { if (peer.ip.indexOf(':') >= 0) {
allPeers[peerId].ipv6 = true allPeers[peerId].ipv6 = true
} else { } else {
@ -489,7 +493,8 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) {
if (!swarm) { if (!swarm) {
return self.emit('warning', new Error('no swarm with that `info_hash`')) return self.emit('warning', new Error('no swarm with that `info_hash`'))
} }
var toPeer = swarm.peers[params.to_peer_id] // Mark the destination peer as recently used in cache
var toPeer = swarm.peers.get(params.to_peer_id)
if (!toPeer) { if (!toPeer) {
return self.emit('warning', new Error('no peer with that `to_peer_id`')) return self.emit('warning', new Error('no peer with that `to_peer_id`'))
} }

View File

@ -52,22 +52,23 @@ function serverTest (t, serverType, serverFamily) {
t.equal(Object.keys(server.torrents).length, 1) t.equal(Object.keys(server.torrents).length, 1)
t.equal(swarm.complete, 0) t.equal(swarm.complete, 0)
t.equal(swarm.incomplete, 1) t.equal(swarm.incomplete, 1)
t.equal(Object.keys(swarm.peers).length, 1) t.equal(Object.keys(swarm.peers.cache).length, 1)
var id = serverType === 'ws' var id = serverType === 'ws'
? peerId.toString('hex') ? peerId.toString('hex')
: hostname + ':6881' : hostname + ':6881'
t.equal(swarm.peers[id].type, serverType) var peer = swarm.peers.peek(id)
t.equal(swarm.peers[id].ip, clientIp) t.equal(peer.type, serverType)
t.equal(swarm.peers[id].peerId, peerId.toString('hex')) t.equal(peer.ip, clientIp)
t.equal(swarm.peers[id].complete, false) t.equal(peer.peerId, peerId.toString('hex'))
t.equal(peer.complete, false)
if (serverType === 'ws') { if (serverType === 'ws') {
t.equal(typeof swarm.peers[id].port, 'number') t.equal(typeof peer.port, 'number')
t.ok(swarm.peers[id].socket) t.ok(peer.socket)
} else { } else {
t.equal(swarm.peers[id].port, 6881) t.equal(peer.port, 6881)
t.notOk(swarm.peers[id].socket) t.notOk(peer.socket)
} }
client1.complete() client1.complete()