Skip to content

Add --retry backoff (Retry-After/rate-limit reset) and surface X-Request-Id on errors - #116

Merged
tcaldwell-x merged 3 commits into
mainfrom
tcaldwell/retry-after-and-request-id
Dec 2, 2026
Merged

tcaldwell-x merged 3 commits into
mainfrom
tcaldwell/retry-after-and-request-id

Conversation

@tcaldwell-x

Copy link
Copy Markdown
Contributor

Summary

  • Add --retry N flag (cli/root.go, api/client.go):
    • Defaults to 0 (existing behavior unchanged unless opted in).
    • Retries HTTP 429 Too Many Requests and 503 Service Unavailable up to N times, rebuilding the request per attempt (preserving body and re-signing OAuth 1.0a nonces).
    • Determines wait time in order: RFC 9110 Retry-After (delta-seconds or HTTP-date) → X-Rate-Limit-Reset / X-User-Limit-24hour-Reset / X-App-Limit-24hour-Reset on 429 → exponential backoff (1s, 2s, 4s, …, capped at 60s).
    • Logs retry countdowns to stderr (e.g., Retrying in 7s (attempt 1/2, status 429, request-id: ...)).
    • Does not retry non-retryable client errors (400, 401, 403, 404).
  • Surface X-Request-Id on errors (errors/errors.go, api/client.go, api/execute.go):
    • Captures X-Request-Id (falling back to X-Transaction-Id) on API and HTTP errors.
    • Appends (request-id: <id>) to the stderr error message while keeping pretty-printed JSON error output unchanged.

Test Plan

  • Ran go vet ./... (clean).
  • Ran go test -v ./api/... ./errors/... ./cli/... including new unit tests covering Retry-After delta-seconds, X-Rate-Limit-Reset fallback, 503 exponential backoff, non-retryable 400, and X-Request-Id error formatting (all passing).

santiagomed
santiagomed previously approved these changes Dec 2, 2026

@kyletruong kyletruong 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.

1 issue open at commit 0122f28aedcd

To clear an issue, push a fix, or reply with the lines of code or the test that show the finding is wrong, or with why this pull request leaves it as it is and where it is handled instead, in the issue's thread. The review reads a reply at the next push.

1. --retry is silently ignored on the streaming and multipart paths

api/execute.go:53-55

The flag is accepted without warning, but `StreamRequest` and `SendMultipartRequest` never read `MaxRetries`. The request fails on the first 429/503 even though the user asked for retries. Connecting to a stream is where 429 is most common.
Trigger: `xurl --retry 3 -s <endpoint>`, a streaming endpoint detected by `IsStreamingEndpoint`, or `xurl --retry 3 -F file <media append endpoint>`, when the server returns 429/503.
Path taken today: cli/root.go:100: a user running `xurl --retry 3 /2/tweets/search/stream` (or with `-s`, or `-F file` on a `/2/media/upload/.../append` endpoint) goes through `HandleRequest` to `StreamRequest` or `SendMultipartRequest`, which make one attempt and ignore `MaxRetries`.
Harm, the change's goal fails: A user who passes `--retry N` on a streaming or media-append request gets no retry and no warning: the command exits with an error on the first 429 or 503, which is the behavior the flag was added to prevent.

Finding F116.1.4.


Reviewed version 1 of xdevplatform/xurl#116, commit 0122f28aedcdf50ceca3eac0a79a7c6bbe0f3ac7, with method 3d9dde55b2f4 in run 3d6effe4-70b7-4dda-893f-be56d7c9c767. Issues to fix: 1.

Comment thread api/execute.go Resolved

@kyletruong kyletruong 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.

2 issues open at commit 73f3ce225242

The re-check cleared 1 issue from an earlier version: 1 fixed.

To clear an issue, push a fix, or reply with the lines of code or the test that show the finding is wrong, or with why this pull request leaves it as it is and where it is handled instead, in the issue's thread. The review reads a reply at the next push.

1. 429 retry always waits on X-Rate-Limit-Reset, so a 24-hour-limit 429 waits for the wrong window and burns every retry

api/client.go:282-291

The first header that parses wins, and X-Rate-Limit-Reset is first, so the client sleeps until the 15-minute window resets (up to 15 minutes per attempt, or 1 second if that reset is already past). It then retries, gets the same 429, and repeats until the retries are used up. The user waits up to N × 15 minutes for a request that cannot succeed, and the 24-hour reset headers are effectively never consulted.
Trigger: Run `xurl --retry N` against an endpoint whose 429 comes from the 24-hour user or app limit (for example POST /2/tweets). The response carries X-Rate-Limit-Reset for the 15-minute window as well as X-User-Limit-24hour-Reset / X-App-Limit-24hour-Reset.
Path taken today: cli/root.go:76: a user who runs `xurl --retry N -X POST /2/tweets ...` after exhausting the 24-hour post limit reaches SendRequest, then doWithRetry, then retryDelay at api/client.go:282, which picks the 15-minute window's reset.
Harm, the change's goal fails: A user who passes --retry on a request blocked by the 24-hour user or app limit waits up to 15 minutes per attempt, for N attempts, on retries that cannot succeed, and the 24-hour reset headers the description says are honored are never consulted.

Finding F116.2.1.

2. Wait until reset is rounded to the nearest second, so the retry can fire before the reset time

api/client.go:293-297

The wait of 14.4 seconds is rounded down to 14 seconds, so the retry is sent 0.4 seconds before the window resets and is rejected with 429 again. With `--retry 1` the single retry is wasted and the command fails. With more retries, each early attempt uses one up.
Trigger: With `--retry 1`, a 429 arrives with X-Rate-Limit-Reset (or an HTTP-date Retry-After) and the local clock's sub-second part is below 0.5 seconds past the reset's whole second boundary, for example reset = T+15s and now = T+0.6s. This happens for about half of all requests.
Path taken today: cli/root.go:76: a user who runs `xurl --retry 1 <url>` and gets a 429 carrying `X-Rate-Limit-Reset` (or an HTTP-date `Retry-After`) reaches `retryDelay` through `SendRequest` and `doWithRetry`, and sleeps up to 0.5 s short of the reset in about half of such responses.
Harm, the change's goal fails: A user running `xurl --retry 1` against a rate-limited endpoint has the single retry sent just before the rate-limit window resets, so it gets 429 again and the command exits with an error after the full wait, even though the PR says the retry honors the rate-limit reset.

Finding F116.2.3.


Reviewed version 2 of xdevplatform/xurl#116, commit 73f3ce22524257d8b1d474e7755d384bda892164, with method 3d9dde55b2f4 in run 96efc0bb-2d60-4348-a73f-6cc42c5eb824. Issues to fix: 2.

Comment thread api/client.go Resolved
Comment thread api/client.go Resolved

@kyletruong kyletruong 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.

✅ Approved at commit 70232d114035

The re-check cleared 2 issues from earlier versions: 2 fixed.


Reviewed version 3 of xdevplatform/xurl#116, commit 70232d1140357b6ba060d146dd078e471fc7a5ca, with method 3d9dde55b2f4 in run b0d3ca97-9777-452a-8c05-cbf3b3d472cd. Issues to fix: 0.

@kyletruong kyletruong 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.

This approval follows the review of version 3 of xdevplatform/xurl#116, commit 70232d1140357b6ba060d146dd078e471fc7a5ca, with method 3d9dde55b2f4 in run b0d3ca97-9777-452a-8c05-cbf3b3d472cd, which found nothing to fix.

@tcaldwell-x
tcaldwell-x merged commit dde1388 into main Dec 2, 2026
3 checks passed
@tcaldwell-x
tcaldwell-x deleted the tcaldwell/retry-after-and-request-id branch December 2, 2026 01:52
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