Skip to content

Commit

Permalink
fix(retry): Retry requests after the x-ratelimit-reset time, not exac…
Browse files Browse the repository at this point in the history
…tly at it. (#635)
  • Loading branch information
jyasskin committed Oct 25, 2023
1 parent b5401b5 commit 39c0080
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ export function throttling(octokit: Octokit, octokitOptions: OctokitOptions) {
~~error.response.headers["x-ratelimit-reset"] * 1000,
).getTime();
const retryAfter = Math.max(
Math.ceil((rateLimitReset - Date.now()) / 1000),
// Add one second so we retry _after_ the reset time
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#exceeding-the-rate-limit
Math.ceil((rateLimitReset - Date.now()) / 1000) + 1,
0,
);
const wantRetry = await emitter.trigger(
Expand Down
4 changes: 2 additions & 2 deletions test/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ describe("Events", function () {
throttle: {
onRateLimit: (retryAfter, options, octokitFromOptions) => {
expect(octokit).toBe(octokitFromOptions);
expect(retryAfter).toBeLessThan(32);
expect(retryAfter).toBeGreaterThan(28);
expect(retryAfter).toBeLessThan(33);
expect(retryAfter).toBeGreaterThan(29);
expect(options).toMatchObject({
method: "GET",
url: "/route2",
Expand Down

0 comments on commit 39c0080

Please sign in to comment.