Esempio n. 1
0
        public ValueTask RemoveAsync(ThrottlerItem throttlerItem, CancellationToken cancellationToken)
        {
            var key = throttlerItem.GenerateCounterKey();

            _cache.Remove(key);
            return(new ValueTask());
        }
Esempio n. 2
0
        public ValueTask SetAsync(ThrottlerItem throttlerItem, Counter counter, TimeSpan?expirationTime, CancellationToken cancellationToken)
        {
            var key = throttlerItem.GenerateCounterKey();

            _cache[key] = counter;
            return(new ValueTask());
        }
Esempio n. 3
0
        public ValueTask <Counter?> GetAsync(ThrottlerItem throttlerItem, CancellationToken cancellationToken)
        {
            var key = throttlerItem.GenerateCounterKey();

            if (_cache.TryGetValue(key, out var counter))
            {
                return(new ValueTask <Counter?>(counter));
            }
            else
            {
                return(new ValueTask <Counter?>(default(Counter?)));
            }
        }
Esempio n. 4
0
        public static ThrottlerItem Get(ThrottlerItem throttlerItem)
        {
            if (throttlerItem == null)
            {
                return(default(ThrottlerItem));
            }


            var key = throttlerItem.GenerateKey();

            if (_cache.TryGetValue(key, out ThrottlerItem stored))
            {
                return(stored);
            }

            return(throttlerItem);
        }
Esempio n. 5
0
        public bool IsAllowed()
        {
            if (_actionExecutingContext == null || _cache == null || _current == null)
            {
                return(true);
            }

            // 获取路由
            var actionDescriptor = _actionExecutingContext.ActionDescriptor as ControllerActionDescriptor;
            var actionName       = actionDescriptor?.ActionName;
            var controllerName   = actionDescriptor?.ControllerName;

            if (string.IsNullOrEmpty(actionName) || string.IsNullOrEmpty(controllerName))
            {
                return(true);
            }

            var route  = $"/{controllerName}/{actionName}";
            var device = _current.Device;

            if (device == null)
            {
                return(false);
            }

            ThrottlerItem throttlerItem = new ThrottlerItem {
                Identity  = device.DeviceUuid,
                Route     = route,
                RuleName  = nameof(DeviceInTimeMaxRequestRuleHandler),
                OptionKey = _optisons.GenerateKey()
            };

            throttlerItem = ThrottlerMemoryStore.Get(throttlerItem);


            var startTime = DateTime.Now;

            switch (_optisons.TimeUnit)
            {
            case TimeUnitEnum.Second: {
                startTime = DateTime.Now.AddSeconds(-_optisons.TimeValue);
                break;
            }

            case TimeUnitEnum.Minute: {
                startTime = DateTime.Now.AddMinutes(-_optisons.TimeValue);
                break;
            }

            case TimeUnitEnum.Hours: {
                startTime = DateTime.Now.AddHours(-_optisons.TimeValue);
                break;
            }

            case TimeUnitEnum.Day: {
                startTime = DateTime.Now.AddDays(-_optisons.TimeValue);
                break;
            }

            case TimeUnitEnum.Today: {
                startTime = DateTimeHelper.GetDayStart(startTime);
                break;
            }
            }

            bool isAllowed = true;

            // 检查多少时间内有多少个请求
            if (throttlerItem.LastRequests.All(i => i > startTime))
            {
                if (throttlerItem.LastRequests.Count >= _optisons.MaxRequest)
                {
                    // 请求太多
                    isAllowed = false;
                }
            }


            // 添加当前请求队列
            if (isAllowed)
            {
                throttlerItem.LastRequestTime = DateTime.Now;
                // 清除无需计算得队列
                if (throttlerItem.LastRequests.Count >= _optisons.MaxRequest)
                {
                    DateTime dequeueItem;
                    throttlerItem.LastRequests.TryDequeue(out dequeueItem);
                }
                throttlerItem.LastRequests.Enqueue(throttlerItem.LastRequestTime);
                ThrottlerMemoryStore.Set(throttlerItem);
            }

            return(isAllowed);
        }
Esempio n. 6
0
        public static void Set(ThrottlerItem throttlerItem)
        {
            var key = throttlerItem.GenerateKey();

            _cache.AddOrUpdate(key, throttlerItem, (key, oldValue) => oldValue = throttlerItem);
        }
Esempio n. 7
0
        public static void Remove(ThrottlerItem throttlerItem)
        {
            var key = throttlerItem.GenerateKey();

            _cache.TryRemove(key, out ThrottlerItem stored);
        }