Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit 2ed0b29

Browse files
authored
fix: add warnings to log for GitHub rate limits (#3988)
Adding explicit logging for GitHub reate limits. No retries implemented. Only for abuse retrying seems an option. In other cases a retry will take to long given lambda timeouts taken into accont.
1 parent c43d1bb commit 2ed0b29

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

Diff for: lambdas/functions/control-plane/jest.config.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const config: Config = {
66
...defaultConfig,
77
coverageThreshold: {
88
global: {
9-
statements: 97.99,
10-
branches: 96.04,
11-
functions: 97.53,
12-
lines: 98.3,
9+
statements: 97.79,
10+
branches: 96.13,
11+
functions: 95.4,
12+
lines: 98.06,
1313
},
1414
},
1515
};

Diff for: lambdas/functions/control-plane/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@aws-sdk/types": "^3.433.0",
4343
"@middy/core": "^4.7.0",
4444
"@octokit/auth-app": "6.0.3",
45+
"@octokit/plugin-throttling": "8.1.3",
4546
"@octokit/rest": "20.0.2",
4647
"@octokit/types": "^13.5.0",
4748
"@terraform-aws-github-runner/aws-powertools-util": "*",

Diff for: lambdas/functions/control-plane/src/gh-auth/gh-auth.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import {
1010
import { OctokitOptions } from '@octokit/core/dist-types/types';
1111
import { request } from '@octokit/request';
1212
import { Octokit } from '@octokit/rest';
13+
import { throttling } from '@octokit/plugin-throttling';
1314
import { createChildLogger } from '@terraform-aws-github-runner/aws-powertools-util';
1415
import { getParameter } from '@terraform-aws-github-runner/aws-ssm-util';
1516

1617
import { axiosFetch } from '../axios/fetch-override';
1718

1819
const logger = createChildLogger('gh-auth');
20+
1921
export async function createOctoClient(token: string, ghesApiUrl = ''): Promise<Octokit> {
22+
const CustomOctokit = Octokit.plugin(throttling);
2023
const ocktokitOptions: OctokitOptions = {
2124
auth: token,
2225
request: { fetch: axiosFetch },
@@ -25,7 +28,20 @@ export async function createOctoClient(token: string, ghesApiUrl = ''): Promise<
2528
ocktokitOptions.baseUrl = ghesApiUrl;
2629
ocktokitOptions.previews = ['antiope'];
2730
}
28-
return new Octokit(ocktokitOptions);
31+
32+
return new CustomOctokit({
33+
...ocktokitOptions,
34+
throttle: {
35+
onRateLimit: (retryAfter, options) => {
36+
logger.warn(
37+
`GitHub rate limit: Request quota exhausted for request ${options.method} ${options.url}. Requested `,
38+
);
39+
},
40+
onSecondaryRateLimit: (retryAfter, options) => {
41+
logger.warn(`GitHub rate limit: SecondaryRateLimit detected for request ${options.method} ${options.url}`);
42+
},
43+
},
44+
});
2945
}
3046

3147
export async function createGithubAppAuth(

Diff for: lambdas/yarn.lock

+14-1
Original file line numberDiff line numberDiff line change
@@ -2736,6 +2736,14 @@
27362736
dependencies:
27372737
"@octokit/types" "^12.0.0"
27382738

2739+
"@octokit/[email protected]":
2740+
version "8.1.3"
2741+
resolved "https://registry.yarnpkg.com/@octokit/plugin-throttling/-/plugin-throttling-8.1.3.tgz#7fb0e001c0cb9383c6be07740b8ec326ed990f6b"
2742+
integrity sha512-pfyqaqpc0EXh5Cn4HX9lWYsZ4gGbjnSmUILeu4u2gnuM50K/wIk9s1Pxt3lVeVwekmITgN/nJdoh43Ka+vye8A==
2743+
dependencies:
2744+
"@octokit/types" "^12.2.0"
2745+
bottleneck "^2.15.3"
2746+
27392747
"@octokit/request-error@^5.0.0":
27402748
version "5.1.0"
27412749
resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-5.1.0.tgz#ee4138538d08c81a60be3f320cd71063064a3b30"
@@ -2766,7 +2774,7 @@
27662774
"@octokit/plugin-request-log" "^4.0.0"
27672775
"@octokit/plugin-rest-endpoint-methods" "^10.0.0"
27682776

2769-
"@octokit/types@^11.0.0", "@octokit/types@^11.1.0", "@octokit/types@^12.0.0", "@octokit/types@^13.0.0", "@octokit/types@^13.1.0", "@octokit/types@^13.5.0":
2777+
"@octokit/types@^11.0.0", "@octokit/types@^11.1.0", "@octokit/types@^12.0.0", "@octokit/types@^12.2.0", "@octokit/types@^13.0.0", "@octokit/types@^13.1.0", "@octokit/types@^13.5.0":
27702778
version "13.5.0"
27712779
resolved "https://registry.yarnpkg.com/@octokit/types/-/types-13.5.0.tgz#4796e56b7b267ebc7c921dcec262b3d5bfb18883"
27722780
integrity sha512-HdqWTf5Z3qwDVlzCrP8UJquMwunpDiMPt5er+QjGzL4hqr/vBVY/MauQgS1xWxCDT1oMx1EULyqxncdCY/NVSQ==
@@ -4323,6 +4331,11 @@ [email protected], body-parser@^1.20.2:
43234331
type-is "~1.6.18"
43244332
unpipe "1.0.0"
43254333

4334+
bottleneck@^2.15.3:
4335+
version "2.19.5"
4336+
resolved "https://registry.yarnpkg.com/bottleneck/-/bottleneck-2.19.5.tgz#5df0b90f59fd47656ebe63c78a98419205cadd91"
4337+
integrity sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==
4338+
43264339
bowser@^2.11.0:
43274340
version "2.11.0"
43284341
resolved "https://registry.yarnpkg.com/bowser/-/bowser-2.11.0.tgz#5ca3c35757a7aa5771500c70a73a9f91ef420a8f"

0 commit comments

Comments
 (0)