RateLimiter class

Token bucket algorithm for sustained rate limiting with burst capacity. Uses Stopwatch (monotonic clock) for accurate timing.

Constructors

RateLimiter({int maxTokens = 10, int refillRate = 1, Duration refillInterval = const Duration(seconds: 1), bool enabled = true, bool debugMode = false, String? name, void onMetrics(int tokensRemaining, bool acquired)?})

Properties

availableTokens int
Current available tokens (rounded down).
no setter
canAcquire bool
Whether at least one token is available.
no setter
debugMode bool
final
enabled bool
Whether rate limiting is enabled. If false, all calls succeed.
final
hashCode int
The hash code for this object.
no setterinherited
maxTokens int
Maximum tokens in the bucket (burst capacity).
final
name String?
final
onMetrics → void Function(int tokensRemaining, bool acquired)?
Callback for metrics tracking.
final
refillInterval Duration
How often tokens are refilled.
final
refillRate int
Number of tokens to add per refillInterval.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
timeUntilNextToken Duration
Time until next token is available.
no setter

Methods

call(VoidCallback callback, [int tokens = 1]) bool
Execute callback if token is available, otherwise do nothing.
callAsync<T>(Future<T> callback(), [int tokens = 1]) Future<T?>
Execute async callback if token is available.
dispose() → void
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
reset() → void
Reset to full capacity.
toString() String
A string representation of this object.
inherited
tryAcquire([int tokens = 1]) bool
Try to acquire tokens tokens. Returns true if successful.

Operators

operator ==(Object other) bool
The equality operator.
inherited