Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(retry): Retry requests after the x-ratelimit-reset time, not exactly at it. #635

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading