Skip to content

feat: add try_wait_n for atomic multi-token acquisition#8

Merged
brayniac merged 1 commit into
iopsystems:mainfrom
brayniac:try-wait-n
Apr 21, 2026
Merged

feat: add try_wait_n for atomic multi-token acquisition#8
brayniac merged 1 commit into
iopsystems:mainfrom
brayniac:try-wait-n

Conversation

@brayniac

Copy link
Copy Markdown
Contributor

Summary

  • Adds Ratelimiter::try_wait_n(n) — atomically consumes n tokens in a single CAS, or fails without partial consumption.
  • Introduces TryWaitError with Insufficient(Duration) (transient; retry after duration) and ExceedsCapacity (n > max_tokens; waiting will never satisfy).
  • try_wait() signature unchanged — still Result<(), Duration>.

Changes

  • c9dc405 feat: add try_wait_n for atomic multi-token acquisition

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 --release
  • cargo clippy --all-targets -- -D warnings
  • cargo fmt --all -- --check
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps
  • Downstream integration (e.g. cachecannon): verify max_tokens >= batch_size at construction, or handle TryWaitError::ExceedsCapacity explicitly

🤖 Generated with Claude Code

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>
@brayniac brayniac merged commit c19d324 into iopsystems:main Apr 21, 2026
6 of 7 checks passed
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.

1 participant