-
-
Notifications
You must be signed in to change notification settings - Fork 583
Handle GitHub API rate limits in script #2471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import fs from "node:fs/promises"; | ||
import timers from "node:timers/promises"; | ||
import { URL } from "node:url"; | ||
|
||
const MAINTAINERS = { | ||
|
@@ -220,16 +221,61 @@ class UserFetchError extends Error { | |
} | ||
} | ||
|
||
async function fetchUserInfo(username) { | ||
const res = await fetch(`https://api.github.com/users/${username}`, { | ||
const BACKOFF_INTERVALS_MINUTES = [1, 2, 3, 5]; | ||
const MAX_RETRIES = BACKOFF_INTERVALS_MINUTES.length - 1; | ||
|
||
async function rateLimitDelay({ retryAfter, ratelimitReset, retryCount }) { | ||
let timeoutInMilliseconds = BACKOFF_INTERVALS_MINUTES[retryCount] * 1000; | ||
if (retryAfter) { | ||
timeoutInMilliseconds = retryAfter * 1000; | ||
} else if (ratelimitRemaining === "0" && ratelimitReset) { | ||
timeoutInMilliseconds = ratelimitReset * 1000 - Date.now(); | ||
} | ||
|
||
const timeoutInSeconds = (timeoutInMilliseconds / 1000).toLocaleString(); | ||
console.warn(`Waiting for ${timeoutInSeconds} seconds...`); | ||
|
||
await timers.setTimeout(timeoutInMilliseconds); | ||
} | ||
|
||
async function fetchUserInfo(username, retryCount = 0) { | ||
if (retryCount >= MAX_RETRIES) { | ||
throw new Error(`Hit max retries (${MAX_RETRIES}) for fetching user ${username}`); | ||
} | ||
|
||
const request = new Request(`https://api.github.com/users/${username}`, { | ||
headers: { | ||
Accept: "application/vnd.github+json", | ||
"X-GitHub-Api-Version": "2022-11-28", | ||
}, | ||
}); | ||
if (process.env.GITHUB_TOKEN) { | ||
request.headers.set("Authorization", `Bearer ${process.env.GITHUB_TOKEN}`); | ||
} | ||
|
||
const res = await fetch(request); | ||
|
||
if (!res.ok) { | ||
const retryAfter = res.headers.get("retry-after"); // seconds | ||
const ratelimitRemaining = res.headers.get("x-ratelimit-remaining"); // quantity of requests remaining | ||
const ratelimitReset = res.headers.get("x-ratelimit-reset"); // UTC epoch seconds | ||
|
||
if (retryAfter || ratelimitRemaining === "0" || ratelimitReset) { | ||
// See https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api?apiVersion=2022-11-28#handle-rate-limit-errors-appropriately | ||
console.warn("Rate limited by GitHub API"); | ||
|
||
await rateLimitDelay({ | ||
retryAfter, | ||
ratelimitReset, | ||
retryCount, | ||
}); | ||
|
||
return await fetchUserInfo(username, retryCount + 1); | ||
} | ||
|
||
throw new UserFetchError(`${res.url} responded with ${res.status}`, res); | ||
} | ||
|
||
return await res.json(); | ||
} | ||
|
||
|
@@ -282,4 +328,6 @@ async function main() { | |
} | ||
} | ||
|
||
main(); | ||
if (import.meta.main) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heh :) First time I see this in JS/TS. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😄 it's the ESM equivalent of |
||
main(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only do the rate-limit retry if we get a 429? (
res.status === 429
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally I'd concur, but after reading the docs on handling rate limit errors appropriately, I think retrying in this manner should cover us.