Repository navigation
fix: do not treat a secondary rate limit as a primary one - #51
benlangfeld wants to merge 3 commits into
Conversation
GitHub sends `x-ratelimit-remaining: 0` on some secondary rate limits, so the status code and that header together do not tell the two apart. Both limiters decided on the header alone: the secondary detector deferred such a response to the primary path, and the primary limiter armed the whole resource category until the hourly reset. The effect is that a burst throttle, which asks to be waited out in seconds, blocks every request in the category for up to an hour - and the primary quota it claims to have exhausted is untouched. The body says which limit it is, and now decides. A response that reports no requests remaining is still kept away from the x-ratelimit-reset fallback, since that names the primary window rather than the backoff being asked for. Fixes gofri#50
A failed read closed the body and returned, so whoever handled the response next - the secondary limiter, or the caller - found it closed. Put the bytes that were read back before returning, so the response is always left in the state it was found in. Co-Authored-By: Claude Opus 5 <[email protected]>
|
@gofri Could I get a review please? |
stevehipwell
left a comment
There was a problem hiding this comment.
The code change looks good but there doesn't seem to be a test for the change you made to parseXRateLimitReset?
Also would it make sense to use synctest to speed up the tests where the code is waiting?
|
@gofri we've recently started using your package in the GitHub Terraform Provider, so thank you for your work. This issue is impacting us and we'd like to get a fix in before we cut our next release. Could you possibly take a look? |
|
@gofri Is this project dead? |
|
Hey @benlangfeld! Just to let you know, I opened #53, which also touches While this change fixes secondary limits that come with They only conflict in text: #51 adds a guard to |
Fixes #50.
GitHub sends
x-ratelimit-remaining: 0on some secondary rate limits, so the status code and that header together do not tell the two apart. Both limiters decided on the header alone, from opposite directions:github_secondary_ratelimit.isSecondaryRateLimitreturned early with// a primary rate limitwheneverremaining == 0, before looking at the body.github_primary_ratelimit.ParsedResponse.limitReachedreturned true for any 403/429 withremaining == 0.Since the secondary limiter is stacked outside the primary one, the primary sees the response first, arms the category, and returns an error — so the secondary limiter never gets the chance the deferral assumed it would.
The result is that a burst throttle asking to be waited out in seconds blocks every request in the resource category until the hourly reset, while the primary quota it claims to have exhausted is untouched.
The change
The body says which limit it is, and now decides.
SecondaryRateLimitBody.IsSecondaryRateLimitalready encodes that test; the primary limiter now consults it before arming, and the secondary detector no longer defers on the header.Reading the body to decide means the primary limiter has to put it back, which it does on every path, including a failed read — the response is always left in the state it was found in.
One guard is kept rather than removed: a response reporting no requests remaining is still kept away from the
x-ratelimit-resetfallback when working out how long to sleep, because that header names the primary window — an hour, rather than the backoff actually being asked for. Such a response is waited out onretry-after.Known gap
A secondary limit carrying
remaining: 0and noretry-afternow has no sleep duration to work from, so it is passed through to the caller as a plain 403 rather than waited out. That is the existing behaviour for any secondary limit with no usable header, and theXXXinparseSecondaryLimitTimealready proposes the fix — GitHub documents a 60-second minimum with exponential backoff. It seemed better to leave that decision where you left it than to smuggle it in here. Before this change the same response armed the primary category for an hour, so passing it through is not a regression, but it is not yet a proper handling either.Test
TestSecondaryRateLimitWithZeroRemainingIsNotPrimarybuilds the response the issue describes — 403, a secondary body,retry-after: 1,x-ratelimit-remaining: 0, and a reset an hour out — and asserts the request is retried once after a short wait, that it succeeds, and that the primary limit-detected callback is never called.Before this change the request fails immediately and the category is armed for the hour.
The existing suite passes unchanged, including
TestPrimaryRateLimitIgnored, which covers the case the removed early return was there for.Reproduction in the wild
A second, independent one, from a Terraform run against an organisation of ~200 repositories:
A 12,500/hour primary limit cannot be exhausted by 210 requests. The run had not come close to its primary budget; it hit a burst limit, had it filed as a primary one, and lost the rest of the window. The 288-at-once shape is the category being armed and every in-flight request being refused without being sent.
🤖 Generated with Claude Code