The RateLimitResolver defines the strategy contract for resolving one or more RateLimitStorage instances for a specific RateLimit operation.
It acts as the bridge between annotation-level configuration (RateLimit) and runtime rate-limiting infrastructure (RateLimitStorage and RateLimitManager).
Purpose
In a multi-storage environment, different rate limits may apply to different
tiers, domains, or regions of the system.
The RateLimitResolver encapsulates the decision logic for mapping a rate limit
declaration to the correct storage(s).
For instance, a global API rate limit might use a distributed Redis storage, while a user-specific limit could rely on a local in-memory backend.
Behavior
- Given a RateLimit annotation, the resolver selects one or more storages where the rate-limit state should be read and updated.
- The resolution may depend on:
- Explicit storage names declared in the annotation.
- Default storage selection rules.
- Contextual information (such as tenant, environment, or endpoint).
Example
@RateLimit(['redis-rate-limit', 'local-cache'], limit: 100, window: Duration(seconds: 30))
Future<Response> getUserData(Request req) async {
...
}
// During runtime
final resolver = DefaultRateLimitResolver(manager);
final storages = await resolver.resolveStorages(rateLimitAnnotation);
for (final storage in storages) {
final allowed = await storage.tryConsume('user:42', limit: rateLimit.limit, window: rateLimit.window);
if (!allowed) throw TooManyRequestsException();
}
Implementation Notes
- Implementations may use priority rules, profile-based resolution, or caching mechanisms to optimize lookups.
- The resolver should be stateless or thread-safe if shared globally.
Related Components
- RateLimit: Annotation defining the rate-limiting metadata.
- RateLimitManager: Provides access to available storages.
- RateLimitStorage: The backend persistence mechanism.
RateLimiter: Uses this resolver during evaluation.
- Implementers
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
resolveStorages(
RateLimit rateLimit, MethodInvocation invocation) → FutureOr< Iterable< RateLimitStorage> > - Resolves the appropriate RateLimitStorage instances for a given RateLimit operation.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited