public RateLimitResult Process(string key, string period, int maxCalls) { var result = new RateLimitResult(); var value = _memoryCache.GetOrCreate(key, c => { var expiration = DateTime.Now.Add(NotationToTimeSpan.Parse(period)); var item = new CacheEntry(expiration); c.AbsoluteExpiration = expiration; c.Value = item; return(item); }); value.Count++; var remaining = maxCalls - value.Count; remaining = remaining > 0 ? remaining : 0; return(new RateLimitResult { IsSuccess = value.Count <= maxCalls, RateLimitReset = value.AbsoluteExpiration, RequestLimit = maxCalls, RequestPeriod = period, RequestsRemaining = remaining }); }
public void Should_parse_seconds(string notation, int value) { Assert.Equal(TimeSpan.FromSeconds(value), NotationToTimeSpan.Parse(notation)); }
public void Should_throw_on_error(string notation) { Assert.Throws <FormatException>(() => NotationToTimeSpan.Parse(notation)); }