RateLimiter<User> class
abstract
A base class that facilitates rate limiting API's or endpoints, typically to prevent spam and abuse.
The rate limiter operates under the assumption that a User
object
can be computed from each request, as well as information about
the current rate-limiting window.
- Implementers
Constructors
- RateLimiter(int maxPointsPerWindow, Duration windowDuration, {String? errorMessage})
Properties
- errorMessage → String
-
The error message to send to a
User
who has exceeded the rate limit during the current window.final - hashCode → int
-
The hash code for this object.
no setterinherited
- maxPointsPerWindow → int
-
The maximum number of points that may be consumed
within the given windowDuration.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- windowDuration → Duration
-
The amount of time, during which, a user is not allowed to consume
more than maxPointsPerWindow.
final
Methods
-
getCurrentWindow(
RequestContext req, ResponseContext res, DateTime currentTime) → FutureOr< RateLimitingWindow< User> > - Computes the current window in which the user is acting.
-
getEndpointCost(
RequestContext req, ResponseContext res, RateLimitingWindow< User> window) → FutureOr<int> -
Computes the amount of points that a given request will cost. This amount
is then added to the amount of points that the user has already consumed
in the current
window
. -
handleRequest(
RequestContext req, ResponseContext res) → Future -
A request middleware that returns
true
if the user has not yet exceeded the maxPointsPerWindow. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
rejectRequest(
RequestContext req, ResponseContext res, RateLimitingWindow< User> window, DateTime currentTime) → FutureOr<Object> -
Signals to a user that they have exceeded the rate limit for the
current window, and terminates execution of the current
RequestContext
. -
sendWindowInformation(
RequestContext req, ResponseContext res, RateLimitingWindow< User> window) → FutureOr<void> -
Alerts the user of information pertinent to the current
window
. -
toString(
) → String -
A string representation of this object.
inherited
-
updateCurrentWindow(
RequestContext req, ResponseContext res, RateLimitingWindow< User> window, DateTime currentTime) → FutureOr<void> -
Updates the underlying store with information about the new
window
that the user is operating in.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited