Skip to content

Commit 39c0080

Browse files
authoredOct 25, 2023
fix(retry): Retry requests after the x-ratelimit-reset time, not exactly at it. (#635)
1 parent b5401b5 commit 39c0080

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed
 

‎src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ export function throttling(octokit: Octokit, octokitOptions: OctokitOptions) {
148148
~~error.response.headers["x-ratelimit-reset"] * 1000,
149149
).getTime();
150150
const retryAfter = Math.max(
151-
Math.ceil((rateLimitReset - Date.now()) / 1000),
151+
// Add one second so we retry _after_ the reset time
152+
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#exceeding-the-rate-limit
153+
Math.ceil((rateLimitReset - Date.now()) / 1000) + 1,
152154
0,
153155
);
154156
const wantRetry = await emitter.trigger(

‎test/events.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ describe("Events", function () {
133133
throttle: {
134134
onRateLimit: (retryAfter, options, octokitFromOptions) => {
135135
expect(octokit).toBe(octokitFromOptions);
136-
expect(retryAfter).toBeLessThan(32);
137-
expect(retryAfter).toBeGreaterThan(28);
136+
expect(retryAfter).toBeLessThan(33);
137+
expect(retryAfter).toBeGreaterThan(29);
138138
expect(options).toMatchObject({
139139
method: "GET",
140140
url: "/route2",

0 commit comments

Comments
 (0)
Please sign in to comment.