PrfRateLimiter class

A robust, industry-grade token bucket rate limiter using prf.

Limits actions to a defined number within a given duration, using a refillable token system with persistent storage.

Example:

final limiter = PrfRateLimiter('chat_send', maxTokens: 100, refillDuration: Duration(minutes: 15));
final canSend = await limiter.tryConsume();

Constructors

PrfRateLimiter.new(dynamic prefix, {required int maxTokens, required Duration refillDuration})
Creates a new rate limiter with the specified prefix and configuration.

Properties

hashCode int
The hash code for this object.
no setterinherited
maxTokens int
The maximum number of tokens that can be accumulated.
final
refillDuration Duration
The time period over which tokens are fully replenished.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

anyStateExists() Future<bool>
Checks if any rate limiter state exists in persistent storage.
debugStats() Future<PrfRateLimiterStats>
Returns detailed stats useful for debugging and logging.
getAvailableTokens() Future<double>
Gets the number of tokens currently available.
isLimitedNow() Future<bool>
Returns true if the limiter is currently rate-limited (no token available).
isReady() Future<bool>
Returns true if the limiter has at least one token available.
nextAllowedTime() Future<DateTime>
Returns the DateTime when a token will be available. Returns DateTime.now() if already available.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
removeAll() Future<void>
Removes all persisted state from storage.
reset() Future<void>
Fully resets the limiter to its initial state.
runIfAllowed<T>(Future<T> action()) Future<T?>
Executes the provided action if a token is available, otherwise returns null.
timeUntilNextToken() Future<Duration>
Calculates the time remaining until the next token is available.
toString() String
A string representation of this object.
inherited
tryConsume() Future<bool>
Attempts to consume 1 token.

Operators

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