cleanup from PR feedback
This commit is contained in:
parent
61f028ad32
commit
ac305849e3
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<Description>ASP.NET Core middleware for queuing incoming HTTP requests, to avoid threadpool starvation.</Description>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
<NoWarn>$(NoWarn);CS1591</NoWarn>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<PackageTags>aspnetcore;queue;queuing</PackageTags>
|
||||
<WarningsNotAsErrors>$(WarningsNotAsErrors);CS1591</WarningsNotAsErrors>
|
||||
</PropertyGroup>
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue