Skip to content

Improve performance for QueuePolicy in ConcurrencyLimiter #13947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions src/Middleware/ConcurrencyLimiter/src/QueuePolicies/QueuePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,57 @@ namespace Microsoft.AspNetCore.ConcurrencyLimiter
{
internal class QueuePolicy : IQueuePolicy, IDisposable
{
private readonly int _maxConcurrentRequests;
private readonly int _requestQueueLimit;
private readonly int _maxTotalRequest;
private readonly SemaphoreSlim _serverSemaphore;

private object _totalRequestsLock = new object();

public int TotalRequests { get; private set; }

public QueuePolicy(IOptions<QueuePolicyOptions> options)
{
_maxConcurrentRequests = options.Value.MaxConcurrentRequests;
if (_maxConcurrentRequests <= 0)
var queuePolicyOptions = options.Value;

var maxConcurrentRequests = queuePolicyOptions.MaxConcurrentRequests;
if (maxConcurrentRequests <= 0)
{
throw new ArgumentException(nameof(_maxConcurrentRequests), "MaxConcurrentRequests must be a positive integer.");
throw new ArgumentException(nameof(maxConcurrentRequests), "MaxConcurrentRequests must be a positive integer.");
}

_requestQueueLimit = options.Value.RequestQueueLimit;
if (_requestQueueLimit < 0)
var requestQueueLimit = queuePolicyOptions.RequestQueueLimit;
if (requestQueueLimit < 0)
{
throw new ArgumentException(nameof(_requestQueueLimit), "The RequestQueueLimit cannot be a negative number.");
throw new ArgumentException(nameof(requestQueueLimit), "The RequestQueueLimit cannot be a negative number.");
}

_serverSemaphore = new SemaphoreSlim(_maxConcurrentRequests);
_serverSemaphore = new SemaphoreSlim(maxConcurrentRequests);

_maxTotalRequest = maxConcurrentRequests + requestQueueLimit;
}

public async ValueTask<bool> TryEnterAsync()
public ValueTask<bool> TryEnterAsync()
{
// a return value of 'false' indicates that the request is rejected
// a return value of 'true' indicates that the request may proceed
// _serverSemaphore.Release is *not* called in this method, it is called externally when requests leave the server

lock (_totalRequestsLock)
{
if (TotalRequests >= _requestQueueLimit + _maxConcurrentRequests)
if (TotalRequests >= _maxTotalRequest)
{
return false;
return new ValueTask<bool>(false);
}

TotalRequests++;
}

await _serverSemaphore.WaitAsync();
Task task = _serverSemaphore.WaitAsync();
if (task.IsCompletedSuccessfully)
{
return new ValueTask<bool>(true);
}

return true;
return SemaphoreAwaited(task);
}

public void OnExit()
Expand All @@ -69,5 +77,12 @@ public void Dispose()
{
_serverSemaphore.Dispose();
}

private async ValueTask<bool> SemaphoreAwaited(Task task)
{
await task;

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ internal class StackPolicy : IQueuePolicy

private readonly object _bufferLock = new Object();

private readonly static ValueTask<bool> _trueTask = new ValueTask<bool>(true);

private int _freeServerSpots;

public StackPolicy(IOptions<QueuePolicyOptions> options)
Expand All @@ -40,7 +38,7 @@ public ValueTask<bool> TryEnterAsync()
if (_freeServerSpots > 0)
{
_freeServerSpots--;
return _trueTask;
return new ValueTask<bool>(true);
}

// if queue is full, cancel oldest request
Expand Down