/// <summary>
        /// Creates a new <see cref="RequestThrottlingMiddleware"/>.
        /// </summary>
        /// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
        /// <param name="loggerFactory">The <see cref="ILoggerFactory"/> used for logging.</param>
        /// <param name="options">The <see cref="RequestThrottlingOptions"/> containing the initialization parameters.</param>
        public RequestThrottlingMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IOptions <RequestThrottlingOptions> options)
        {
            _requestThrottlingOptions = options.Value;

            if (_requestThrottlingOptions.MaxConcurrentRequests == null)
            {
                throw new ArgumentException("The value of 'options.MaxConcurrentRequests' must be specified.", nameof(options));
            }
            if (_requestThrottlingOptions.MaxConcurrentRequests < 0)
            {
                throw new ArgumentException("The value of 'options.MaxConcurrentRequests' must be a positive integer.", nameof(options));
            }
            if (_requestThrottlingOptions.RequestQueueLimit < 0)
            {
                throw new ArgumentException("The value of 'options.RequestQueueLimit' must be a positive integer.", nameof(options));
            }

            if (_requestThrottlingOptions.OnRejected == null)
            {
                throw new ArgumentException("The value of 'options.OnRejected' must not be null.", nameof(options));
            }

            _next         = next;
            _logger       = loggerFactory.CreateLogger <RequestThrottlingMiddleware>();
            _requestQueue = new RequestQueue(
                _requestThrottlingOptions.MaxConcurrentRequests.Value,
                _requestThrottlingOptions.RequestQueueLimit);
        }
Пример #2
0
        /// <summary>
        /// Creates a new <see cref="RequestThrottlingMiddleware"/>.
        /// </summary>
        /// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
        /// <param name="loggerFactory">The <see cref="ILoggerFactory"/> used for logging.</param>
        /// <param name="options">The <see cref="RequestThrottlingOptions"/> containing the initialization parameters.</param>
        public RequestThrottlingMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IOptions <RequestThrottlingOptions> options)
        {
            _requestThrottlingOptions = options.Value;

            if (_requestThrottlingOptions.MaxConcurrentRequests == null)
            {
                throw new ArgumentException("The value of 'options.MaxConcurrentRequests' must be specified.", nameof(options));
            }
            if (_requestThrottlingOptions.MaxConcurrentRequests <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(options), "The value of `options.MaxConcurrentRequests` must be a positive integer.");
            }
            if (_requestThrottlingOptions.RequestQueueLimit < 0)
            {
                throw new ArgumentException("The value of 'options.RequestQueueLimit' must be a positive integer.", nameof(options));
            }

            if (_requestThrottlingOptions.OnRejected == null)
            {
                throw new ArgumentException("The value of 'options.OnRejected' must not be null.", nameof(options));
            }

            _next   = next;
            _logger = loggerFactory.CreateLogger <RequestThrottlingMiddleware>();

            if (_requestThrottlingOptions.ServerAlwaysBlocks)
            {
                // note: this option for testing only. Blocks all requests from entering the server.
                _requestQueue = new TailDrop(0, _requestThrottlingOptions.RequestQueueLimit);
            }
            else
            {
                _requestQueue = new TailDrop(_requestThrottlingOptions.MaxConcurrentRequests.Value, _requestThrottlingOptions.RequestQueueLimit);
            }
        }
Пример #3
0
        /// <summary>
        /// Creates a new <see cref="RequestThrottlingMiddleware"/>.
        /// </summary>
        /// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
        /// <param name="loggerFactory">The <see cref="ILoggerFactory"/> used for logging.</param>
        /// <param name="options">The <see cref="RequestThrottlingOptions"/> containing the initialization parameters.</param>
        public RequestThrottlingMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IOptions <RequestThrottlingOptions> options)
        {
            if (options.Value.MaxConcurrentRequests == null)
            {
                throw new ArgumentException("The value of 'options.MaxConcurrentRequests' must be specified.", nameof(options));
            }

            _next         = next;
            _logger       = loggerFactory.CreateLogger <RequestThrottlingMiddleware>();
            _options      = options.Value;
            _requestQueue = new RequestQueue(_options.MaxConcurrentRequests.Value);
        }