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.
This commit is contained in:
parent
d65603b566
commit
b543112dbb
2 changed files with 16 additions and 1 deletions
14
js/api.js
14
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) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue