public RateLimitCounter Process(RequestIdentity requestIdentity, CounterRule rule) { var counter = new RateLimitCounter { Timestamp = DateTime.UtcNow, TotalRequests = 1 }; var counterId = GetRequestIdentityKey(requestIdentity, rule); // serial reads and writes lock (_processLocker) { var entry = _store.Get(counterId); if (entry.HasValue) { // entry has not expired if (entry.Value.Timestamp.AddSeconds(rule.Period) >= DateTime.UtcNow) { // increment request count var totalRequests = entry.Value.TotalRequests + 1; // deep copy counter = new RateLimitCounter { Timestamp = entry.Value.Timestamp, TotalRequests = totalRequests }; } } // stores: id (string) - timestamp (datetime) - total_requests (long) _store.Set(counterId, counter, TimeSpan.FromSeconds(rule.Period)); } return(counter); }
public void Set(string key, RateLimitCounter counter, TimeSpan expiration) { _manager.Set(key, counter, expiration); }