private async Task <bool> HandleCaseWhenPrevAndCurrMinHasValue(ThrottleRequest throttleRequest, string userRateLimitConfigCacheKey, DateTime now, DateTime currMin, int currMinIntegerValue, int prevMinIntergerValue) { if (currMinIntegerValue >= throttleRequest.AppliedConfig.PerMinLimit) { return(true); } var currWindow = now - now.AddMinutes(-1); var prevMinPartInCurrWindow = currMin - (now.AddMinutes(-1)); var prevMinFractionalPartInCurrWindow = prevMinPartInCurrWindow / currWindow; var calculatedvalue = (int)(currMinIntegerValue + prevMinIntergerValue * prevMinFractionalPartInCurrWindow); if (calculatedvalue >= throttleRequest.AppliedConfig.PerMinLimit) { return(true); } if (_nextThrottler == null) { await _cacheManager.HashIncrementAsync(userRateLimitConfigCacheKey, currMin.ToString()); return(false); } var shouldThrottle = await _nextThrottler.ShouldThrottle(throttleRequest); if (!shouldThrottle) { await _cacheManager.HashIncrementAsync(userRateLimitConfigCacheKey, currMin.ToString()); } return(shouldThrottle); }
private async Task <bool> HandleCaseWhenPrevAndCurrSecHasValue(ThrottleRequest throttleRequest, string userRateLimitConfigCacheKey, DateTime now, DateTime currSec, int currSecValue, int prevSecValue) { if (currSecValue > throttleRequest.AppliedConfig.PerSecLimit) { return(true); } var currWindow = now - (now.AddSeconds(-1)); var prevSecPartInCurrWindow = currSec - (now.AddSeconds(-1)); var prevSecFractionInCurrWindow = prevSecPartInCurrWindow / currWindow; var calculatedThrottleLimit = (int)(currSecValue + (prevSecValue * prevSecFractionInCurrWindow)); if (calculatedThrottleLimit >= throttleRequest.AppliedConfig.PerSecLimit) { return(true); } if (_nextThrottler == null) { await _redisCacheManager.HashIncrementAsync(userRateLimitConfigCacheKey, currSec.ToString()); return(false); } var shouldThrottle = await _nextThrottler.ShouldThrottle(throttleRequest); if (!shouldThrottle) { await _redisCacheManager.HashIncrementAsync(userRateLimitConfigCacheKey, currSec.ToString()); } return(shouldThrottle); }