Пример #1
0
        public async Task <Counter> ProcessRequestAsync(ThrottlerItem throttlerItem, ThrottleRule rule, CancellationToken cancellationToken)
        {
            Counter counter;

            using (await _asyncLock.WriterLockAsync(throttlerItem.GenerateCounterKey()))
            {
                var entry = await _counterStore.GetAsync(throttlerItem, cancellationToken);

                if (entry.HasValue)
                {
                    // entry has not expired
                    if (entry.Value.Timestamp + rule.TimeWindow >= _systemClock.UtcNow)
                    {
                        // increment request count
                        var totalCount = entry.Value.Count + 1;

                        counter = new Counter(entry.Value.Timestamp, totalCount);
                    }
                    else
                    {
                        counter = new Counter(_systemClock.UtcNow, 1);
                    }
                }
                else
                {
                    counter = new Counter(_systemClock.UtcNow, 1);
                }

                await _counterStore.SetAsync(throttlerItem, counter, rule.TimeWindow, cancellationToken);
            }

            return(counter);
        }
Пример #2
0
        public async Task Set_Multiple_Get_Each()
        {
            var store = CreateCounterStore();

            var timestamp1 = DateTime.Now;
            var count1     = 11;

            var throttleRule = new ThrottleRule {
                Quota = 10, TimeWindow = TimeSpan.FromSeconds(10)
            };
            var throttlerItem1 = new ThrottlerItem(throttleRule, "policy", "scope1", "endpoint");
            var throttlerItem2 = new ThrottlerItem(throttleRule, "policy", "scope2", "endpoint");

            await store.SetAsync(throttlerItem1, new Counter(timestamp1, count1), null, CancellationToken.None);

            var timestamp2 = DateTime.Now;
            var count2     = 12;
            await store.SetAsync(throttlerItem2, new Counter(timestamp2, count2), null, CancellationToken.None);

            var counter1 = await store.GetAsync(throttlerItem1, CancellationToken.None);

            var counter2 = await store.GetAsync(throttlerItem2, CancellationToken.None);

            Assert.True(counter1.HasValue);
            Assert.Equal(count1, counter1.Value.Count);
            Assert.Equal(timestamp1, counter1.Value.Timestamp);

            Assert.True(counter2.HasValue);
            Assert.Equal(count2, counter2.Value.Count);
            Assert.Equal(timestamp2, counter2.Value.Timestamp);
        }
Пример #3
0
        public ValueTask RemoveAsync(ThrottlerItem throttlerItem, CancellationToken cancellationToken)
        {
            var key = GenerateThrottlerItemKey(throttlerItem);

            _cache.Remove(key);

            return(new ValueTask());
        }
Пример #4
0
        public ValueTask <Counter?> GetAsync(ThrottlerItem throttlerItem, CancellationToken cancellationToken)
        {
            var key = GenerateThrottlerItemKey(throttlerItem);

            if (_cache.TryGetValue(key, out Counter stored))
            {
                return(new ValueTask <Counter?>(stored));
            }

            return(new ValueTask <Counter?>(default(Counter?)));
        }
        public async ValueTask <Counter?> GetAsync(ThrottlerItem throttlerItem, CancellationToken cancellationToken)
        {
            var key    = GenerateThrottlerItemKey(throttlerItem);
            var stored = await _cache.GetAsync(key, cancellationToken);

            if (stored != null)
            {
                return(Deserialize(stored));
            }

            return(default);
        public async ValueTask SetAsync(ThrottlerItem throttlerItem, Counter counter, TimeSpan?expirationTime, CancellationToken cancellationToken)
        {
            var options = new DistributedCacheEntryOptions();

            if (expirationTime.HasValue)
            {
                options.SetAbsoluteExpiration(expirationTime.Value);
            }

            var key = GenerateThrottlerItemKey(throttlerItem);
            await _cache.SetAsync(key, Serialize(counter), options, cancellationToken);
        }
Пример #7
0
        public async Task Get_Null()
        {
            var store = CreateCounterStore();

            var throttleRule = new ThrottleRule {
                Quota = 10, TimeWindow = TimeSpan.FromSeconds(10)
            };
            var throttlerItem = new ThrottlerItem(throttleRule, "policy", "scope", "endpoint");

            var counter = await store.GetAsync(throttlerItem, CancellationToken.None);

            Assert.False(counter.HasValue);
        }
Пример #8
0
        public ValueTask SetAsync(ThrottlerItem throttlerItem, Counter counter, TimeSpan?expirationTime, CancellationToken cancellationToken)
        {
            var options = new MemoryCacheEntryOptions
            {
                Priority = CacheItemPriority.NeverRemove
            };

            if (expirationTime.HasValue)
            {
                options.SetAbsoluteExpiration(expirationTime.Value);
            }

            var key = GenerateThrottlerItemKey(throttlerItem);

            _cache.Set(key, counter, options);

            return(new ValueTask());
        }
Пример #9
0
        public async Task Remove()
        {
            var store = CreateCounterStore();

            var timestamp = DateTime.Now;

            var throttleRule = new ThrottleRule {
                Quota = 10, TimeWindow = TimeSpan.FromSeconds(10)
            };
            var throttlerItem = new ThrottlerItem(throttleRule, "policy", "scope", "endpoint");

            await store.SetAsync(throttlerItem, new Counter(timestamp, 12), null, CancellationToken.None);

            await store.RemoveAsync(throttlerItem, CancellationToken.None);

            var counter = await store.GetAsync(throttlerItem, CancellationToken.None);

            Assert.False(counter.HasValue);
        }
Пример #10
0
 public virtual string GenerateThrottlerItemKey(ThrottlerItem throttlerItem)
 {
     return(throttlerItem.GenerateCounterKey("Throttler"));
 }