-
Notifications
You must be signed in to change notification settings - Fork 21
Queue for a resilience slot instead of failing parallel invocations #712
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b1c0cec
Queue for a resilience slot instead of failing parallel invocations
jeremy f4503f0
Keep the rate limiter's sleep inside the budget and round Retry-After up
jeremy 6d3796e
Fail fast on an open circuit and let cancellation outrank the gate de…
jeremy 2199275
Check the gate deadline before every attempt, and cancellation before…
jeremy c6e10bb
Convert gate rejections centrally and report the effective request rate
jeremy 836c236
Report the whole gate wait and release a slot taken under cancellation
jeremy 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
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,34 @@ | ||
| package output | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/basecamp/basecamp-cli/internal/resilience" | ||
| ) | ||
|
|
||
| // A gate rejection reaches the user with its own message and hint through | ||
| // the central conversion too, so commands that return SDK errors directly | ||
| // render the same thing as those that call convertSDKError. | ||
| func TestAsErrorCarriesTheGateMessageAndHint(t *testing.T) { | ||
| store := resilience.NewStore(t.TempDir()) | ||
| require.NoError(t, store.Update(func(state *resilience.State) error { | ||
| state.Bulkhead.ActivePIDs = []int{os.Getppid()} | ||
| return nil | ||
| })) | ||
| gateErr := resilience.NewBulkhead(store, resilience.BulkheadConfig{MaxConcurrent: 1}).Wait(context.Background(), time.Now()) | ||
| require.Error(t, gateErr) | ||
|
|
||
| err := AsError(fmt.Errorf("listing chat lines: %w", gateErr)) | ||
|
|
||
| assert.Equal(t, CodeRateLimit, err.Code) | ||
| assert.Equal(t, "Too many concurrent basecamp processes (limit 1); waited 0s", err.Message) | ||
| assert.Equal(t, "Re-run, or lower parallelism.", err.Hint) | ||
| assert.True(t, err.Retryable) | ||
| } |
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
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,60 @@ | ||
| package resilience | ||
|
|
||
| import ( | ||
| "context" | ||
| "math/rand/v2" | ||
| "time" | ||
| ) | ||
|
|
||
| // DefaultMaxWait is how long OnOperationGate queues for a token or a slot | ||
| // before giving up. It is long enough to absorb a burst of parallel CLI | ||
| // invocations (10 workers draining a 50-token bucket refill at 10/s in well | ||
| // under a second) and short enough that a genuinely saturated machine still | ||
| // fails within one attention span. | ||
| const DefaultMaxWait = 10 * time.Second | ||
|
|
||
| // GateError is a gate rejection that already waited its turn. It unwraps to | ||
| // the SDK sentinel (basecamp.ErrRateLimited or basecamp.ErrBulkheadFull) so | ||
| // existing errors.Is checks keep working, and carries the message and hint | ||
| // the CLI shows: which limit, how long it waited, and what to do. | ||
| type GateError struct { | ||
| Message string | ||
| Hint string | ||
| sentinel error | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| func (e *GateError) Error() string { return e.Message } | ||
|
|
||
| // Unwrap returns the SDK sentinel the rejection stands for. | ||
| func (e *GateError) Unwrap() error { return e.sentinel } | ||
|
|
||
| // pause sleeps for d or until ctx is done, whichever comes first. | ||
| func pause(ctx context.Context, d time.Duration) error { | ||
| timer := time.NewTimer(d) | ||
| defer timer.Stop() | ||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case <-timer.C: | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // jittered stretches d by up to half again so that processes which woke | ||
| // together do not retry in lockstep and starve each other at the lock. | ||
| func jittered(d time.Duration) time.Duration { | ||
| return d + jitter(d) | ||
| } | ||
|
|
||
| // jitter is the random stretch jittered adds; a variable so tests can pin it. | ||
| var jitter = func(d time.Duration) time.Duration { | ||
| return rand.N(d/2 + 1) //nolint:gosec // jitter spreads retries; it guards nothing | ||
| } | ||
|
|
||
| // ceilSeconds rounds d up to a whole second. | ||
| func ceilSeconds(d time.Duration) time.Duration { | ||
| if d%time.Second == 0 { | ||
| return d | ||
| } | ||
| return d.Truncate(time.Second) + time.Second | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.