Repository navigation
feat: add WithRetryAfterReset to wait for the limit and continue - #52
Open
benlangfeld wants to merge 3 commits into
Open
benlangfeld wants to merge 3 commits into
benlangfeld wants to merge 3 commits into
Conversation
WithSleepUntilReset sleeps on the request that discovers the limit and then fails it anyway, while every request already prevented fails without sleeping at all. A caller with requests in flight sees one nap and the rest error. WithRetryAfterReset waits on both paths and sends the request, so the caller sees them complete. The wait ends with the request's context, and a request whose body cannot be rewound is not retried. It does not occupy the LimitDetectedCallback, so a caller can still be told when a limit is reached. Closes gofri#46
benlangfeld
marked this pull request as ready for review
September 30, 2026 22:23
A response that names no reset time, or one that does not parse, yields a reset of the epoch. Waiting for it returns at once, so the request is sent again immediately, and a limit still in force sends it again after that: 190,000 requests in two seconds against an API that has just said stop. Bound a single RoundTrip to four attempts. That covers the case the option is for - wait out the limit, send the request - while a limit that survives the waits is reported as an error rather than hammered at. WithBypassLimit now also takes precedence over waiting: it asks for no request to be prevented, and a request held until the reset is prevented, just slowly. Co-Authored-By: Claude Opus 5 <[email protected]>
This was referenced Sep 30, 2026
Author
|
@gofri Could I get a review please? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #46.
Note
Depends on #51. On its own this makes a misclassified secondary rate limit worse: the caller would sleep until the primary window resets — up to an hour — rather than failing fast. #51 stops that misclassification; this is only safe once a primary limit means a primary limit.
WithSleepUntilResetdoes not do what its name suggests to a caller with requests in flight. It is implemented as aLimitDetectedCallback, so it sleeps on the one request that discovers the limit and then fails it anyway, while every request that arrives afterwards takes theresetTime != nilpath and fails immediately without sleeping. One goroutine naps; the rest error. It also occupies the callback slot, so a caller cannot both sleep and be told.WithRetryAfterResetwaits on both paths and then sends the request, so a caller sees its requests complete rather than fail.Details worth reviewing
Retrying is bounded to four attempts per
RoundTrip.ParsedResponse.GetResetTimeignores the error fromstrconv.Atoi, so a response with a missing or unparseablex-ratelimit-resetyields a reset of the epoch. Waiting for that returns at once, the request is sent again immediately, and a limit still in force sends it again after that. An earlier revision of this branch did exactly that: 190,000 requests in two seconds, against an API that had just said stop.TestPrimaryRateLimitRetryAfterResetIsBoundedis the regression test. Four covers what the option is for — wait out the limit, send the request — and a limit that survives the waits is reported as an error rather than hammered at.WithBypassLimittakes precedence. It asks for no request to be prevented, and a request held until the reset is prevented, just slowly.The wait ends with the request's context. A cancelled or expired context returns the context's error rather than sleeping out the window — important when the reset can be an hour away.
Requests are not mutated.
RoundTripmust not modify the request it is given, so each retry is sent on a clone, with the body reproduced throughGetBody. Rewinding happens wherever a retry is decided on, not only where it waits, so a retry that finds the limit already lifted still sends a complete body.A request whose body cannot be rewound is not retried. If there is a body and no
GetBody, it returnsErrBodyNotRewindablerather than sending something truncated.The refused response is drained and closed before waiting, so the connection is not held for the duration.
RoundTripbecame a loop rather than recursing, so a limit hit repeatedly cannot grow the stack.WithSleepUntilResetis left alone, with a note pointing at the difference, so nothing existing changes behaviour.Tests
TestPrimaryRateLimitRetryAfterReset— the request that would otherwise fail waits out the limit and returns the upstream response, and the limit-detected callback is still the caller's rather than being spent on sleeping.TestPrimaryRateLimitRetryAfterResetHonoursContext— an hour-long wait ends promptly when the request's context does.TestPrimaryRateLimitRetryAfterResetIsBounded— a limit that never lifts and names no reset stops after four requests instead of spinning.TestPrimaryRateLimitBypassIsNotDelayedByRetryAfterReset—WithBypassLimitstill sends immediately when both are set.🤖 Generated with Claude Code