Skip to content

Commit 5467c50

Browse files
committed
Implement throttling
1 parent 6da1f93 commit 5467c50

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

extensions/ql-vscode/src/variant-analysis/gh-api/gh-api-client.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
import { Repository } from "./repository";
1010
import { Progress } from "vscode";
1111
import { CancellationToken } from "vscode-jsonrpc";
12+
import { throttling } from "@octokit/plugin-throttling";
13+
import { Octokit } from "@octokit/rest";
1214

1315
export async function getCodeSearchRepositories(
1416
credentials: Credentials,
@@ -20,18 +22,46 @@ export async function getCodeSearchRepositories(
2022
token: CancellationToken,
2123
): Promise<string[]> {
2224
let nwos: string[] = [];
23-
const octokit = await credentials.getOctokit();
25+
const MyOctokit = Octokit.plugin(throttling);
26+
const auth = await credentials.getAccessToken();
27+
28+
const octokit = new MyOctokit({
29+
auth,
30+
throttle: {
31+
onRateLimit: (
32+
retryAfter: number,
33+
options: any,
34+
octokit: Octokit,
35+
): boolean => {
36+
octokit.log.warn(
37+
`Request quota exhausted for request ${options.method} ${options.url}. Retrying after ${retryAfter} seconds!`,
38+
);
39+
40+
return true;
41+
},
42+
onSecondaryRateLimit: (
43+
_retryAfter: number,
44+
options: any,
45+
octokit: Octokit,
46+
): void => {
47+
octokit.log.warn(
48+
`SecondaryRateLimit detected for request ${options.method} ${options.url}`,
49+
);
50+
},
51+
},
52+
});
53+
2454
for await (const response of octokit.paginate.iterator(
25-
octokit.rest.search.repos,
55+
octokit.rest.search.code,
2656
{
2757
q: query,
2858
per_page: 100,
2959
},
3060
)) {
31-
nwos.push(...response.data.map((item) => item.full_name));
61+
nwos.push(...response.data.map((item) => item.repository.full_name));
3262
// calculate progress bar: 80% of the progress bar is used for the code search
3363
const totalNumberOfRequests = Math.ceil(response.data.total_count / 100);
34-
// Since we have a maximum 10 of requests, we use a fixed increment whenever the totalNumberOfRequests is greater than 10
64+
// Since we have a maximum of 1000 responses of the api, we can use a fixed increment whenever the totalNumberOfRequests would be greater than 10
3565
const increment =
3666
totalNumberOfRequests < 10 ? 80 / totalNumberOfRequests : 8;
3767
progress.report({ increment });

0 commit comments

Comments
 (0)