Repository navigation
Clarify GitHub API quota failures and reset diagnostics #68737
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
+523
−26
Merged
Changes from 1 commit
Commits
Show all changes
2 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
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
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
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // @ts-check | ||
|
|
||
| const { isRateLimitError } = require("./error_helpers.cjs"); | ||
|
|
||
| function getGitHubAPIQuota(headers, now = Date.now(), quotaStopped = false) { | ||
| const header = name => (typeof headers?.get === "function" ? headers.get(name) : headers?.[name]); | ||
| const integer = value => { | ||
| if (typeof value !== "string" || !/^\d{1,16}$/.test(value)) return null; | ||
| const number = Number(value); | ||
| return Number.isSafeInteger(number) ? number : null; | ||
| }; | ||
| const remaining = integer(header("x-ratelimit-remaining")); | ||
| const limit = integer(header("x-ratelimit-limit")); | ||
| const used = integer(header("x-ratelimit-used")); | ||
| const resetSeconds = integer(header("x-ratelimit-reset")); | ||
| const resetMs = resetSeconds === null ? NaN : resetSeconds * 1000; | ||
| const reset = Number.isFinite(resetMs) && resetMs > 0 && resetMs <= 8640000000000000 ? new Date(resetMs).toISOString() : ""; | ||
| const retrySeconds = integer(header("retry-after")); | ||
| const retryHeader = header("retry-after"); | ||
| const retryMs = retrySeconds === null ? (typeof retryHeader === "string" && retryHeader.length <= 64 ? Date.parse(retryHeader) : NaN) : now + retrySeconds * 1000; | ||
| const retryAt = Number.isFinite(retryMs) && retryMs > now && retryMs <= 8640000000000000 ? new Date(retryMs).toISOString() : ""; | ||
| const deadlines = [reset && (remaining === 0 || quotaStopped) && resetMs > now ? resetMs : NaN, retryAt ? retryMs : NaN].filter(Number.isFinite); | ||
| const deadline = Math.max(...deadlines); | ||
| const retryNotBefore = Number.isFinite(deadline) && deadline > now && deadline <= 8640000000000000 ? new Date(deadline).toISOString() : ""; | ||
| const resourceHeader = header("x-ratelimit-resource"); | ||
| const resource = ["core", "search", "graphql", "code_search", "integration_manifest", "actions_runner_registration"].includes(resourceHeader) ? resourceHeader : "unknown"; | ||
| return { resource, remaining, limit, used, reset, retryRestricted: retryHeader != null, retryNotBefore }; | ||
| } | ||
|
|
||
| /** | ||
| * Derive diagnostics only from the failed business response, never another | ||
| * client's /rate_limit precheck. Publish validated metadata, not API payloads. | ||
| * @param {any} error | ||
| * @param {number} [now] | ||
| */ | ||
| function getGitHubAPIDiagnostic(error, now = Date.now()) { | ||
| const quotaStopped = error?.code === "DAILY_AIC_QUOTA_STOP"; | ||
| const metadata = getGitHubAPIQuota(error?.response?.headers, now, quotaStopped); | ||
|
pelikhan marked this conversation as resolved.
|
||
| const { resource, remaining, limit, used, reset, retryRestricted, retryNotBefore } = metadata; | ||
| const status = Number(error?.status || error?.response?.status || 0); | ||
| const secondary = typeof error?.message === "string" && /\bsecondary rate limit\b/i.test(error.message); | ||
| const limited = status === 429 || (status === 403 && (remaining === 0 || retryRestricted || isRateLimitError(error) || secondary)); | ||
|
pelikhan marked this conversation as resolved.
Outdated
|
||
| const quota = quotaStopped || limited; | ||
| if (!quota && status !== 401 && status !== 403) return null; | ||
|
|
||
| const classification = status === 401 ? "authentication_failed" : status === 403 && !limited ? "permission_denied" : quota ? (remaining === 0 ? "quota_exhausted" : quotaStopped ? "quota_reserve" : "rate_limited") : "permission_denied"; | ||
|
pelikhan marked this conversation as resolved.
Outdated
|
||
| const details = quota | ||
| ? `resource=${resource}; remaining=${remaining ?? "unknown"}; limit=${limit ?? "unknown"}; used=${used ?? "unknown"}; reset=${reset || "unknown"}; retry_not_before=${retryNotBefore || "unknown"}${quotaStopped && !limited && (status === 401 || status === 403) ? "; quota reserve guard also stopped further reads" : ""}` | ||
| : "Check the credential's access to this operation; quota exhaustion was not established."; | ||
| return { quota, classification, message: `GitHub API ${classification} (HTTP ${status >= 100 && status <= 599 && Number.isInteger(status) ? status : "unknown"}; ${details})` }; | ||
| } | ||
|
|
||
| module.exports = { getGitHubAPIQuota, getGitHubAPIDiagnostic }; | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.