tryAcquire method

bool tryAcquire([
  1. int tokens = 1
])

Tries to spend tokens (default 1). Returns true and deducts them if enough have accrued, false otherwise (no partial spend). Throws ArgumentError if tokens is below 1 or above capacity — a request larger than the bucket can ever hold is a programming error, not a denial. Audited: 2026-06-12 11:26 EDT

Implementation

bool tryAcquire([int tokens = 1]) {
  _validate(tokens);
  _refill();
  if (_tokens >= tokens) {
    _tokens -= tokens;
    return true;
  }
  return false;
}