Skip to content

feat: add WithRetryAfterReset to wait for the limit and continue - #52

Open
benlangfeld wants to merge 3 commits into
gofri:mainfrom
benlangfeld:retry-after-reset
Open

benlangfeld wants to merge 3 commits into
gofri:mainfrom
benlangfeld:retry-after-reset

Conversation

@benlangfeld

@benlangfeld benlangfeld commented Sep 30, 2026 •

Copy link
Copy Markdown

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.

WithSleepUntilReset does not do what its name suggests to a caller with requests in flight. It is implemented as a LimitDetectedCallback, so it sleeps on the one request that discovers the limit and then fails it anyway, while every request that arrives afterwards takes the resetTime != nil path 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.

WithRetryAfterReset waits 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.GetResetTime ignores the error from strconv.Atoi, so a response with a missing or unparseable x-ratelimit-reset yields 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. TestPrimaryRateLimitRetryAfterResetIsBounded is 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.

WithBypassLimit takes 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. RoundTrip must not modify the request it is given, so each retry is sent on a clone, with the body reproduced through GetBody. 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 returns ErrBodyNotRewindable rather than sending something truncated.

The refused response is drained and closed before waiting, so the connection is not held for the duration.

RoundTrip became a loop rather than recursing, so a limit hit repeatedly cannot grow the stack.

WithSleepUntilReset is 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 — WithBypassLimit still sends immediately when both are set.

🤖 Generated with Claude Code

benlangfeld and others added 2 commits September 30, 2026 19:15
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
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]>
@benlangfeld

Copy link
Copy Markdown
Author

@gofri Could I get a review please?

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.

Wait on primary rate limit

1 participant