handle method
Inspect or reject the request. Throw to abort the chain.
The default implementation does nothing, so a middleware that only overrides process does not need to define it.
Implementation
@override
Future<void> handle(Request req) async {
// Fails closed: an unset APP_ENV is treated as production, so an app
// that never sets it still gets rate limiting.
if (bypassInDevelopment &&
env<String>('APP_ENV', 'production') == 'development') {
return;
}
final String identifier = await _getRequestIdentifier(req);
final remaining = _throttle.remainingAttempts(identifier);
_addRateLimitHeaders(req.response, remaining);
if (!_throttle.request(identifier)) {
final retryAfter = _throttle.retryAfter(identifier);
throw ThrottleException(
message: customMessage ?? 'Too Many Requests. Please try again later.',
code: HttpStatus.tooManyRequests,
headers: {'Retry-After': retryAfter.inSeconds.toString(), ...?headers},
);
}
}