Skip to content

Commit

Permalink
feat: add request throttling to octokit
Browse files Browse the repository at this point in the history
  • Loading branch information
ajhenry committed Jan 29, 2024
1 parent 358113b commit 92b926c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions ts-backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ts-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@octokit/graphql-schema": "^14.50.0",
"@octokit/plugin-paginate-graphql": "^4.0.0",
"@octokit/plugin-retry": "^6.0.1",
"@octokit/plugin-throttling": "^8.1.3",
"@octokit/rest": "^20.0.2",
"dotenv": "^16.4.1",
"fs-extra": "^11.2.0",
Expand Down
22 changes: 21 additions & 1 deletion ts-backend/src/lib/octokit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { paginateGraphql } from "@octokit/plugin-paginate-graphql";
import { retry } from "@octokit/plugin-retry";
import { throttling } from "@octokit/plugin-throttling";
import { Octokit } from "@octokit/rest";

/**
Expand All @@ -9,9 +10,28 @@ import { Octokit } from "@octokit/rest";
*/
export const personalOctokit = (token: string) => {
// Not sure if plugin order matters
const ModifiedOctokit = Octokit.plugin(paginateGraphql, retry);
const ModifiedOctokit = Octokit.plugin(paginateGraphql, retry, throttling);
return new ModifiedOctokit({
auth: token,
throttle: {
onRateLimit: (retryAfter, options, octokit, retryCount) => {
octokit.log.warn(
`Request quota exhausted for request ${options.method} ${options.url} - retrying in ${retryAfter} seconds`
);

if (retryCount < 1) {
// only retries once
octokit.log.info(`Retry attempt ${retryCount + 1}, retrying...`);
return true;
}
},
onSecondaryRateLimit: (retryAfter, options, octokit) => {
// does not retry, only logs a warning
octokit.log.warn(
`SecondaryRateLimit detected for request ${options.method} ${options.url}`
);
},
},
});
};

Expand Down

0 comments on commit 92b926c

Please sign in to comment.