public RateLimiterRule Occur(IDateTime dateTime) { var current = _counterQueue.LastOrDefault(); if (current == null || current.Start < dateTime.GetUtcNow().AddMinutes(-1)) { current = new MinuteCounter(dateTime.GetUtcNow()); lock (_syncLock) { _counterQueue.Add(current); while (_counterQueue.Count > _minutesToRetainCount) { _counterQueue.RemoveAt(0); } } } foreach (var rule in _rules) { var count = _counterQueue .Where(c => c.Start >= dateTime.GetUtcNow().AddMinutes(-rule.Minutes)) .Sum(c => c.Count); if (count >= rule.Count) { return(rule); } } current.Count++; return(null); }