diff --git a/lib/src/rate_limiter.dart b/lib/src/rate_limiter.dart index e7bba885..8ff3ead4 100644 --- a/lib/src/rate_limiter.dart +++ b/lib/src/rate_limiter.dart @@ -52,6 +52,25 @@ abstract class RateLimiter { return Future.value(1); } + /// Alerts the user of information pertinent to the current [window]. + /// + /// The default implementation is to send the following headers, akin to + /// Github's v4 Graph API: + /// * `X-RateLimit-Limit`: The maximum number of points consumed per window. + /// * `X-RateLimit-Remaining`: The remaining number of points that may be consumed + /// before the rate limit is reached for the current window. + /// * `X-RateLimit-Reset`: The Unix timestamp, at which the window will + /// reset. + FutureOr sendWindowInformation(RequestContext req, ResponseContext res, + RateLimitingWindow window) { + res.headers.addAll({ + 'x-ratelimit-limit': window.pointLimit.toString(), + 'x-ratelimit-remaining': window.remainingPoints.toString(), + 'x-ratelimit-reset': + (window.resetsAt.millisecondsSinceEpoch ~/ 1000).toString(), + }); + } + /// Signals to a user that they have exceeded the rate limit for the /// current window. ///