From 1b17782114cf6e0cbbf66aafd3a542bc9e1d803e Mon Sep 17 00:00:00 2001 From: Luke Hagar Date: Thu, 4 Dec 2025 21:37:27 +0000 Subject: [PATCH] Enhance retry logic in getReposContributorsStats to use exponential backoff for handling 202 responses. Update related comments for clarity and improve logging for retry attempts. --- src/index.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 80fe516..1b91cd2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,8 +18,8 @@ import type { GraphQlQueryResponseData } from "@octokit/graphql"; const ThrottledOctokit = Octokit.plugin(throttling); // Constants -const MAX_RETRY_COUNT = 10; -const RETRY_DELAY_MS = 1000; +const MAX_RETRY_COUNT = 10; // With exponential backoff: 1+2+4+8+16+32+64+128+256+512 = ~17 minutes max +const INITIAL_RETRY_DELAY_MS = 1000; /** * Log rate limit information from GraphQL response @@ -303,7 +303,7 @@ export async function getUsersStars(octokit: Octokit, username: string) { } /** - * Get contributor stats for a repo with retry logic for 202 responses + * Get contributor stats for a repo with exponential backoff for 202 responses */ export async function getReposContributorsStats( octokit: Octokit, @@ -319,11 +319,22 @@ export async function getReposContributorsStats( if (response.status === 202) { if (retryCount >= MAX_RETRY_COUNT) { - console.warn(`Max retries reached for ${owner}/${repo}, skipping`); + console.warn(`Max retries (${MAX_RETRY_COUNT}) reached for ${owner}/${repo} after exponential backoff, skipping`); return undefined; } - await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY_MS)); + // Exponential backoff: 1s, 2s, 4s, 8s, 16s, 32s, 64s, 128s, 256s, 512s + const delay = INITIAL_RETRY_DELAY_MS * Math.pow(2, retryCount); + const maxDelay = 60000; // Cap at 60 seconds per retry + const actualDelay = Math.min(delay, maxDelay); + + if (retryCount === 0) { + console.log(`[202] ${owner}/${repo}: GitHub computing stats, waiting ${actualDelay}ms...`); + } else { + console.log(`[202] ${owner}/${repo}: Retry ${retryCount + 1}/${MAX_RETRY_COUNT}, waiting ${actualDelay}ms...`); + } + + await new Promise((resolve) => setTimeout(resolve, actualDelay)); return getReposContributorsStats(octokit, owner, repo, retryCount + 1); }