Пример #1
0
        public RateLimitCounter GetCurrentRateLimitCounter(ClientRequestIdentity requestIdentity, RateLimitRule rule)
        {
            var counter = new RateLimitCounter
            {
                Timestamp     = DateTime.UtcNow,
                TotalRequests = 0
            };

            var counterId = ComputeCounterKey(requestIdentity, rule);

            // serial reads and writes
            lock (_processLocker)
            {
                var entry = _counterStore.Get(counterId);
                if (entry.HasValue)
                {
                    // deep copy
                    counter = new RateLimitCounter
                    {
                        Timestamp     = entry.Value.Timestamp,
                        TotalRequests = entry.Value.TotalRequests
                    };
                }
            }
            return(counter);
        }
        /// <summary>
        /// 处理请求数
        /// </summary>
        /// <param name="requestIdentity"></param>
        /// <param name="rule"></param>
        /// <returns></returns>
        private RateLimitCounter ProcessRequest(RequestIdentity requestIdentity, RateLimitRule rule)
        {
            var counter = new RateLimitCounter
            {
                Timestamp     = DateTime.Now,
                TotalRequests = 1
            };

            var counterId = ComputeCounterKey(requestIdentity, rule);

            lock (_processLocker)
            {
                var entry = _counterStore.Get(counterId);
                if (entry != null)
                {
                    // 没有过期,请求数+1
                    if (entry.Timestamp + rule.PeriodTimespan.Value >= DateTime.Now)
                    {
                        var totalRequests = entry.TotalRequests + 1;
                        counter = new RateLimitCounter
                        {
                            Timestamp     = entry.Timestamp,
                            TotalRequests = totalRequests
                        };
                    }
                }
                _counterStore.Set(counterId, counter, rule.PeriodTimespan.Value);
            }
            return(counter);
        }
Пример #3
0
        public RateLimitCounter ProcessRequest(ClientRequestIdentity requestIdentity, RateLimitRule rule)
        {
            var counter = new RateLimitCounter
            {
                Timestamp     = DateTime.UtcNow,
                TotalRequests = 1
            };

            var counterId = ComputeCounterKey(requestIdentity, rule);

            // serial reads and writes
            lock (_processLocker)
            {
                var entry = _counterStore.Get(counterId);
                if (entry.HasValue)
                {
                    // entry has not expired
                    if (entry.Value.Timestamp + rule.PeriodTimespan.Value >= 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)
                _counterStore.Set(counterId, counter, rule.PeriodTimespan.Value);
            }

            return(counter);
        }