示例#1
0
        private static Func <NancyContext, Response> CheckRequestCount(LeakyBucketRateLimiterConfiguration configuration)
        {
            return(context =>
            {
                var identifier = configuration.ClientIdentifierFunc(context);
                var store = configuration.RequestStore;
                var config = new LeakyBucketContainerConfiguration
                {
                    RefreshRate = configuration.RefreshRate,
                    Limit = configuration.MaxNumberOfRequests
                };
                var container = new LeakyBucketContainer(store, config);

                return container.RequestsRemaining(identifier) > 0
                    ? null
                    : new Response().WithStatusCode(HttpStatusCode.TooManyRequests);
            });
        }
示例#2
0
        public LeakyBucketContainer(
            IRequestStore requestStore,
            LeakyBucketContainerConfiguration configuration,
            ISystemClock systemClock = null)
        {
            if (requestStore == null)
            {
                throw new ArgumentNullException(nameof(requestStore));
            }

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            _requestStore  = requestStore;
            _configuration = configuration;
            _systemClock   = systemClock ?? new SystemClock();
        }