Skip to content

Commit

Permalink
Support retrying Secondary Rate Limit errors (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikbuskirk-dh authored Jun 21, 2023
1 parent 6b7630e commit b6bdc1b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"@fortawesome/fontawesome-svg-core": "^6.1.2",
"@fortawesome/free-solid-svg-icons": "^6.1.2",
"@fortawesome/react-fontawesome": "^0.2.0",
"@octokit/plugin-throttling": "^4.2.0",
"@octokit/plugin-throttling": "^4.3.2",
"@octokit/rest": "^19.0.4",
"@types/lodash": "^4.14.184",
"assert-never": "^1.2.1",
Expand Down
22 changes: 10 additions & 12 deletions src/github-api/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import { Octokit } from "@octokit/rest";
import { GitHubApi } from "./api";
import { GraphQLClient, gql } from "graphql-request";

const ThrottledOctokit = Octokit.plugin(throttling as any);
const ThrottledOctokit = Octokit.plugin(throttling);
const graphQLEndpoint = "https://api.github.com/graphql";

interface ThrottlingOptions {
method: string;
url: string;
request: {
retryCount: number;
};
}

export function buildGitHubApi(token: string): GitHubApi {
Expand All @@ -21,25 +18,26 @@ export function buildGitHubApi(token: string): GitHubApi {
// Enable Draft Pull Request API.
previews: ["shadow-cat"],
throttle: {
onRateLimit: (retryAfterSeconds: number, options: ThrottlingOptions) => {
onRateLimit: (retryAfter: number, options: ThrottlingOptions, _: Octokit, retryCount: number) => {
console.warn(
`Request quota exhausted for request ${options.method} ${options.url}`
);
// Only retry twice.
if (options.request.retryCount < 2) {
console.log(`Retrying after ${retryAfterSeconds} seconds!`);
if (retryCount < 2) {
console.log(`Retrying after ${retryAfter} seconds!`);
return true;
}
return false;
},
onSecondaryRateLimit: (
_retryAfterSeconds: number,
options: ThrottlingOptions
) => {
// Does not retry, only logs a warning.
onSecondaryRateLimit: (retryAfter: number, options: ThrottlingOptions, _: Octokit, retryCount: number) => {
console.warn(
`Secondary Rate Limit detected for request ${options.method} ${options.url}`
);
// Only retry twice.
if (retryCount < 2) {
console.log(`Retrying after ${retryAfter} seconds!`);
return true;
}
return false;
},
},
Expand Down
22 changes: 17 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,11 @@
resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-13.4.0.tgz#06fe8fda93bf21bdd397fe7ef8805249efda6c06"
integrity sha512-2mVzW0X1+HDO3jF80/+QFZNzJiTefELKbhMu6yaBYbp/1gSMkVDm4rT472gJljTokWUlXaaE63m7WrWENhMDLw==

"@octokit/openapi-types@^14.0.0":
version "14.0.0"
resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-14.0.0.tgz#949c5019028c93f189abbc2fb42f333290f7134a"
integrity sha512-HNWisMYlR8VCnNurDU6os2ikx0s0VyEjDYHNS/h4cgb8DeOxQ0n72HyinUtdDVxJhFy3FWLGl0DJhfEWk3P5Iw==

"@octokit/plugin-paginate-rest@^4.0.0":
version "4.1.0"
resolved "https://registry.yarnpkg.com/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-4.1.0.tgz#670ac9ac369448c69a2371bfcd7e2b37d95534f2"
Expand All @@ -1447,12 +1452,12 @@
"@octokit/types" "^7.0.0"
deprecation "^2.3.1"

"@octokit/plugin-throttling@^4.2.0":
version "4.2.0"
resolved "https://registry.yarnpkg.com/@octokit/plugin-throttling/-/plugin-throttling-4.2.0.tgz#261f265426dcf2f5126e60b75edf547e57e24def"
integrity sha512-5QilB43MU9MgKfrQh7ZBJ/sPwasoNOazHVZd77xS7iMRu81qxH9uOt31t2QXNx6Jnc29h7eiLZIRrJAVn+7kJA==
"@octokit/plugin-throttling@^4.3.2":
version "4.3.2"
resolved "https://registry.yarnpkg.com/@octokit/plugin-throttling/-/plugin-throttling-4.3.2.tgz#d5eb363d5282c74b2839454a87545c5f90591a80"
integrity sha512-ZaCK599h3tzcoy0Jtdab95jgmD7X9iAk59E2E7hYKCAmnURaI4WpzwL9vckImilybUGrjY1JOWJapDs2N2D3vw==
dependencies:
"@octokit/types" "^7.0.0"
"@octokit/types" "^8.0.0"
bottleneck "^2.15.3"

"@octokit/request-error@^3.0.0":
Expand Down Expand Up @@ -1493,6 +1498,13 @@
dependencies:
"@octokit/openapi-types" "^13.4.0"

"@octokit/types@^8.0.0":
version "8.2.1"
resolved "https://registry.yarnpkg.com/@octokit/types/-/types-8.2.1.tgz#a6de091ae68b5541f8d4fcf9a12e32836d4648aa"
integrity sha512-8oWMUji8be66q2B9PmEIUyQm00VPDPun07umUWSaCwxmeaquFBro4Hcc3ruVoDo3zkQyZBlRvhIMEYS3pBhanw==
dependencies:
"@octokit/openapi-types" "^14.0.0"

"@polka/url@^1.0.0-next.20":
version "1.0.0-next.21"
resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.21.tgz#5de5a2385a35309427f6011992b544514d559aa1"
Expand Down

0 comments on commit b6bdc1b

Please sign in to comment.