Repository navigation
CLI: one-shot 'relayfile read' does not retry 429 workspace_busy (Retry-After is parsed but unused) #527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
agent-relay-code
wants to merge
4
commits into
main
from
relayflow/relayfile-garden-relayfile-2-9119454f-d1cf0e5e
Closed
CLI: one-shot 'relayfile read' does not retry 429 workspace_busy (Retry-After is parsed but unused) #527
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "time" | ||
| ) | ||
|
|
||
| const ( | ||
| oneShotGETMaxAttempts = 3 | ||
| oneShotGETMinDelay = time.Second | ||
| oneShotGETMaxDelay = 60 * time.Second | ||
| oneShotGETJitter = 0.2 | ||
| ) | ||
|
|
||
| type oneShotGETRetryPolicy struct { | ||
| enabled bool | ||
| stderr io.Writer | ||
| opts politeOpts | ||
| jitter func(time.Duration, float64) time.Duration | ||
| } | ||
|
|
||
| func defaultOneShotGETRetryPolicy(enabled bool, stderr io.Writer) oneShotGETRetryPolicy { | ||
| return oneShotGETRetryPolicy{ | ||
| enabled: enabled, | ||
| stderr: stderr, | ||
| opts: politeOpts{ | ||
| minInterval: oneShotGETMinDelay, | ||
| maxInterval: oneShotGETMaxDelay, | ||
| jitterFraction: oneShotGETJitter, | ||
| hardMaxPerSecond: 1, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (p oneShotGETRetryPolicy) run(ctx context.Context, get func(context.Context) error) error { | ||
| if !p.enabled { | ||
| return get(ctx) | ||
| } | ||
|
|
||
| opts := p.opts.withDefaults() | ||
| jitter := p.jitter | ||
| if jitter == nil { | ||
| jitter = applyJitter | ||
| } | ||
| for attempt := 1; ; attempt++ { | ||
| if err := ctx.Err(); err != nil { | ||
| return err | ||
| } | ||
| err := get(ctx) | ||
| if err == nil || attempt >= oneShotGETMaxAttempts || !isRetryableOneShotGETError(err) { | ||
| return err | ||
| } | ||
|
|
||
| var apiErr *apiError | ||
| _ = errors.As(err, &apiErr) | ||
| baseDelay := backoffFor(attempt, opts.minInterval, opts.maxInterval) | ||
| if apiErr.RetryAfter > 0 { | ||
| baseDelay = clampDuration(apiErr.RetryAfter, opts.minInterval, opts.maxInterval) | ||
| } | ||
| delay := jitter(baseDelay, opts.jitterFraction) | ||
| // Retry-After is a lower bound. Symmetric jitter must never make us | ||
| // retry before the server's advertised delay has elapsed. | ||
| if apiErr.RetryAfter > 0 && delay < baseDelay { | ||
| delay = baseDelay | ||
| } | ||
| delay = clampDuration(delay, time.Millisecond, opts.maxInterval) | ||
| if p.stderr != nil { | ||
| fmt.Fprintf(p.stderr, "%s, retrying in %s\n", oneShotRetryLabel(err), delay.Round(time.Millisecond)) | ||
| } | ||
| if err := opts.sleep(ctx, delay); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func isRetryableOneShotGETError(err error) bool { | ||
| var apiErr *apiError | ||
| return errors.As(err, &apiErr) && (apiErr.StatusCode == http.StatusTooManyRequests || apiErr.StatusCode == http.StatusServiceUnavailable) | ||
| } | ||
|
|
||
| func oneShotRetryLabel(err error) string { | ||
| var apiErr *apiError | ||
| if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusTooManyRequests && apiErr.Code == "workspace_busy" { | ||
| return "workspace busy" | ||
| } | ||
| if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusTooManyRequests { | ||
| return "rate limited" | ||
| } | ||
| return "service unavailable" | ||
| } | ||
|
|
||
| func (p oneShotGETRetryPolicy) getWorkspaceBytes(ctx context.Context, client *workspaceCommandClient, pathForWorkspace func(string) string) ([]byte, string, error) { | ||
| var body []byte | ||
| var contentType string | ||
| err := p.run(ctx, func(ctx context.Context) error { | ||
| var err error | ||
| body, contentType, err = client.getWorkspaceBytes(ctx, pathForWorkspace) | ||
| return err | ||
| }) | ||
| return body, contentType, err | ||
| } | ||
|
|
||
| func (p oneShotGETRetryPolicy) getWorkspaceJSON(ctx context.Context, client *workspaceCommandClient, pathForWorkspace func(string) string, out any) error { | ||
| return p.run(ctx, func(ctx context.Context) error { | ||
| return client.getWorkspaceJSON(ctx, pathForWorkspace, out) | ||
| }) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Rate limits mislabeled as outages
When a
429 rate_limitedresponse triggers a retry,oneShotRetryLabelprints “service unavailable.” Users see an outage notice instead of the rate limit that blocked their read.Was this helpful? React with 👍 or 👎 to provide feedback.