feat: add try_wait_n for atomic multi-token acquisition#8
Merged
Conversation
Consumes n tokens in a single CAS or fails without partial consumption. Introduces TryWaitError with two variants: Insufficient(Duration) for transient shortage (same retry semantics as try_wait's Err) and ExceedsCapacity when n > max_tokens. The capacity check runs before refill so callers get a clear signal instead of spinning against a bucket that can never fill. try_wait() is unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Ratelimiter::try_wait_n(n)— atomically consumesntokens in a single CAS, or fails without partial consumption.TryWaitErrorwithInsufficient(Duration)(transient; retry after duration) andExceedsCapacity(n > max_tokens; waiting will never satisfy).try_wait()signature unchanged — stillResult<(), Duration>.Changes
Test plan
cargo test— 23 tests pass (4 new covering try_wait_n basic, zero-noop, unlimited, no-partial-consumption, and exceeds-capacity)cargo test --releasecargo clippy --all-targets -- -D warningscargo fmt --all -- --checkRUSTDOCFLAGS="-D warnings" cargo doc --no-depsmax_tokens >= batch_sizeat construction, or handleTryWaitError::ExceedsCapacityexplicitly🤖 Generated with Claude Code