From 9c7f6f305178e40bc6dd4e422ace1a34d6c6ec2e Mon Sep 17 00:00:00 2001 From: David Soria Parra Date: Sat, 21 Mar 2026 21:34:26 +0000 Subject: [PATCH] fix(discord): widen rate-limit jitter to de-sync parallel retries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/discord.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/discord.ts b/src/discord.ts index 2c50ad3..e1f086f 100644 --- a/src/discord.ts +++ b/src/discord.ts @@ -50,7 +50,10 @@ async function discordFetch( 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})` );