Use points instead of request count
This commit is contained in:
parent
71f2c748f1
commit
a202b189be
1 changed files with 13 additions and 13 deletions
|
@ -10,29 +10,29 @@ import 'rate_limiting_window.dart';
|
||||||
/// can be computed from each request, as well as information about
|
/// can be computed from each request, as well as information about
|
||||||
/// the current rate-limiting window.
|
/// the current rate-limiting window.
|
||||||
abstract class RateLimiter<User> {
|
abstract class RateLimiter<User> {
|
||||||
/// The maximum number of requests allowed within the given [window].
|
/// The maximum number of points that may be consumed
|
||||||
final int maxRequestsPerWindow;
|
/// within the given [windowDuration].
|
||||||
|
final int maxPointsPerWindow;
|
||||||
|
|
||||||
/// The amount of time, during which, a user is not allowed to send
|
/// The amount of time, during which, a user is not allowed to consume
|
||||||
/// more than [maxRequestsPerWindow].
|
/// more than [maxPointsPerWindow].
|
||||||
final Duration window;
|
final Duration windowDuration;
|
||||||
|
|
||||||
RateLimiter(this.maxRequestsPerWindow, this.window);
|
RateLimiter(this.maxPointsPerWindow, this.windowDuration);
|
||||||
|
|
||||||
/// Computes the current window in which the user is acting.
|
/// Computes the current window in which the user is acting.
|
||||||
///
|
///
|
||||||
/// For example, if your API was limited to 1000 requests/hour,
|
/// For example, if your API was limited to 1000 requests/hour,
|
||||||
/// then you would return a window containing the current hour,
|
/// then you would return a window containing the current hour,
|
||||||
/// and the number of requests the user has sent in the past hour.
|
/// and the number of requests the user has sent in the past hour.
|
||||||
FutureOr<RateLimitingWindow<User>> getCurrentWindow(
|
FutureOr<RateLimitingWindow<User>> getCurrentWindow(
|
||||||
RequestContext req, ResponseContext res);
|
RequestContext req, ResponseContext res);
|
||||||
|
|
||||||
/// Updates the underlying store with information about the
|
/// Updates the underlying store with information about the new
|
||||||
/// [newWindow] that the user is operating in.
|
/// [window] that the user is operating in.
|
||||||
FutureOr<void> updateCurrentWindow(RequestContext req, ResponseContext res,
|
FutureOr<void> updateCurrentWindow(RequestContext req, ResponseContext res,
|
||||||
RateLimitingWindow<User> newWindow);
|
RateLimitingWindow<User> window);
|
||||||
|
|
||||||
FutureOr<Object> denyRequest(RequestContext req, ResponseContext res) {
|
FutureOr<Object> denyRequest(RequestContext req, ResponseContext res,
|
||||||
|
RateLimitingWindow<User> window) {}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue