public async Task <RateLimitingValidationResponse> ValidateContext(RateLimitingValidationContext rateLimitingValidationContext)
        {
            var rateLimitingCounter = new RateLimitingCounter
            {
                Timestamp = DateTime.UtcNow,
                Count     = 1
            };
            var expirationTime = rateLimitingValidationContext.Period.ToTimeSpan();

            var counterKey = await _rateLimitingCounterKeyBuilder.BuildCounterKey(rateLimitingValidationContext);

            var rateLimitingCounterEntry = await _rateLimitingCounterStore.GetCounter(counterKey);

            if (rateLimitingCounterEntry != null)
            {
                rateLimitingCounter        = rateLimitingCounterEntry;
                rateLimitingCounter.Count += 1;
                var expirationDateTime = rateLimitingCounterEntry.Timestamp + rateLimitingValidationContext.Period.ToTimeSpan();
                expirationTime = expirationDateTime - DateTime.UtcNow;
            }
            await _rateLimitingCounterStore.SetCounter(counterKey, rateLimitingCounterEntry, expirationTime);

            var rateLimitingValidationResponse = new RateLimitingValidationResponse
            {
                RateLimitingValidationContext = rateLimitingValidationContext,
                RateLimitingCounter           = rateLimitingCounter
            };

            return(rateLimitingValidationResponse);
        }
Ejemplo n.º 2
0
 public Task SetCounter(string counterKey, RateLimitingCounter rateLimitingCounter, TimeSpan expirationTime)
 {
     _memoryCache.Set(counterKey, rateLimitingCounter, expirationTime);
     return(Task.CompletedTask);
 }