Refactor id usage in announce and fix default peersCacheTtl

This commit is contained in:
Yoann Ciabaud 2016-06-14 07:15:57 +02:00
parent d51a77b028
commit 2c7ea4e307

View File

@ -9,7 +9,7 @@ var randomIterate = require('random-iterate')
function Swarm (infoHash, server) {
this.peers = new LRU({
max: server.peersCacheLength || 1000,
maxAge: server.peersCacheTtl || 900 // 900s = 15 minutes
maxAge: server.peersCacheTtl || 900000 // 900 000ms = 15 minutes
})
this.complete = 0
this.incomplete = 0
@ -21,18 +21,15 @@ Swarm.prototype.announce = function (params, cb) {
// Mark the source peer as recently used in cache
var peer = self.peers.get(id)
// Get the peer back in swarm if missing
if (params.event === 'started' || !peer) {
self._onAnnounceStarted(params, peer)
}
if (params.event === 'stopped') {
self._onAnnounceStopped(params, peer)
if (params.event === 'started') {
self._onAnnounceStarted(params, peer, id)
} else if (params.event === 'stopped') {
self._onAnnounceStopped(params, peer, id)
} else if (params.event === 'completed') {
self._onAnnounceCompleted(params, peer)
self._onAnnounceCompleted(params, peer, id)
} else if (params.event === 'update') {
self._onAnnounceUpdate(params, peer)
} else if (params.event !== 'started') {
self._onAnnounceUpdate(params, peer, id)
} else {
cb(new Error('invalid event'))
return
}
@ -50,7 +47,7 @@ Swarm.prototype.scrape = function (params, cb) {
})
}
Swarm.prototype._onAnnounceStarted = function (params, peer) {
Swarm.prototype._onAnnounceStarted = function (params, peer, id) {
if (peer) {
debug('unexpected `started` event from peer that is already in swarm')
return this._onAnnounceUpdate(params, peer) // treat as an update
@ -58,7 +55,6 @@ Swarm.prototype._onAnnounceStarted = function (params, peer) {
if (params.left === 0) this.complete += 1
else this.incomplete += 1
var id = params.type === 'ws' ? params.peer_id : params.addr
peer = this.peers.set(id, {
type: params.type,
complete: params.left === 0,
@ -69,7 +65,7 @@ Swarm.prototype._onAnnounceStarted = function (params, peer) {
})
}
Swarm.prototype._onAnnounceStopped = function (params, peer) {
Swarm.prototype._onAnnounceStopped = function (params, peer, id) {
if (!peer) {
debug('unexpected `stopped` event from peer that is not in swarm')
return // do nothing
@ -77,11 +73,10 @@ Swarm.prototype._onAnnounceStopped = function (params, peer) {
if (peer.complete) this.complete -= 1
else this.incomplete -= 1
var id = params.type === 'ws' ? params.peer_id : params.addr
this.peers.remove(id)
}
Swarm.prototype._onAnnounceCompleted = function (params, peer) {
Swarm.prototype._onAnnounceCompleted = function (params, peer, id) {
if (!peer) {
debug('unexpected `completed` event from peer that is not in swarm')
return this._onAnnounceStarted(params, peer) // treat as a start
@ -94,11 +89,10 @@ Swarm.prototype._onAnnounceCompleted = function (params, peer) {
this.complete += 1
this.incomplete -= 1
peer.complete = true
var id = params.type === 'ws' ? params.peer_id : params.addr
this.peers.set(id, peer)
}
Swarm.prototype._onAnnounceUpdate = function (params, peer) {
Swarm.prototype._onAnnounceUpdate = function (params, peer, id) {
if (!peer) {
debug('unexpected `update` event from peer that is not in swarm')
return this._onAnnounceStarted(params, peer) // treat as a start
@ -108,7 +102,6 @@ Swarm.prototype._onAnnounceUpdate = function (params, peer) {
this.complete += 1
this.incomplete -= 1
peer.complete = true
var id = params.type === 'ws' ? params.peer_id : params.addr
this.peers.set(id, peer)
}
}