Skip to content

fix: wait one minute for a secondary rate limit without retry-after - #53

Open
lnutimura wants to merge 1 commit into
gofri:mainfrom
lnutimura:fix/secondary-limit-default-wait
Open

lnutimura wants to merge 1 commit into
gofri:mainfrom
lnutimura:fix/secondary-limit-default-wait

Conversation

@lnutimura

Copy link
Copy Markdown

When a secondary rate limit response has no retry-after header, the limiter sleeps until x-ratelimit-reset. That header is the reset of the primary rate limit window, which can be up to an hour away, so every request through the limiter blocks for up to an hour.

GitHub's docs (Exceeding the rate limit) use x-ratelimit-reset for a secondary limit only when x-ratelimit-remaining is 0:

If the x-ratelimit-remaining header is 0, you should not retry your request until after the time, in UTC epoch seconds, specified by the x-ratelimit-reset header. Otherwise, wait for at least one minute before retrying.

This limiter never handles that case: a response with x-ratelimit-remaining: 0 is treated as a primary limit and passed through (see #50). So the x-ratelimit-reset fallback only runs when quota remains, which is exactly when the docs say to wait a minute instead. This is the long backoff reported in #38, and the first part of #29.

Seen in production

Atlantis wraps all its GitHub calls in NewSecondaryLimiter (v2.0.2). After a burst of about 80 PR comments in one minute, every GitHub request from the server stopped for 32 minutes. When the wait ended, the queued requests went out together and the server stopped again, this time for a full hour. The two wake-ups were exactly an hour apart, matching the primary window's reset. For about 90 minutes the server couldn't run comment commands, post comments or set commit statuses.

The change

  • Without retry-after, sleep one minute instead of until x-ratelimit-reset.
  • parseXRateLimitReset had no other caller and is removed. HeaderXRateLimitReset stays exported.
  • TestXRateLimit asserted the old behavior. TestXRateLimitResetIgnored replaces it: it injects a reset 30 minutes away and checks that the computed sleep is one minute. A zero WithSingleSleepLimit reads the sleep without waiting it out.

Not included: the exponential backoff the docs suggest if the limit persists. Callers can still bound waits with WithSingleSleepLimit and WithTotalSleepLimit.

With #51

#51 lets secondary limits with x-ratelimit-remaining: 0 reach this function, and notes that without retry-after they are passed through unwaited. With this change they would wait one minute. Both PRs edit parseXRateLimitReset; whichever lands second resolves to removing it.

Testing

go test ./... and golangci-lint v1.64.6 on Go 1.23.1, the versions CI uses.

x-ratelimit-reset is the primary rate limit's reset, which can be up to an
hour away. GitHub's docs only use it for a secondary limit when
x-ratelimit-remaining is 0; otherwise they ask to wait at least one minute.
Sleeping until x-ratelimit-reset blocked every request through the limiter
for up to an hour while the primary quota was mostly unused.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant