예제 #1
0
        public void IncrementRequestCount()
        {
            ThrottleInfo throttleInfo = getThrottleInfoFromCache();

            throttleInfo.RequestCount++;
            _cache[ThrottleGroup] = throttleInfo;
        }
예제 #2
0
        public static bool IsThrottled(string key)
        {
            //Gets the rate limit by AppId from configuration file.
            int _rateLimit = Config.GetRateLimitByAppId(key);

            //Check to see if request for this appId is blocked for 5 minutes.
            _cache.TryGetValue($"{key}-BLOCKED", out ThrottleInfo throttled);
            if (throttled != null && throttled.Expiry > DateTime.Now)
            {
                return(true);
            }
            else
            {
                _cache.TryRemove($"{key}-BLOCKED", out ThrottleInfo _);
            }

            //Gets value from cache by appId, If does not exist then add in cache.
            _cache.TryGetValue(key, out ThrottleInfo throttleInfo);


            if (throttleInfo != null && throttleInfo.Expiry <= DateTime.Now)
            {
                throttleInfo        = new ThrottleInfo();
                throttleInfo.Expiry = DateTime.Now.AddSeconds(Config.DefaultExpiryTime);

                _cache.AddOrUpdate(key, throttleInfo, (oldVal, newVal) => throttleInfo);
            }

            if (throttleInfo == null)
            {
                throttleInfo = new ThrottleInfo
                {
                    RequestCount = 1,
                    Expiry       = DateTime.Now.AddSeconds(Config.DefaultExpiryTime)
                };

                _cache.AddOrUpdate(key, throttleInfo, (oldVal, newVal) => throttleInfo);
            }
            else
            {
                //updates the request count by 1 in the cache per request.
                throttleInfo.RequestCount++;

                //Check if the appId has exceeded the number of requests.
                //If yes then add a new key in the cache with suffix "{appId}-BLOCKED" and set expiry to 5 minutes
                //This means all request for "{appId}" will be temporarily blocked for 5 minutes.
                if (throttleInfo.RequestCount > _rateLimit)
                {
                    throttleInfo.Expiry = DateTime.Now.AddMinutes(Config.PenaltyInMinutes); //Penalty 5 minutes.
                    _cache.TryAdd($"{key}-BLOCKED", throttleInfo);
                }
            }
            return(throttleInfo.RequestCount > _rateLimit);
        }
예제 #3
0
        public Dictionary <string, string> GetRateLimitHeaders()
        {
            ThrottleInfo throttleInfo = getThrottleInfoFromCache();

            int requestsRemaining = Math.Max(RequestLimit - throttleInfo.RequestCount, 0);

            var headers = new Dictionary <string, string>();

            headers.Add("X-RateLimit-Limit", RequestLimit.ToString());
            headers.Add("X-RateLimit-Remaining", RequestsRemaining.ToString());
            headers.Add("X-RateLimit-Reset", toUnixTime(throttleInfo.ExpiresAt).ToString());
            return(headers);
        }
예제 #4
0
        public Dictionary <string, string> GetRateLimitHeaders()
        {
            ThrottleInfo throttleInfo = GetThrottleInfoFromCache();

            int requestsRemaining = Math.Max(RequestLimit - throttleInfo.RequestCount, 0);

            var headers = new Dictionary <string, string>
            {
                { "X-RateLimit-Limit", RequestLimit.ToString() },
                { "X-RateLimit-Remaining", RequestsRemaining.ToString() },
                { "X-RateLimit-Reset", throttleInfo.ExpiresAt.ToString(CultureInfo.InvariantCulture) }
            };

            return(headers);
        }
예제 #5
0
        private ThrottleInfo getThrottleInfoFromCache()
        {
            ThrottleInfo throttleInfo =
                _cache.ContainsKey(ThrottleGroup) ? _cache[ThrottleGroup] : null;

            if (throttleInfo == null || throttleInfo.ExpiresAt <= DateTime.Now)
            {
                throttleInfo = new ThrottleInfo
                {
                    ExpiresAt    = DateTime.Now.AddSeconds(_timeoutInSeconds),
                    RequestCount = 0
                };
            }
            ;

            return(throttleInfo);
        }
예제 #6
0
        public void IncrementRequestCount()
        {
            //_cache.AddOrUpdate(ThrottleGroup, new ThrottleInfo
            //{
            //    ExpiresAt = DateTime.Now.AddSeconds(_timeoutInSeconds),
            //    RequestCount = 1
            //}, (retrievedKey, throttleInfo) =>
            //{
            //    _throttleInfoRequestCount = throttleInfo.RequestCount;
            //    Interlocked.Increment(ref _throttleInfoRequestCount);
            //    return throttleInfo;
            //});

            ThrottleInfo throttleInfo = GetThrottleInfoFromCache();

            throttleInfo.RequestCount++;
            _cache[ThrottleGroup] = throttleInfo;
        }
예제 #7
0
        public bool RequestShouldBeThrottled()
        {
            ThrottleInfo throttleInfo = _cache.ContainsKey(_key) ? _cache[_key] : null;

            if (throttleInfo == null || throttleInfo.ExpiresAt <= DateTime.Now)
            {
                throttleInfo = new ThrottleInfo
                {
                    ExpiresAt    = DateTime.Now.AddSeconds(_timeoutInSeconds),
                    RequestCount = 0
                };
            }
            ;

            throttleInfo.RequestCount++;

            _cache[_key] = throttleInfo;

            return(throttleInfo.RequestCount > _requestLimit);
        }
예제 #8
0
        public bool RequestShouldBeThrottled()
        {
            ThrottleInfo throttleInfo = _cache.ContainsKey(_key) ? _cache[_key] : null;

            if (throttleInfo == null || throttleInfo.ExpiresAt <= DateTime.Now)
            {
                throttleInfo = new ThrottleInfo
                {
                    ExpiresAt    = DateTime.Now.AddSeconds(TimeoutInSeconds),
                    RequestCount = 0
                };
            }
            ;

            WindowResetDate = throttleInfo.ExpiresAt;

            throttleInfo.RequestCount++;

            _cache[_key] = throttleInfo;

            RequestsRemaining = Math.Max(RequestLimit - throttleInfo.RequestCount, 0);

            return(throttleInfo.RequestCount > RequestLimit);
        }