diff --git a/src/Middleware/RequestThrottling/sample/Startup.cs b/src/Middleware/RequestThrottling/sample/Startup.cs index 5768d045e6..a9bc74e319 100644 --- a/src/Middleware/RequestThrottling/sample/Startup.cs +++ b/src/Middleware/RequestThrottling/sample/Startup.cs @@ -12,8 +12,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -namespace RequestQueueSample -{ +namespace RequestThrottlingSample +{ public class Startup { // This method gets called by the runtime. Use this method to add services to the container. diff --git a/src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj b/src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj index 9f410e75c5..c9b165fa16 100644 --- a/src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj +++ b/src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj @@ -1,11 +1,10 @@ - + ASP.NET Core middleware for queuing incoming HTTP requests, to avoid threadpool starvation. netcoreapp3.0 - $(NoWarn);CS1591 - true true + aspnetcore;queue;queuing $(WarningsNotAsErrors);CS1591 diff --git a/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs b/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs index aff2f66919..7bfb6d377a 100644 --- a/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs +++ b/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs @@ -4,30 +4,30 @@ using System.Text; using System.Threading; using System.Threading.Tasks; -namespace Microsoft.AspNetCore.RequestQueue +namespace Microsoft.AspNetCore.RequestThrottling { internal class SemaphoreWrapper { - private static SemaphoreSlim semaphore; + private SemaphoreSlim _semaphore; - public SemaphoreWrapper(int qlength) + public SemaphoreWrapper(int queueLength) { - semaphore = new SemaphoreSlim(qlength); + _semaphore = new SemaphoreSlim(queueLength); } public Task EnterQueue() { - return semaphore.WaitAsync(); + return _semaphore.WaitAsync(); } public void LeaveQueue() { - semaphore.Release(); + _semaphore.Release(); } - public int SpotsLeft + public int Count { - get => semaphore.CurrentCount; + get => _semaphore.CurrentCount; } } } diff --git a/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs b/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs index 33bf6ea861..d6c990bf1f 100644 --- a/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs +++ b/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs @@ -8,25 +8,25 @@ using System; using System.Runtime.CompilerServices; using Microsoft.AspNetCore.Testing; -namespace Microsoft.AspNetCore.RequestQueue.Tests +namespace Microsoft.AspNetCore.RequestThrottling.Tests { public class SemaphoreWrapperTests { [Fact] - public async Task TestBehavior() + public async Task TracksQueueLength() { var s = new SemaphoreWrapper(1); - Assert.Equal(1, s.SpotsLeft); + Assert.Equal(1, s.Count); await s.EnterQueue(); - Assert.Equal(0, s.SpotsLeft); + Assert.Equal(0, s.Count); s.LeaveQueue(); - Assert.Equal(1, s.SpotsLeft); + Assert.Equal(1, s.Count); } [Fact] - public void TestQueueLength() + public void DoesNotWaitIfSpaceAvailible() { var s = new SemaphoreWrapper(2); @@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.RequestQueue.Tests } [Fact] - public async Task TestWaiting() + public async Task WaitsIfNoSpaceAvailible() { var s = new SemaphoreWrapper(1); await s.EnterQueue(); @@ -50,7 +50,17 @@ namespace Microsoft.AspNetCore.RequestQueue.Tests Assert.False(waitingTask.IsCompleted); s.LeaveQueue(); - await waitingTask.TimeoutAfter(TimeSpan.FromSeconds(1)); + await waitingTask.TimeoutAfter(TimeSpan.FromSeconds(30)); + } + + [Fact] + public async Task IsEncapsulated() + { + var s1 = new SemaphoreWrapper(1); + var s2 = new SemaphoreWrapper(1); + + await s1.EnterQueue(); + await s2.EnterQueue().TimeoutAfter(TimeSpan.FromSeconds(30)); } } }