/// <summary> /// Invokes the logic of the middleware. /// </summary> /// <param name="context">The <see cref="HttpContext"/>.</param> /// <returns>A <see cref="Task"/> that completes when the request leaves.</returns> public async Task Invoke(HttpContext context) { Interlocked.Increment(ref _queuedRequests); var success = false; try { success = await _queuePolicy.TryEnterAsync(); } finally { Interlocked.Decrement(ref _queuedRequests); } if (success) { try { await _next(context); } finally { _queuePolicy.OnExit(); } } else { RequestThrottlingLog.RequestRejectedQueueFull(_logger); context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable; await _onRejected(context); } }
/// <summary> /// Invokes the logic of the middleware. /// </summary> /// <param name="context">The <see cref="HttpContext"/>.</param> /// <returns>A <see cref="Task"/> that completes when the request leaves.</returns> public async Task Invoke(HttpContext context) { var waitInQueueTask = _queuePolicy.TryEnterAsync(); if (waitInQueueTask.IsCompleted) { RequestThrottlingEventSource.Log.QueueSkipped(); } else { using (RequestThrottlingEventSource.Log.QueueTimer()) { await waitInQueueTask; } } if (waitInQueueTask.Result) { try { await _next(context); } finally { _queuePolicy.OnExit(); } } else { RequestThrottlingEventSource.Log.RequestRejected(); RequestThrottlingLog.RequestRejectedQueueFull(_logger); context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable; await _onRejected(context); } }
/// <summary> /// Invokes the logic of the middleware. /// </summary> /// <param name="context">The <see cref="HttpContext"/>.</param> /// <returns>A <see cref="Task"/> that completes when the request leaves.</returns> public async Task Invoke(HttpContext context) { var waitInQueueTask = _requestQueue.TryEnterQueueAsync(); if (waitInQueueTask.IsCompletedSuccessfully && !waitInQueueTask.Result) { RequestThrottlingLog.RequestRejectedQueueFull(_logger); context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable; return; } else if (!waitInQueueTask.IsCompletedSuccessfully) { RequestThrottlingLog.RequestEnqueued(_logger, ActiveRequestCount); var result = await waitInQueueTask; RequestThrottlingLog.RequestDequeued(_logger, ActiveRequestCount); Debug.Assert(result); } else { RequestThrottlingLog.RequestRunImmediately(_logger, ActiveRequestCount); } try { await _next(context); } finally { _requestQueue.Release(); } }
/// <summary> /// Invokes the logic of the middleware. /// </summary> /// <param name="context">The <see cref="HttpContext"/>.</param> /// <returns>A <see cref="Task"/> that completes when the request leaves.</returns> public async Task Invoke(HttpContext context) { var waitInQueueTask = _requestQueue.EnterQueue(); if (waitInQueueTask.IsCompletedSuccessfully) { RequestThrottlingLog.RequestRunImmediately(_logger); } else { RequestThrottlingLog.RequestEnqueued(_logger, WaitingRequests); await waitInQueueTask; RequestThrottlingLog.RequestDequeued(_logger, WaitingRequests); } try { await _next(context); } finally { _requestQueue.Release(); } }