Skip to content

Commit b8b1753

Browse files
committed
feat(api): Completely destroy webtorrent when done
1 parent f3ffc19 commit b8b1753

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

apps/api/src/shared/torrent/torrent.service.ts

+45-10
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ export class TorrentService {
101101
* @param wasCrash
102102
*/
103103
private setupWebTorrent(wasCrash = false) {
104+
// If there is already a instance we don't need to do anything
105+
if (this.webTorrent !== null) {
106+
return
107+
}
108+
109+
this.logger.log('Creating new WebTorrent client')
110+
104111
this.webTorrent = new WebTorrent({
105112
maxConns: 55 // Is the default
106113
})
@@ -119,12 +126,31 @@ export class TorrentService {
119126
}
120127
}
121128

129+
/**
130+
* Cleanup / destroy WebTorrent when there are not downloads / torrents left
131+
*/
132+
private cleanupWebTorrent(): void {
133+
// Check if we have a client, downloads and torrents if not then destroy the client
134+
if (this.webTorrent && this.connectingTorrents.length === 0 && this.torrents.length === 0 && this.downloads.length === 0) {
135+
this.webTorrent?.destroy((err) => {
136+
if (err) {
137+
this.logger.error('Error cleaning up WebTorrent', err.toString())
138+
139+
} else {
140+
this.logger.log('No torrents left, WebTorrent client destroyed')
141+
}
142+
})
143+
144+
this.webTorrent = null
145+
}
146+
}
147+
122148
/**
123149
* Starts the streaming process of one item
124150
*
125151
* @param download
126152
*/
127-
public startStreaming(download: Model<Download>) {
153+
public startStreaming(download: Model<Download>): void {
128154
this.logger.log(`[${download._id}]: Start streaming`)
129155

130156
this.download(download)
@@ -204,6 +230,12 @@ export class TorrentService {
204230
*/
205231
public async startDownloads(): Promise<void> {
206232
if (this.backgroundDownloading || this.downloads.length === 0) {
233+
// If start downloads is called and we have no downlaods then also cleanup
234+
// WebTorrent client to be sure
235+
if (this.downloads.length === 0) {
236+
this.cleanupWebTorrent()
237+
}
238+
207239
return
208240
}
209241

@@ -222,6 +254,8 @@ export class TorrentService {
222254

223255
// We are no longer downloading to disable
224256
this.backgroundDownloading = false
257+
// Cleanup WebTorrent if needed
258+
this.cleanupWebTorrent()
225259
}
226260

227261
/**
@@ -250,7 +284,10 @@ export class TorrentService {
250284
*
251285
* @param {Download} download - Item to download
252286
*/
253-
private async download(download: Download) {
287+
private async download(download: Download): Promise<void> {
288+
// Make sure WebTorrent is setup
289+
this.setupWebTorrent()
290+
254291
return new Promise((async (resolve) => {
255292
this.logger.log(`[${download._id}]: Start download`)
256293

@@ -317,7 +354,7 @@ export class TorrentService {
317354
maxWebConns: 5,
318355
announce: this.trackers
319356
},
320-
this.handleTorrent(resolve, item, download, magnet)
357+
this.handleTorrent(resolve, item, download)
321358
)
322359

323360
// Add to active torrents array
@@ -331,13 +368,8 @@ export class TorrentService {
331368

332369
/**
333370
* Handles the torrent and resolves when the torrent is done
334-
*
335-
* @param resolve
336-
* @param item
337-
* @param download
338-
* @param magnet
339371
*/
340-
private handleTorrent(resolve, item, download, magnet) {
372+
private handleTorrent(resolve, item, download) {
341373
return (torrent: Torrent) => {
342374
// Let's make sure all the not needed files are deselected
343375
const { file } = torrent.files.reduce((previous, current, index) => {
@@ -434,7 +466,7 @@ export class TorrentService {
434466
const now = Date.now()
435467
// Only update every 1 second
436468
if (lastUpdate === null || (lastUpdate + 1000) < now) {
437-
this.logger.debug(`[${download._id}]: Progress ${newProgress.toFixed(1)}% at ${formatKbToString(torrent.downloadSpeed)}`)
469+
this.logger.debug(`[${download._id}]: Progress ${newProgress.toFixed(2)}% at ${formatKbToString(torrent.downloadSpeed)} (${torrent.timeRemaining})`)
438470

439471
lastUpdate = now
440472

@@ -485,6 +517,9 @@ export class TorrentService {
485517
// Remove from the queue as the item is downloaded
486518
this.removeFromDownloads(download)
487519

520+
// Cleanup WebTorrent if there is nothing left anymore
521+
this.cleanupWebTorrent()
522+
488523
// Wait at-least 0,5 second before updating, this is to prevent
489524
// a double save happening
490525
setTimeout(async () => {

0 commit comments

Comments
 (0)