Skip to content

Commit

Permalink
fix: trying to add more error handling for ratelimit
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaochunqi committed Sep 23, 2024
1 parent bd1d3ec commit cab2402
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions src/fetchRepos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ function formatDate(date: string) {

const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

const exponentialBackoff = (retryCount: number) => {
const baseDelay = 60000; // 1 minute
return Math.min(baseDelay * Math.pow(2, retryCount), 600000); // Max 10 minutes
};

export const fetchRepos = async (
github: GithubClient,
collection: Repo[],
username: string,
after = ``
after = ``,
retryCount = 0
) => {
const query = `
query($login: String!, $after: String!) {
Expand Down Expand Up @@ -72,27 +78,38 @@ export const fetchRepos = async (

if (starred.pageInfo?.hasNextPage) {
// 添加延迟以避免频繁请求
await delay(1000);
await delay(5000); // Increased delay to 5 seconds
await fetchRepos(
github,
collection,
username,
starred.pageInfo.endCursor
starred.pageInfo.endCursor,
0 // Reset retry count on successful request
);
}
} catch (error) {
if (
(error as any).status === 403 &&
(error as any).message.includes("API rate limit exceeded")
) {
const resetTime = new Date(
(error as any).headers["x-ratelimit-reset"] * 1000
);
warning(
`API rate limit exceeded. Waiting until ${resetTime} to continue...`
);
await delay(Date.now() - resetTime.getTime());
await fetchRepos(github, collection, username, after);
} catch (error: any) {
if (error.status === 403) {
if (error.message.includes("API rate limit exceeded")) {
// Handle primary rate limit
const resetTime = new Date(error.headers["x-ratelimit-reset"] * 1000);
warning(
`API rate limit exceeded. Waiting until ${resetTime} to continue...`
);
await delay(resetTime.getTime() - Date.now());
} else if (error.message.includes("secondary rate limit")) {
// Handle secondary rate limit
const waitTime = exponentialBackoff(retryCount);
warning(
`Secondary rate limit hit. Waiting for ${
waitTime / 1000
} seconds before retrying...`
);
await delay(waitTime);
} else {
throw error;
}
// Retry the request
await fetchRepos(github, collection, username, after, retryCount + 1);
} else {
throw error;
}
Expand Down

0 comments on commit cab2402

Please sign in to comment.