From 745d07c1a9c02300545eb06584ac1dd1145894c3 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 14 Aug 2019 15:28:44 -0400 Subject: [PATCH] sendWindowInfo --- lib/src/rate_limiter.dart | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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. ///