From b543112dbb3fac69dde830ae3d29127c33e963e0 Mon Sep 17 00:00:00 2001 From: Julien Maille Date: Wed, 7 Jan 2026 22:49:07 +0100 Subject: [PATCH] Fix: Improve API rate limit handling during CSV import - Update 'fetchWithRetry' in api.js to respect Retry-After header and use exponential backoff for 429 errors. - Add 300ms delay between requests in CSV import loop to reduce load. --- js/api.js | 14 +++++++++++++- js/app.js | 3 +++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/js/api.js b/js/api.js index 44b71c1..324182d 100644 --- a/js/api.js +++ b/js/api.js @@ -48,7 +48,19 @@ export class LosslessAPI { const response = await fetch(url, { signal: options.signal }); if (response.status === 429) { - throw new Error(RATE_LIMIT_ERROR_MESSAGE); + const retryAfter = response.headers.get('Retry-After'); + let waitTime = 2000 * attempt; // Default exponential backoff + + if (retryAfter) { + const seconds = parseInt(retryAfter, 10); + if (!isNaN(seconds)) { + waitTime = seconds * 1000; + } + } + + console.warn(`Rate limit hit. Waiting ${waitTime}ms before retry ${attempt}/${maxRetries}...`); + await delay(waitTime); + continue; } if (response.ok) { diff --git a/js/app.js b/js/app.js index 0476dd8..69ae0da 100644 --- a/js/app.js +++ b/js/app.js @@ -1058,6 +1058,9 @@ async function parseCSV(csvText, api, onProgress) { // Search for the track in hifi tidal api's catalog if (trackTitle && artistNames) { + // Add a small delay to prevent rate limiting + await new Promise(resolve => setTimeout(resolve, 300)); + try { const searchQuery = `${trackTitle} ${artistNames}`; const searchResults = await api.searchTracks(searchQuery);