async function makeRequestWithRetry(url, options, maxRetries = 3) {
const baseDelay = 1000; // Base delay of 1 second
const maxDelay = 32000; // Maximum delay of 32 seconds
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
const response = await fetch(url, options);
if (response.status === 429) {
// Calculate exponential backoff
const exponentialDelay = Math.min(
baseDelay * Math.pow(2, attempt),
maxDelay
);
// Add random jitter (between 0-100% of the delay)
const jitter = Math.random() * exponentialDelay;
const totalDelay = exponentialDelay + jitter;
console.log(`Rate limited. Retrying in ${Math.round(totalDelay/1000)} seconds...`);
await new Promise(resolve => setTimeout(resolve, totalDelay));
continue;
}
return response;
} catch (error) {
if (attempt === maxRetries - 1) throw error;
}
}
}