From d924c64710c0eac738c7d6ac58eed9e49f3dadef Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Sat, 18 May 2024 14:55:56 -0300 Subject: [PATCH] Update web-torrent-data.ts Small adjustment to webTorrentData logic --- src/main/services/web-torrent-data.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/services/web-torrent-data.ts b/src/main/services/web-torrent-data.ts index 7d5ec34d..72f29be6 100644 --- a/src/main/services/web-torrent-data.ts +++ b/src/main/services/web-torrent-data.ts @@ -5,25 +5,34 @@ type WebTorrentHealthData = { peers: number; }; +const MILLISECONDS = 1000; +const SECONDS = 1.5; + export const webTorrentData = { async getSeedersAndPeers( magnet: string - ): Promise<{ seeders: number; peers: number }> { + ): Promise<{ seeders: number; peers: number } | null> { let peers = 0; let seeds = 0; let retry = 0; + let timeout = SECONDS * MILLISECONDS; - while (retry < 3 && (!peers || !seeds)) { + while (retry < 3) { try { const data: WebTorrentHealthData = await webTorrentHealth(magnet, { - timeout: 1500 * (retry + 1), + timeout, }); peers = data.peers; seeds = data.seeds; + + if (peers || seeds) break; } catch (e) { - retry++; + return null; } + + timeout *= 2; + retry++; } return { peers, seeders: seeds };