Skip to content

Commit f62eb74

Browse files
committed
release swarm after all peers timeouted
1 parent 1f512ec commit f62eb74

File tree

3 files changed

+54
-23
lines changed

3 files changed

+54
-23
lines changed

lib/server/swarm.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import LRU from 'lru'
55
import randomIterate from 'random-iterate'
66

77
const debug = Debug('bittorrent-tracker:swarm')
8+
const MAX_SWARM_LIFETIME = 20 * 60 * 1000; // 20 minutes
89

910
const getLru = (options) => {
1011
if (!globalThis?.swarmHijack) { return new LRU(options); }
@@ -99,11 +100,11 @@ class Swarm {
99100
self.infoHash = infoHash
100101
self.complete = 0
101102
self.incomplete = 0
103+
self.lifeTime = server.peersCacheTtl || MAX_SWARM_LIFETIME;
102104

103105
self.peers = getLru({
104106
max: server.peersCacheLength || 1000,
105-
maxAge: server.peersCacheTtl || 20 * 60 * 1000, // 20 minutes
106-
infoHash,
107+
maxAge: self.lifeTime, infoHash,
107108
})
108109

109110
// When a peer is evicted from the LRU store, send a synthetic 'stopped' event
@@ -119,8 +120,23 @@ class Swarm {
119120
await self._onAnnounceStopped(params, peer, peer.peerId)
120121
peer.socket = null
121122
})
123+
124+
this.touch();
122125
}
123126

127+
touch () {
128+
return this.touched = new Date();
129+
};
130+
131+
shouldRelease () {
132+
return (new Date() - this.touched > MAX_SWARM_LIFETIME);
133+
};
134+
135+
free () {
136+
console.log('Freeing torrent 1');
137+
return this.peers = null; // @todo: not sure if this is enough for free LRU
138+
};
139+
124140
async announce (params, cb) {
125141
const self = this
126142
const id = params.type === 'ws' ? params.peer_id : params.addr
@@ -142,6 +158,9 @@ class Swarm {
142158
cb(new Error('invalid event'))
143159
return
144160
}
161+
162+
this.touch();
163+
145164
cb(null, {
146165
complete: self.complete,
147166
incomplete: self.incomplete,

package.json

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,28 @@
2828
"type": "module",
2929
"dependencies": {
3030
"bencode": "^3.0.3",
31-
"bittorrent-peerid": "^1.3.3",
32-
"bn.js": "^5.2.0",
31+
"bittorrent-peerid": "^1.3.6",
32+
"bn.js": "^5.2.1",
3333
"chrome-dgram": "^3.0.6",
34-
"clone": "^2.0.0",
34+
"clone": "^2.1.2",
3535
"compact2string": "^1.4.1",
36-
"debug": "^4.1.1",
37-
"ip": "^1.1.5",
36+
"debug": "^4.3.4",
37+
"ip": "^1.1.8",
3838
"lru": "^3.1.0",
39-
"minimist": "^1.2.5",
39+
"minimist": "^1.2.8",
4040
"once": "^1.4.0",
4141
"queue-microtask": "^1.2.3",
4242
"random-iterate": "^1.0.1",
4343
"randombytes": "^2.1.0",
4444
"run-parallel": "^1.2.0",
4545
"run-series": "^1.1.9",
46-
"simple-get": "^4.0.0",
47-
"simple-peer": "^9.11.0",
46+
"simple-get": "^4.0.1",
47+
"simple-peer": "^9.11.1",
4848
"simple-websocket": "^9.1.0",
49-
"socks": "^2.0.0",
50-
"string2compact": "^2.0.0",
49+
"socks": "^2.7.1",
50+
"string2compact": "^2.0.1",
5151
"unordered-array-remove": "^1.0.2",
52-
"ws": "^8.0.0"
52+
"ws": "^8.13.0"
5353
},
5454
"devDependencies": {
5555
"@mapbox/node-pre-gyp": "1.0.10",
@@ -59,7 +59,7 @@
5959
"semantic-release": "20.1.3",
6060
"standard": "*",
6161
"tape": "5.6.3",
62-
"utilitas": "^1992.4.5",
62+
"utilitas": "^1992.4.7",
6363
"webtorrent-fixtures": "2.0.2",
6464
"wrtc": "0.4.7"
6565
},
@@ -82,8 +82,8 @@
8282
"license": "MIT",
8383
"main": "index.js",
8484
"optionalDependencies": {
85-
"bufferutil": "^4.0.3",
86-
"utf-8-validate": "^5.0.5"
85+
"bufferutil": "^4.0.7",
86+
"utf-8-validate": "^6.0.3"
8787
},
8888
"repository": {
8989
"type": "git",
@@ -92,7 +92,9 @@
9292
"scripts": {
9393
"preversion": "npm run update-authors",
9494
"test": "standard && tape test/*.js",
95-
"update-authors": "./tools/update-authors.sh"
95+
"update-authors": "./tools/update-authors.sh",
96+
"upstream": "git pull [email protected]:webtorrent/bittorrent-tracker.git",
97+
"updep": "npx npm-check-updates -u && npm install"
9698
},
9799
"funding": [
98100
{

server.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const hasOwnProperty = Object.prototype.hasOwnProperty
3434
* @param {function} opts.filter black/whitelist fn for disallowing/allowing torrents
3535
*/
3636
class Server extends EventEmitter {
37-
constructor (opts = {}) {
37+
constructor(opts = {}) {
3838
super()
3939
debug('new server %s', JSON.stringify(opts))
4040

@@ -295,7 +295,7 @@ class Server extends EventEmitter {
295295
this.emit('error', err)
296296
}
297297

298-
listen (...args) /* port, hostname, onlistening */{
298+
listen (...args) /* port, hostname, onlistening */ {
299299
if (this._listenCalled || this.listening) throw new Error('server already listening')
300300
this._listenCalled = true
301301

@@ -330,19 +330,19 @@ class Server extends EventEmitter {
330330
if (this.udp4) {
331331
try {
332332
this.udp4.close()
333-
} catch (err) {}
333+
} catch (err) { }
334334
}
335335

336336
if (this.udp6) {
337337
try {
338338
this.udp6.close()
339-
} catch (err) {}
339+
} catch (err) { }
340340
}
341341

342342
if (this.ws) {
343343
try {
344344
this.ws.close()
345-
} catch (err) {}
345+
} catch (err) { }
346346
}
347347

348348
if (this.http) this.http.close(cb)
@@ -692,10 +692,20 @@ class Server extends EventEmitter {
692692
} // else, return full peer objects (used for websocket responses)
693693

694694
cb(null, response)
695+
self.free();
695696
})
696697
}
697698
}
698699

700+
free () {
701+
for (let i in this.torrents) {
702+
if (!this.torrents[i].shouldRelease()) { continue; }
703+
const toFree = this.torrents[i];
704+
delete this.torrents[i];
705+
toFree?.free?.();
706+
}
707+
}
708+
699709
_onScrape (params, cb) {
700710
if (params.info_hash == null) {
701711
// if info_hash param is omitted, stats for all torrents are returned
@@ -804,6 +814,6 @@ function toNumber (x) {
804814
return x >= 0 ? x : false
805815
}
806816

807-
function noop () {}
817+
function noop () { }
808818

809819
export default Server

0 commit comments

Comments
 (0)