Skip to content

Commit

Permalink
fix: handle 429 status code when being throttled (#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
tenjaa authored Oct 4, 2024
1 parent 1d9370f commit 128ecb6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export function throttling(octokit: Octokit, octokitOptions: OctokitOptions) {
const shouldRetryGraphQL =
pathname.startsWith("/graphql") && error.status !== 401;

if (!(shouldRetryGraphQL || error.status === 403)) {
if (!(shouldRetryGraphQL || error.status === 403 || error.status === 429)) {
return;
}

Expand Down
22 changes: 14 additions & 8 deletions test/retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import { throttling } from "../src/index.ts";
import type { AddressInfo } from "node:net";
import { createServer } from "node:http";

/**
* According to https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28
* both 403 and 429 status codes can be returned when hitting a rate limit.
*/
const restStatusCodes = [403, 429];

describe(
"Retry",
function () {
describe("REST", function () {
it("Should retry 'secondary-limit' and succeed", async function () {
describe.each(restStatusCodes)("REST", function (statusCode) {
it(`Should retry 'secondary-limit' and succeed when status ${statusCode}`, async function () {
let eventCount = 0;
const octokit = new TestOctokit({
throttle: {
Expand All @@ -34,7 +40,7 @@ describe(
request: {
responses: [
{
status: 403,
status: statusCode,
headers: { "retry-after": "1" },
data: { message: "You have exceeded a secondary rate limit" },
},
Expand Down Expand Up @@ -82,12 +88,12 @@ describe(
request: {
responses: [
{
status: 403,
status: statusCode,
headers: { "retry-after": "1" },
data: { message },
},
{
status: 403,
status: statusCode,
headers: { "retry-after": "2" },
data: { message },
},
Expand Down Expand Up @@ -131,7 +137,7 @@ describe(
.end(JSON.stringify({ message: "Success!" }));
} else {
res
.writeHead(403, {
.writeHead(statusCode, {
"Content-Type": "application/json",
"retry-after": "1",
})
Expand Down Expand Up @@ -177,7 +183,7 @@ describe(
}
});

it("Should retry 'rate-limit' and succeed", async function () {
it(`Should retry 'rate-limit' with status code ${statusCode} and succeed`, async function () {
let eventCount = 0;
const octokit = new TestOctokit({
throttle: {
Expand All @@ -199,7 +205,7 @@ describe(
request: {
responses: [
{
status: 403,
status: statusCode,
headers: {
"x-ratelimit-remaining": "0",
"x-ratelimit-reset": "123",
Expand Down

0 comments on commit 128ecb6

Please sign in to comment.