Skip to content

feat(CLI): retry requests on 429 concurrency and rate limit responses #STRINGS-4937 - #1379

Merged
Sven Dunemann (forelabs) merged 6 commits into
mainfrom
strings-4937-cli-retry-429
Nov 23, 2026
Merged

Sven Dunemann (forelabs) merged 6 commits into
mainfrom
strings-4937-cli-retry-429

Conversation

@forelabs

@forelabs Sven Dunemann (forelabs) commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Ticket: https://phrase.atlassian.net/browse/STRINGS-4937

What

phrase push / phrase pull now retry requests the API rejects with HTTP 429, via a RoundTripper wrapper set on the API client in newClient() (clients/cli/cmd/internal/retry.go).

  • X-Rate-Limit-Reason: global-concurrency (or a 429 without a usable X-Rate-Limit-Reset): exponential backoff from 250 ms up to 5 s, plus up to 50% jitter so parallel workers don't retry in lockstep.
  • global-rate (or any other 429 with a valid X-Rate-Limit-Reset): print a warning, wait until the reset (+1 s), then retry.
  • At most 20 retries and 10 minutes of total waiting per request, then the 429 is returned as before. Request bodies are replayed via GetBody, so multipart uploads are sent intact. The backoff wait stops when the request context is cancelled.

Unified retry logic in pull

The existing rate-limit handling in pull.go is replaced by the transport, and retryTransient (body/transport errors) now lives in retry.go with the same sleep hook.

  • Removed: downloadWithRateGate (the RWMutex gate), waitForRateLimit and the Rate.Remaining == 0 retry blocks.
  • Behaviour changes:
    • Parallel workers no longer pause together; each waits for the reset itself, so each prints its own rate limit warning.
    • The old code retried once after any API error with missing/zero rate headers, which could include non-429 errors; it no longer does.
    • A retried download keeps its cache headers, so a 304 still counts as not modified.
    • The wait after a rate limit is reset + 1 s (was + 5 s).

Verification

  • Unit tests (retry_test.go, pull_retry_test.go) cover both reasons, reset in the past, giving up, body replay, cancellation during backoff, and transport-error retries.
  • Not verified: the X-Rate-Limit-Reset format is assumed to be a unix timestamp (inferred, not confirmed against the live API). X-Rate-Limit-Limit/Remaining are not read. Not run against the real API.
  • go test ./cmd/... reports a build failure in the cmd package's generated api_*.go files, which don't match phrase-go v4.38.0. This comes from files not touched by this PR.

🤖 Generated with Claude Code

…#STRINGS-4937

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

API changelog (oasdiff)

Doc-only edits (descriptions, examples) do not appear here.

No changes detected

@forelabs Sven Dunemann (forelabs) changed the title fix(CLI): retry requests on 429 concurrency and rate limit responses #STRINGS-4937 fea(CLI): retry requests on 429 concurrency and rate limit responses #STRINGS-4937 Nov 20, 2026
@forelabs
Sven Dunemann (forelabs) marked this pull request as ready for review November 20, 2026 12:33
@forelabs
Sven Dunemann (forelabs) requested a balanced review from Copilot November 20, 2026 12:33
@forelabs Sven Dunemann (forelabs) changed the title fea(CLI): retry requests on 429 concurrency and rate limit responses #STRINGS-4937 feat(CLI): retry requests on 429 concurrency and rate limit responses #STRINGS-4937 Nov 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The retry backoff sleep ignores request cancellation and deadlines, potentially blocking callers for minutes before retrying.

1 open issue
What changed in this PR

This PR adds HTTP 429 retries to the CLI’s shared API client to help phrase push and phrase pull recover from concurrency and rate limits.

Changes:

  • Adds a transport wrapper with up to 20 retries, reason-based delays, and request-body replay.
  • Enables the wrapper in newClient().
  • Adds tests for retry delays, past reset times, retry exhaustion, and body replay.
File Description
clients/​cli/​cmd/​internal/​retry_transport.go Implements retry handling and backoff calculation.
clients/​cli/​cmd/​internal/​retry_transport_test.go Tests retry behavior, delays, and body replay.
clients/​cli/​cmd/​internal/​pull.go Configures the shared API client to use the retry transport.

Review effort: Balanced

…NGS-4937

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues requiring resolution for the HTTP 429 retry changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@jablan

jablan commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Thanks for unifying this, and having the transport cover push, polling and the other API calls is a good improvement. I read the diff and checked the generated Go client template. Request replay is safe, because bodies are *bytes.Buffer and http.NewRequest sets GetBody for them, so multipart uploads are replayed intact.

A few things I'd suggest changing or discussing:

  1. Concurrency backoff is short and fixed. 250 ms × 20 retries gives up after about 5 s. If another request holds the slot, for example a long export, the CLI fails with a 429 where it could have waited. Every retry is also an extra request that may count toward the rate limit. Exponential backoff with jitter (say 250 ms growing to about 5 s, with a larger overall budget) would wait longer while sending fewer requests.

  2. No overall cap on global-rate waits. Each wait can last until the window resets, and up to 20 retries are allowed. In sync pull and push, req.Context() has no deadline, so the worst case is a very long hang. Consider a total-wait cap, or at least a smaller retry count for global-rate, and return the 429 once it's exceeded.

  3. Duplicate warnings from parallel workers. With the gate gone, each of the 4 workers prints its own "Rate limit exceeded" warning. They also all wake at the same moment after reset. That is probably acceptable given the 4-request cap, but a one-line mention or a small de-dup would be nicer.

  4. retryTransient ignores cancellation. It sleeps via retrySleep(context.Background(), …) and drops the returned error, so a cancelled context isn't respected between attempts. It would be more consistent to pass the caller's context and stop on error.

  5. Optional: 429s without a reason header (for example from a proxy or load balancer in front of the API) currently fall into the concurrency branch. A fallback that uses X-Rate-Limit-Reset when it's present, and backoff otherwise, would be more robust.

  6. Tests. Cases for a mix of global-rate and global-concurrency across retries, and for retryTransient with a cancelled context, would round out the coverage.

On the description's open items: the X-Rate-Limit-Reset format and the reason header could be confirmed with a curl -i against a rate-limited request before merge, so the "inferred" note can come out.

@jablan

jablan commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

another suggestion: should be easy to add a simple spec in main/clients/cli/spec/locales_download_spec.rb too (just that it retries).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@forelabs

Copy link
Copy Markdown
Member Author

Thanks for the thorough review, jablan. Pushed 434b01c8, 30eafe3d and c2f427a8.

  1. Backoff: the concurrency delay is now exponential, 250 ms growing to 5 s, plus up to 50% jitter.
  2. Cap: each request stops after 20 retries or 10 minutes of total waiting and returns the 429. A reset beyond that budget returns the 429 immediately.
  3. Duplicate warnings: left as is, and the PR description now mentions it. Each of the 4 workers prints its own warning and retries on its own after the reset.
  4. retryTransient cancellation: not changed. There is no caller context to pass in: Auth is never cancelled, and the parallel pull's cancellable context doesn't carry the API key. Threading one through downloadLocale is a bigger change that I'd do separately if you want it.
  5. 429 without a reason header: it now uses X-Rate-Limit-Reset when it is valid and falls back to backoff otherwise.
  6. Tests: added cases for mixed reasons, a missing reason or reset header, the retry limit and the wait budget.

Spec: I added it to spec/pull_spec.rb instead of locales_download_spec.rb. The generated phrase locales download command builds its own plain client, so it doesn't go through the retry transport. Only push and pull use newClient(). The new specs cover a concurrency 429 and a rate-limit 429 on a pull download.

Open: I haven't checked the X-Rate-Limit-Reset format against the live API, so the "inferred" note stays in the PR description.

🤖 Addressed by Claude Code

@forelabs
Sven Dunemann (forelabs) merged commit e26155b into main Nov 23, 2026
13 checks passed
@forelabs
Sven Dunemann (forelabs) deleted the strings-4937-cli-retry-429 branch November 23, 2026 06:57
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.

3 participants