8000 fix: handle 429 status code when being throttled (#740) · octokit/plugin-throttling.js@128ecb6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 128ecb6

Browse files
authored
fix: handle 429 status code when being throttled (#740)
1 parent 1d9370f commit 128ecb6

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export function throttling(octokit: Octokit, octokitOptions: OctokitOptions) {
121121
const shouldRetryGraphQL =
122122
pathname.startsWith("/graphql") && error.status !== 401;
123123

124-
if (!(shouldRetryGraphQL || error.status === 403)) {
124+
if (!(shouldRetryGraphQL || error.status === 403 || error.status === 429)) {
125125
return;
126126
}
127127

test/retry.test.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ import { throttling } from "../src/index.ts";
66
import type { AddressInfo } from "node:net";
77
import { createServer } from "node:http";
88

9+
/**
10+
* According to https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28
11+
* both 403 and 429 status codes can be returned when hitting a rate limit.
12+
*/
13+
const restStatusCodes = [403, 429];
14+
915
describe(
1016
"Retry",
1117
function () {
12-
describe("REST", function () {
13-
it("Should retry 'secondary-limit' and succeed", async function () {
18+
describe.each(restStatusCodes)("REST", function (statusCode) {
19+
it(`Should retry 'secondary-limit' and succeed when status ${statusCode}`, async function () {
1420
let eventCount = 0;
1521
const octokit = new TestOctokit({
1622
throttle: {
@@ -34,7 +40,7 @@ describe(
3440
request: {
3541
responses: [
3642
{
37-
status: 403,
43+
status: statusCode,
3844
headers: { "retry-after": "1" },
3945
data: { message: "You have exceeded a secondary rate limit" },
4046
},
@@ -82,12 +88,12 @@ describe(
8288
request: {
8389
responses: [
8490
{
85-
status: 403,
91+
status: statusCode,
8692
headers: { "retry-after": "1" },
8793
data: { message },
8894
},
8995
{
90-
status: 403,
96+
status: statusCode,
9197
headers: { "retry-after": "2" },
9298
data: { message },
9399
},
@@ -131,7 +137,7 @@ describe(
131137
.end(JSON.stringify({ message: "Success!" }));
132138
} else {
133139
res
134-
.writeHead(403, {
140+
.writeHead(statusCode, {
135141
"Content-Type": "application/json",
136142
"retry-after": "1",
137143
})
@@ -177,7 +183,7 @@ describe(
177183
}
178184
});
179185

180-
it("Should retry 'rate-limit' and succeed", async function () {
186+
it(`Should retry 'rate-limit' with status code ${statusCode} and succeed`, async function () {
181187
let eventCount = 0;
182188
const octokit = new TestOctokit({
183189
throttle: {
@@ -199,7 +205,7 @@ describe(
199205
request: {
200206
responses: [
201207
{
202-
status: 403,
208+
status: statusCode,
203209
headers: {
204210
"x-ratelimit-remaining": "0",
205211
"x-ratelimit-reset": "123",

0 commit comments

Comments
 (0)
0