fix(discord): widen rate-limit jitter to de-sync parallel retries

The 250ms jitter was too narrow for ~68 parallel refresh reads — all
retries clustered after the same retry_after and collided again. Scale
jitter linearly with attempt number so the herd progressively spreads
out across attempts.
This commit is contained in:
David Soria Parra 2026-03-21 21:34:26 +00:00
commit 9c7f6f3051
No known key found for this signature in database

View file

@ -50,7 +50,10 @@ async function discordFetch<T>(
if (response.status === 429) {
const body = (await response.json()) as DiscordRateLimitResponse;
const retryAfterMs = Math.ceil(body.retry_after * 1000) + Math.random() * 250;
// Linearly increasing jitter de-syncs the thundering herd when many
// resources refresh in parallel and all receive the same retry_after.
const jitterMs = Math.random() * 1000 * (attempt + 1);
const retryAfterMs = Math.ceil(body.retry_after * 1000) + jitterMs;
lastError = new Error(
`Discord API rate limited on ${endpoint} (retry_after=${body.retry_after}s, global=${body.global})`
);