コード例 #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 virtual string GenerateThrottlerItemKey(ThrottlerItem throttlerItem)
 {
     return(throttlerItem.GenerateCounterKey("Throttler"));
 }