Struggling to figure out why my SvelteKit server route keeps getting throttled when updating 100+ users via the Genesys Cloud Platform API. I am using the fetch API with a simple loop to PATCH /api/v2/users/{userId} for status changes. The first 50 requests succeed, but then I hit a wall of 429 Too Many Requests errors. My current retry logic is just a hard setTimeout(1000), which is inefficient and still fails under load. I need to implement proper exponential backoff based on the Retry-After header or the 429 response body. Here is my current implementation:
async function updateUserStatus(userId, status) {
const res = await fetch(`https://{{myorg}}.mygenesys.com/api/v2/users/${userId}`, {
method: 'PATCH',
headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ status })
});
if (res.status === 429) {
await new Promise(r => setTimeout(r, 1000)); // Naive retry
return updateUserStatus(userId, status);
}
return res.json();
}
How do I correctly parse the rate limit headers and implement a robust backoff strategy in this async context without blocking the event loop?