From 12c44a611e65629d180b6d2c327f06d999e1accf Mon Sep 17 00:00:00 2001 From: Gronod Date: Fri, 22 May 2026 22:25:11 +0100 Subject: [PATCH] fix: optimize polling parallelism and resolve redundant SABnzbd requests (fixes #35, fixes #36) --- server/clients/SABnzbdClient.js | 23 +++++++++++++++--- server/utils/downloadClients.js | 41 ++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/server/clients/SABnzbdClient.js b/server/clients/SABnzbdClient.js index b02bffb..fc01c68 100644 --- a/server/clients/SABnzbdClient.js +++ b/server/clients/SABnzbdClient.js @@ -45,15 +45,32 @@ class SABnzbdClient extends DownloadClient { async getActiveDownloads() { try { // Get both queue and history to provide complete picture - const [queueResponse, historyResponse, clientStatus] = await Promise.all([ + const [queueResponse, historyResponse] = await Promise.all([ this.makeRequest({ mode: 'queue' }), - this.makeRequest({ mode: 'history', limit: 10 }), - this.getClientStatus() + this.makeRequest({ mode: 'history', limit: 10 }) ]); const queueData = queueResponse.data; const historyData = historyResponse.data; + // Extract client status directly from the fetched queue data instead of making a redundant HTTP request + let clientStatus = null; + if (queueData && queueData.queue) { + const q = queueData.queue; + clientStatus = { + status: q.status, + speed: q.speed, + kbpersec: q.kbpersec, + sizeleft: q.sizeleft, + mbleft: q.mbleft, + mb: q.mb, + diskspace1: q.diskspace1, + diskspace2: q.diskspace2, + loadavg: q.loadavg, + pause_int: q.pause_int + }; + } + const downloads = []; // Process active queue items diff --git a/server/utils/downloadClients.js b/server/utils/downloadClients.js index 9cd2193..1ddc554 100644 --- a/server/utils/downloadClients.js +++ b/server/utils/downloadClients.js @@ -149,23 +149,42 @@ class DownloadClientRegistry { const clients = this.getAllClients(); const result = {}; - // Group by client type + if (clients.length === 0) { + return result; + } + + // Reset fallback flags for qBittorrent clients for (const client of clients) { + if (client.resetFallbackFlag) { + client.resetFallbackFlag(); + } + } + + // Fetch downloads from all clients in parallel + const results = await Promise.allSettled( + clients.map(async (client) => { + const downloads = await client.getActiveDownloads(); + return { + type: client.getClientType(), + downloads + }; + }) + ); + + // Group by client type + for (let i = 0; i < clients.length; i++) { + const client = clients[i]; const type = client.getClientType(); if (!result[type]) { result[type] = []; } - // Reset fallback flags for qBittorrent clients - if (client.resetFallbackFlag) { - client.resetFallbackFlag(); - } - - try { - const downloads = await client.getActiveDownloads(); - result[type].push(...downloads); - } catch (error) { - logToFile(`[DownloadClientRegistry] Error fetching from ${client.name}: ${error.message}`); + const res = results[i]; + if (res.status === 'fulfilled' && res.value) { + result[type].push(...res.value.downloads); + } else { + const errorMsg = res.status === 'rejected' ? res.reason?.message : 'Unknown error'; + logToFile(`[DownloadClientRegistry] Error fetching from ${client.name}: ${errorMsg}`); } }