hit method

  1. @override
RateLimitHit hit(
  1. String key,
  2. Duration window
)
override

Records a request for key within window and returns the new state.

Implementation

@override
RateLimitHit hit(String key, Duration window) {
  final now = DateTime.now();
  final existing = _hits[key];
  final RateLimitHit updated;
  if (existing == null || !now.isBefore(existing.resetAt)) {
    updated = RateLimitHit(1, now.add(window)); // new window
  } else {
    updated = RateLimitHit(existing.count + 1, existing.resetAt);
  }
  _hits[key] = updated;
  if (_hits.length > sweepThreshold) {
    _hits.removeWhere((_, h) => now.isAfter(h.resetAt));
  }
  return updated;
}