isRateLimited method
Checks if the client IP address is rate-limited.
This function first checks if the given clientIp
is in the blocked
list using the IP blocker. If the IP is blocked, it returns true
.
Otherwise, it checks the number of requests associated with the clientIp
.
If the number of requests exceeds the maxRequests within the
resetDuration, the function returns true
, indicating that the IP is
rate-limited. Otherwise, it records the current request and returns false
.
-
Parameters:
- clientIp: The IP address of the client to check for rate limiting.
-
Returns: A
Future
that resolves totrue
if the IP is rate-limited, orfalse
if the IP is not rate-limited.
Implementation
Future<bool> isRateLimited(String clientIp) async {
if (ipBlocker.isIpBlocked(clientIp)) {
return true;
}
final now = DateTime.now();
_requests[clientIp] ??= [];
_requests[clientIp]!
.removeWhere((time) => now.difference(time) > resetDuration);
if (_requests[clientIp]!.length < maxRequests) {
_requests[clientIp]!.add(now);
_saveRequests();
return false;
}
return true;
}