ConcurrencyLimiter polishing and cleanup (#12127)

* renames and cleanup
This commit is contained in:
Dylan Dmitri Gray 2019-07-13 08:08:21 -07:00 committed by GitHub
parent 87a92e52c8
commit a5411de678
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 167 additions and 186 deletions

View File

@ -73,6 +73,7 @@
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Authorization" ProjectPath="$(RepoRoot)src\Security\Authorization\Core\src\Microsoft.AspNetCore.Authorization.csproj" RefProjectPath="$(RepoRoot)src\Security\Authorization\Core\ref\Microsoft.AspNetCore.Authorization.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Authorization.Policy" ProjectPath="$(RepoRoot)src\Security\Authorization\Policy\src\Microsoft.AspNetCore.Authorization.Policy.csproj" RefProjectPath="$(RepoRoot)src\Security\Authorization\Policy\ref\Microsoft.AspNetCore.Authorization.Policy.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.CookiePolicy" ProjectPath="$(RepoRoot)src\Security\CookiePolicy\src\Microsoft.AspNetCore.CookiePolicy.csproj" RefProjectPath="$(RepoRoot)src\Security\CookiePolicy\ref\Microsoft.AspNetCore.CookiePolicy.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.ConcurrencyLimiter" ProjectPath="$(RepoRoot)src\Middleware\ConcurrencyLimiter\src\Microsoft.AspNetCore.ConcurrencyLimiter.csproj" RefProjectPath="$(RepoRoot)src\Middleware\ConcurrencyLimiter\ref\Microsoft.AspNetCore.ConcurrencyLimiter.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Cors" ProjectPath="$(RepoRoot)src\Middleware\CORS\src\Microsoft.AspNetCore.Cors.csproj" RefProjectPath="$(RepoRoot)src\Middleware\CORS\ref\Microsoft.AspNetCore.Cors.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Diagnostics.Abstractions" ProjectPath="$(RepoRoot)src\Middleware\Diagnostics.Abstractions\src\Microsoft.AspNetCore.Diagnostics.Abstractions.csproj" RefProjectPath="$(RepoRoot)src\Middleware\Diagnostics.Abstractions\ref\Microsoft.AspNetCore.Diagnostics.Abstractions.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" ProjectPath="$(RepoRoot)src\Middleware\Diagnostics.EntityFrameworkCore\src\Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.csproj" RefProjectPath="$(RepoRoot)src\Middleware\Diagnostics.EntityFrameworkCore\ref\Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.csproj" />
@ -87,7 +88,6 @@
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Localization" ProjectPath="$(RepoRoot)src\Middleware\Localization\src\Microsoft.AspNetCore.Localization.csproj" RefProjectPath="$(RepoRoot)src\Middleware\Localization\ref\Microsoft.AspNetCore.Localization.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.MiddlewareAnalysis" ProjectPath="$(RepoRoot)src\Middleware\MiddlewareAnalysis\src\Microsoft.AspNetCore.MiddlewareAnalysis.csproj" RefProjectPath="$(RepoRoot)src\Middleware\MiddlewareAnalysis\ref\Microsoft.AspNetCore.MiddlewareAnalysis.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.NodeServices" ProjectPath="$(RepoRoot)src\Middleware\NodeServices\src\Microsoft.AspNetCore.NodeServices.csproj" RefProjectPath="$(RepoRoot)src\Middleware\NodeServices\ref\Microsoft.AspNetCore.NodeServices.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.RequestThrottling" ProjectPath="$(RepoRoot)src\Middleware\RequestThrottling\src\Microsoft.AspNetCore.RequestThrottling.csproj" RefProjectPath="$(RepoRoot)src\Middleware\RequestThrottling\ref\Microsoft.AspNetCore.RequestThrottling.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.ResponseCaching.Abstractions" ProjectPath="$(RepoRoot)src\Middleware\ResponseCaching.Abstractions\src\Microsoft.AspNetCore.ResponseCaching.Abstractions.csproj" RefProjectPath="$(RepoRoot)src\Middleware\ResponseCaching.Abstractions\ref\Microsoft.AspNetCore.ResponseCaching.Abstractions.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.ResponseCaching" ProjectPath="$(RepoRoot)src\Middleware\ResponseCaching\src\Microsoft.AspNetCore.ResponseCaching.csproj" RefProjectPath="$(RepoRoot)src\Middleware\ResponseCaching\ref\Microsoft.AspNetCore.ResponseCaching.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.ResponseCompression" ProjectPath="$(RepoRoot)src\Middleware\ResponseCompression\src\Microsoft.AspNetCore.ResponseCompression.csproj" RefProjectPath="$(RepoRoot)src\Middleware\ResponseCompression\ref\Microsoft.AspNetCore.ResponseCompression.csproj" />

View File

@ -1,6 +1,6 @@
{
"solution": {
"path": "..\\Middleware.sln",
"path": "C:\\Users\\t-dygra\\Desktop\\AspNetCore\\src\\Middleware\\Middleware.sln",
"projects": [
"..\\Hosting\\Abstractions\\src\\Microsoft.AspNetCore.Hosting.Abstractions.csproj",
"..\\Hosting\\Hosting\\src\\Microsoft.AspNetCore.Hosting.csproj",
@ -15,11 +15,11 @@
"..\\Servers\\Kestrel\\Transport.Sockets\\src\\Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.csproj",
"..\\http\\Headers\\src\\Microsoft.Net.Http.Headers.csproj",
"..\\http\\http\\src\\Microsoft.AspNetCore.Http.csproj",
"HttpsPolicy\\src\\Microsoft.AspNetCore.HttpsPolicy.csproj",
"RequestThrottling\\sample\\RequestThrottlingSample.csproj",
"RequestThrottling\\src\\Microsoft.AspNetCore.RequestThrottling.csproj",
"RequestThrottling\\test\\Microsoft.AspNetCore.RequestThrottling.Tests.csproj",
"RequestThrottling\\perf\\Microbenchmarks\\Microsoft.AspNetCore.RequestThrottling.Microbenchmarks.csproj"
"ConcurrencyLimiter\\perf\\Microbenchmarks\\Microsoft.AspNetCore.ConcurrencyLimiter.Microbenchmarks.csproj",
"ConcurrencyLimiter\\sample\\ConcurrencyLimiterSample.csproj",
"ConcurrencyLimiter\\src\\Microsoft.AspNetCore.ConcurrencyLimiter.csproj",
"ConcurrencyLimiter\\test\\Microsoft.AspNetCore.ConcurrencyLimiter.Tests.csproj",
"HttpsPolicy\\src\\Microsoft.AspNetCore.HttpsPolicy.csproj"
]
}
}

View File

@ -3,16 +3,16 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<!--<StartupObject>Microsoft.AspNetCore.RequestThrottling.Microbenchmarks.Test</StartupObject>-->
<!--<StartupObject>Microsoft.AspNetCore.ConcurrencyLimiter.Microbenchmarks.Test</StartupObject>-->
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\test\Microsoft.AspNetCore.RequestThrottling.Tests.csproj" />
<ProjectReference Include="..\..\test\Microsoft.AspNetCore.ConcurrencyLimiter.Tests.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="BenchmarkDotNet" />
<Reference Include="Microsoft.AspNetCore.BenchmarkRunner.Sources" />
<Reference Include="Microsoft.AspNetCore.RequestThrottling" />
<Reference Include="Microsoft.AspNetCore.ConcurrencyLimiter" />
</ItemGroup>
</Project>

View File

@ -5,16 +5,16 @@ using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.RequestThrottling.Tests;
using Microsoft.AspNetCore.ConcurrencyLimiter.Tests;
namespace Microsoft.AspNetCore.RequestThrottling.Microbenchmarks
namespace Microsoft.AspNetCore.ConcurrencyLimiter.Microbenchmarks
{
public class QueueEmptyOverhead
{
private const int _numRequests = 20000;
private RequestThrottlingMiddleware _middlewareFIFO;
private RequestThrottlingMiddleware _middlewareLIFO;
private ConcurrencyLimiterMiddleware _middlewareFIFO;
private ConcurrencyLimiterMiddleware _middlewareLIFO;
private RequestDelegate _restOfServer;
[GlobalSetup]

View File

@ -5,9 +5,9 @@ using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.RequestThrottling.Tests;
using Microsoft.AspNetCore.ConcurrencyLimiter.Tests;
namespace Microsoft.AspNetCore.RequestThrottling.Microbenchmarks
namespace Microsoft.AspNetCore.ConcurrencyLimiter.Microbenchmarks
{
public class QueueFullOverhead
{
@ -15,8 +15,8 @@ namespace Microsoft.AspNetCore.RequestThrottling.Microbenchmarks
private int _requestCount = 0;
private ManualResetEventSlim _mres = new ManualResetEventSlim();
private RequestThrottlingMiddleware _middleware_FIFO;
private RequestThrottlingMiddleware _middleware_LIFO;
private ConcurrencyLimiterMiddleware _middleware_FIFO;
private ConcurrencyLimiterMiddleware _middleware_LIFO;
[Params(8)]
public int MaxConcurrentRequests;

View File

@ -4,7 +4,7 @@
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<Compile Include="Microsoft.AspNetCore.RequestThrottling.netcoreapp3.0.cs" />
<Compile Include="Microsoft.AspNetCore.ConcurrencyLimiter.netcoreapp3.0.cs" />
<Reference Include="Microsoft.AspNetCore.Http.Abstractions" />
<Reference Include="Microsoft.Extensions.Logging.Abstractions" />
<Reference Include="Microsoft.Extensions.Options" />

View File

@ -3,13 +3,24 @@
namespace Microsoft.AspNetCore.Builder
{
public static partial class RequestThrottlingExtensions
public static partial class ConcurrencyLimiterExtensions
{
public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseRequestThrottling(this Microsoft.AspNetCore.Builder.IApplicationBuilder app) { throw null; }
public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseConcurrencyLimiter(this Microsoft.AspNetCore.Builder.IApplicationBuilder app) { throw null; }
}
}
namespace Microsoft.AspNetCore.RequestThrottling
namespace Microsoft.AspNetCore.ConcurrencyLimiter
{
public partial class ConcurrencyLimiterMiddleware
{
public ConcurrencyLimiterMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, Microsoft.AspNetCore.ConcurrencyLimiter.IQueuePolicy queue, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.ConcurrencyLimiter.ConcurrencyLimiterOptions> options) { }
[System.Diagnostics.DebuggerStepThroughAttribute]
public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; }
}
public partial class ConcurrencyLimiterOptions
{
public ConcurrencyLimiterOptions() { }
public Microsoft.AspNetCore.Http.RequestDelegate OnRejected { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
}
public partial interface IQueuePolicy
{
void OnExit();
@ -21,23 +32,12 @@ namespace Microsoft.AspNetCore.RequestThrottling
public int MaxConcurrentRequests { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public int RequestQueueLimit { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
}
public partial class RequestThrottlingMiddleware
{
public RequestThrottlingMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, Microsoft.AspNetCore.RequestThrottling.IQueuePolicy queue, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.RequestThrottling.RequestThrottlingOptions> options) { }
[System.Diagnostics.DebuggerStepThroughAttribute]
public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; }
}
public partial class RequestThrottlingOptions
{
public RequestThrottlingOptions() { }
public Microsoft.AspNetCore.Http.RequestDelegate OnRejected { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
}
}
namespace Microsoft.Extensions.DependencyInjection
{
public static partial class QueuePolicyServiceCollectionExtensions
{
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddStackQueue(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.RequestThrottling.QueuePolicyOptions> configure) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddTailDropQueue(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.RequestThrottling.QueuePolicyOptions> configure) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddStackQueue(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.ConcurrencyLimiter.QueuePolicyOptions> configure) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddTailDropQueue(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.AspNetCore.ConcurrencyLimiter.QueuePolicyOptions> configure) { throw null; }
}
}

View File

@ -6,13 +6,13 @@
<ItemGroup Condition="'$(BenchmarksTargetFramework)' == ''">
<Reference Include="Microsoft.Extensions.Logging.Console" />
<Reference Include="Microsoft.AspNetCore.RequestThrottling" />
<Reference Include="Microsoft.AspNetCore.ConcurrencyLimiter" />
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
<Reference Include="Microsoft.Extensions.Configuration.CommandLine" />
</ItemGroup>
<ItemGroup Condition="'$(BenchmarksTargetFramework)' != ''">
<PackageReference Include="Microsoft.AspNetCore.RequestThrottling" Version="$(MicrosoftAspNetCoreAppPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.ConcurrencyLimiter" Version="$(MicrosoftAspNetCoreAppPackageVersion)" />
<FrameworkReference Update="Microsoft.AspNetCore.App" RuntimeFrameworkVersion="$(MicrosoftAspNetCoreAppPackageVersion)" />
<FrameworkReference Update="Microsoft.NETCore.App" RuntimeFrameworkVersion="$(MicrosoftNETCoreAppPackageVersion)" />

View File

@ -0,0 +1,47 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace ConcurrencyLimiterSample
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddStackQueue((options) => {
options.MaxConcurrentRequests = Environment.ProcessorCount;
options.RequestQueueLimit = 50;
});
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseConcurrencyLimiter();
app.Run(async context =>
{
var delay = 100;
Task.Delay(delay).Wait();
await context.Response.WriteAsync("Hello World!");
});
}
public static void Main(string[] args)
{
new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.Build()
.Run();
}
}
}

View File

@ -8,11 +8,11 @@ using System.Text;
using System.Threading;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.RequestThrottling
namespace Microsoft.AspNetCore.ConcurrencyLimiter
{
internal sealed class RequestThrottlingEventSource : EventSource
internal sealed class ConcurrencyLimiterEventSource : EventSource
{
public static readonly RequestThrottlingEventSource Log = new RequestThrottlingEventSource();
public static readonly ConcurrencyLimiterEventSource Log = new ConcurrencyLimiterEventSource();
private static readonly QueueFrame CachedNonTimerResult = new QueueFrame(timer: null, parent: Log);
private PollingCounter _rejectedRequestsCounter;
@ -22,13 +22,13 @@ namespace Microsoft.AspNetCore.RequestThrottling
private long _rejectedRequests;
private int _queueLength;
internal RequestThrottlingEventSource()
: base("Microsoft.AspNetCore.RequestThrottling")
internal ConcurrencyLimiterEventSource()
: base("Microsoft.AspNetCore.ConcurrencyLimiter")
{
}
// Used for testing
internal RequestThrottlingEventSource(string eventSourceName)
internal ConcurrencyLimiterEventSource(string eventSourceName)
: base(eventSourceName)
{
}
@ -65,9 +65,9 @@ namespace Microsoft.AspNetCore.RequestThrottling
internal struct QueueFrame : IDisposable
{
private ValueStopwatch? _timer;
private RequestThrottlingEventSource _parent;
private ConcurrencyLimiterEventSource _parent;
public QueueFrame(ValueStopwatch? timer, RequestThrottlingEventSource parent)
public QueueFrame(ValueStopwatch? timer, ConcurrencyLimiterEventSource parent)
{
_timer = timer;
_parent = parent;

View File

@ -2,28 +2,28 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.RequestThrottling;
using Microsoft.AspNetCore.ConcurrencyLimiter;
namespace Microsoft.AspNetCore.Builder
{
/// <summary>
/// Extension methods for adding the <see cref="RequestThrottlingMiddleware"/> to an application.
/// Extension methods for adding the <see cref="ConcurrencyLimiterMiddleware"/> to an application.
/// </summary>
public static class RequestThrottlingExtensions
public static class ConcurrencyLimiterExtensions
{
/// <summary>
/// Adds the <see cref="RequestThrottlingMiddleware"/> to limit the number of concurrently-executing requests.
/// Adds the <see cref="ConcurrencyLimiterMiddleware"/> to limit the number of concurrently-executing requests.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseRequestThrottling(this IApplicationBuilder app)
public static IApplicationBuilder UseConcurrencyLimiter(this IApplicationBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseMiddleware<RequestThrottlingMiddleware>();
return app.UseMiddleware<ConcurrencyLimiterMiddleware>();
}
}
}

View File

@ -7,12 +7,12 @@ using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.RequestThrottling
namespace Microsoft.AspNetCore.ConcurrencyLimiter
{
/// <summary>
/// Limits the number of concurrent requests allowed in the application.
/// </summary>
public class RequestThrottlingMiddleware
public class ConcurrencyLimiterMiddleware
{
private readonly IQueuePolicy _queuePolicy;
private readonly RequestDelegate _next;
@ -20,13 +20,13 @@ namespace Microsoft.AspNetCore.RequestThrottling
private readonly ILogger _logger;
/// <summary>
/// Creates a new <see cref="RequestThrottlingMiddleware"/>.
/// Creates a new <see cref="ConcurrencyLimiterMiddleware"/>.
/// </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="queue">The queueing strategy to use for the server.</param>
/// <param name="options">The options for the middleware, currently containing the 'OnRejected' callback.</param>
public RequestThrottlingMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IQueuePolicy queue, IOptions<RequestThrottlingOptions> options)
public ConcurrencyLimiterMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IQueuePolicy queue, IOptions<ConcurrencyLimiterOptions> options)
{
if (options.Value.OnRejected == null)
{
@ -34,7 +34,7 @@ namespace Microsoft.AspNetCore.RequestThrottling
}
_next = next;
_logger = loggerFactory.CreateLogger<RequestThrottlingMiddleware>();
_logger = loggerFactory.CreateLogger<ConcurrencyLimiterMiddleware>();
_onRejected = options.Value.OnRejected;
_queuePolicy = queue;
}
@ -50,11 +50,11 @@ namespace Microsoft.AspNetCore.RequestThrottling
if (waitInQueueTask.IsCompleted)
{
RequestThrottlingEventSource.Log.QueueSkipped();
ConcurrencyLimiterEventSource.Log.QueueSkipped();
}
else
{
using (RequestThrottlingEventSource.Log.QueueTimer())
using (ConcurrencyLimiterEventSource.Log.QueueTimer())
{
await waitInQueueTask;
}
@ -73,14 +73,14 @@ namespace Microsoft.AspNetCore.RequestThrottling
}
else
{
RequestThrottlingEventSource.Log.RequestRejected();
RequestThrottlingLog.RequestRejectedQueueFull(_logger);
ConcurrencyLimiterEventSource.Log.RequestRejected();
ConcurrencyLimiterLog.RequestRejectedQueueFull(_logger);
context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
await _onRejected(context);
}
}
private static class RequestThrottlingLog
private static class ConcurrencyLimiterLog
{
private static readonly Action<ILogger, int, Exception> _requestEnqueued =
LoggerMessage.Define<int>(LogLevel.Debug, new EventId(1, "RequestEnqueued"), "MaxConcurrentRequests limit reached, request has been queued. Current active requests: {ActiveRequests}.");

View File

@ -4,12 +4,12 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.RequestThrottling
namespace Microsoft.AspNetCore.ConcurrencyLimiter
{
/// <summary>
/// Specifies options for the <see cref="RequestThrottlingMiddleware"/>.
/// Specifies options for the <see cref="ConcurrencyLimiterMiddleware"/>.
/// </summary>
public class RequestThrottlingOptions
public class ConcurrencyLimiterOptions
{
/// <summary>
/// A <see cref="RequestDelegate"/> that handles requests rejected by this middleware.

View File

@ -0,0 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.ConcurrencyLimiter.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]

View File

@ -6,9 +6,9 @@ using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.RequestThrottling
namespace Microsoft.AspNetCore.ConcurrencyLimiter
{
internal class TailDropQueuePolicy : IQueuePolicy, IDisposable
internal class FIFOQueuePolicy : IQueuePolicy, IDisposable
{
private readonly int _maxConcurrentRequests;
private readonly int _requestQueueLimit;
@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.RequestThrottling
private object _totalRequestsLock = new object();
public int TotalRequests { get; private set; }
public TailDropQueuePolicy(IOptions<QueuePolicyOptions> options)
public FIFOQueuePolicy(IOptions<QueuePolicyOptions> options)
{
_maxConcurrentRequests = options.Value.MaxConcurrentRequests;
if (_maxConcurrentRequests <= 0)

View File

@ -4,10 +4,10 @@
using System;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.RequestThrottling
namespace Microsoft.AspNetCore.ConcurrencyLimiter
{
/// <summary>
/// Queueing policies, meant to be used with the <see cref="RequestThrottlingMiddleware"></see>.
/// Queueing policies, meant to be used with the <see cref="ConcurrencyLimiterMiddleware"></see>.
/// </summary>
public interface IQueuePolicy
{

View File

@ -3,9 +3,9 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.RequestThrottling
namespace Microsoft.AspNetCore.ConcurrencyLimiter
{
internal class StackQueuePolicy : IQueuePolicy
internal class LIFOQueuePolicy : IQueuePolicy
{
private readonly List<TaskCompletionSource<bool>> _buffer;
private readonly int _maxQueueCapacity;
@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.RequestThrottling
private int _freeServerSpots;
public StackQueuePolicy(IOptions<QueuePolicyOptions> options)
public LIFOQueuePolicy(IOptions<QueuePolicyOptions> options)
{
_buffer = new List<TaskCompletionSource<bool>>();
_maxQueueCapacity = options.Value.RequestQueueLimit;

View File

@ -2,7 +2,7 @@ using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.AspNetCore.RequestThrottling
namespace Microsoft.AspNetCore.ConcurrencyLimiter
{
/// <summary>
/// Specifies options for the <see cref="IQueuePolicy"/>
@ -19,6 +19,6 @@ namespace Microsoft.AspNetCore.RequestThrottling
/// Maximum number of queued requests before the server starts rejecting connections with '503 Service Unavailible'.
/// Defaults to 5000 queued requests.
/// </summary>
public int RequestQueueLimit { get; set; } = 5000;
public int RequestQueueLimit { get; set; }
}
}

View File

@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.RequestThrottling;
using Microsoft.AspNetCore.ConcurrencyLimiter;
namespace Microsoft.Extensions.DependencyInjection
{
@ -12,7 +12,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static class QueuePolicyServiceCollectionExtensions
{
/// <summary>
/// Tells <see cref="RequestThrottlingMiddleware"/> to use a FIFO queue as its queueing strategy.
/// Tells <see cref="ConcurrencyLimiterMiddleware"/> to use a FIFO queue as its queueing strategy.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <param name="configure">Set the options used by the queue.
@ -21,12 +21,12 @@ namespace Microsoft.Extensions.DependencyInjection
public static IServiceCollection AddTailDropQueue(this IServiceCollection services, Action<QueuePolicyOptions> configure)
{
services.Configure(configure);
services.AddSingleton<IQueuePolicy, TailDropQueuePolicy>();
services.AddSingleton<IQueuePolicy, FIFOQueuePolicy>();
return services;
}
/// <summary>
/// Tells <see cref="RequestThrottlingMiddleware"/> to use a LIFO stack as its queueing strategy.
/// Tells <see cref="ConcurrencyLimiterMiddleware"/> to use a LIFO stack as its queueing strategy.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <param name="configure">Set the options used by the queue.
@ -35,7 +35,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static IServiceCollection AddStackQueue(this IServiceCollection services, Action<QueuePolicyOptions> configure)
{
services.Configure(configure);
services.AddSingleton<IQueuePolicy, StackQueuePolicy>();
services.AddSingleton<IQueuePolicy, LIFOQueuePolicy>();
return services;
}
}

View File

@ -9,17 +9,17 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Internal;
using Xunit;
namespace Microsoft.AspNetCore.RequestThrottling.Tests
namespace Microsoft.AspNetCore.ConcurrencyLimiter.Tests
{
public class RequestThrottlingEventSourceTests
public class ConcurrencyLimiterEventSourceTests
{
[Fact]
public void MatchesNameAndGuid()
{
var eventSource = new RequestThrottlingEventSource();
var eventSource = new ConcurrencyLimiterEventSource();
Assert.Equal("Microsoft.AspNetCore.RequestThrottling", eventSource.Name);
Assert.Equal(Guid.Parse("436f1cb1-8acc-56c0-86ec-e0832bd696ed"), eventSource.Guid);
Assert.Equal("Microsoft.AspNetCore.ConcurrencyLimiter", eventSource.Name);
Assert.Equal(Guid.Parse("a605548a-6963-55cf-f000-99a6013deb01"), eventSource.Guid);
}
[Fact]
@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests
// Arrange
var expectedId = 1;
var eventListener = new TestEventListener(expectedId);
var eventSource = GetRequestThrottlingEventSource();
var eventSource = GetConcurrencyLimiterEventSource();
eventListener.EnableEvents(eventSource, EventLevel.Informational);
// Act
@ -54,7 +54,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests
"requests-rejected",
});
using var eventSource = GetRequestThrottlingEventSource();
using var eventSource = GetConcurrencyLimiterEventSource();
using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
@ -95,7 +95,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests
"requests-rejected",
});
using var eventSource = GetRequestThrottlingEventSource();
using var eventSource = GetConcurrencyLimiterEventSource();
using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));
@ -140,9 +140,9 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests
return false;
}
private static RequestThrottlingEventSource GetRequestThrottlingEventSource()
private static ConcurrencyLimiterEventSource GetConcurrencyLimiterEventSource()
{
return new RequestThrottlingEventSource(Guid.NewGuid().ToString());
return new ConcurrencyLimiterEventSource(Guid.NewGuid().ToString());
}
}
}

View File

@ -12,6 +12,6 @@
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Http" />
<Reference Include="Microsoft.AspNetCore.Hosting" />
<Reference Include="Microsoft.AspNetCore.RequestThrottling" />
<Reference Include="Microsoft.AspNetCore.ConcurrencyLimiter" />
</ItemGroup>
</Project>

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Xunit;
namespace Microsoft.AspNetCore.RequestThrottling.Tests
namespace Microsoft.AspNetCore.ConcurrencyLimiter.Tests
{
public class MiddlewareTests
{

View File

@ -3,14 +3,14 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Xunit;
namespace Microsoft.AspNetCore.RequestThrottling.Tests.PolicyTests
namespace Microsoft.AspNetCore.ConcurrencyLimiter.Tests.PolicyTests
{
public static class StackQueueTests
{
[Fact]
public static void BaseFunctionality()
{
var stack = new StackQueuePolicy(Options.Create(new QueuePolicyOptions {
var stack = new LIFOQueuePolicy(Options.Create(new QueuePolicyOptions {
MaxConcurrentRequests = 0,
RequestQueueLimit = 2,
}));
@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests.PolicyTests
[Fact]
public static void OldestRequestOverwritten()
{
var stack = new StackQueuePolicy(Options.Create(new QueuePolicyOptions {
var stack = new LIFOQueuePolicy(Options.Create(new QueuePolicyOptions {
MaxConcurrentRequests = 0,
RequestQueueLimit = 3,
}));
@ -49,7 +49,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests.PolicyTests
[Fact]
public static void RespectsMaxConcurrency()
{
var stack = new StackQueuePolicy(Options.Create(new QueuePolicyOptions {
var stack = new LIFOQueuePolicy(Options.Create(new QueuePolicyOptions {
MaxConcurrentRequests = 2,
RequestQueueLimit = 2,
}));
@ -67,7 +67,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests.PolicyTests
[Fact]
public static void ExitRequestsPreserveSemaphoreState()
{
var stack = new StackQueuePolicy(Options.Create(new QueuePolicyOptions {
var stack = new LIFOQueuePolicy(Options.Create(new QueuePolicyOptions {
MaxConcurrentRequests = 1,
RequestQueueLimit = 2,
}));
@ -90,7 +90,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests.PolicyTests
[Fact]
public static void StaleRequestsAreProperlyOverwritten()
{
var stack = new StackQueuePolicy(Options.Create(new QueuePolicyOptions
var stack = new LIFOQueuePolicy(Options.Create(new QueuePolicyOptions
{
MaxConcurrentRequests = 0,
RequestQueueLimit = 4,
@ -108,7 +108,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests.PolicyTests
[Fact]
public static async Task OneTryEnterAsyncOneOnExit()
{
var stack = new StackQueuePolicy(Options.Create(new QueuePolicyOptions
var stack = new LIFOQueuePolicy(Options.Create(new QueuePolicyOptions
{
MaxConcurrentRequests = 1,
RequestQueueLimit = 4,

View File

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Internal;
using Xunit;
namespace Microsoft.AspNetCore.RequestThrottling.Tests.PolicyTests
namespace Microsoft.AspNetCore.ConcurrencyLimiter.Tests.PolicyTests
{
public class TailDropTests
{

View File

@ -8,18 +8,18 @@ using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.RequestThrottling.Tests
namespace Microsoft.AspNetCore.ConcurrencyLimiter.Tests
{
public static class TestUtils
{
public static RequestThrottlingMiddleware CreateTestMiddleware(IQueuePolicy queue = null, RequestDelegate onRejected = null, RequestDelegate next = null)
public static ConcurrencyLimiterMiddleware CreateTestMiddleware(IQueuePolicy queue = null, RequestDelegate onRejected = null, RequestDelegate next = null)
{
var options = Options.Create(new RequestThrottlingOptions
var options = Options.Create(new ConcurrencyLimiterOptions
{
OnRejected = onRejected ?? (context => Task.CompletedTask),
});
return new RequestThrottlingMiddleware(
return new ConcurrencyLimiterMiddleware(
next: next ?? (context => Task.CompletedTask),
loggerFactory: NullLoggerFactory.Instance,
queue: queue ?? CreateTailDropQueue(1, 0),
@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests
);
}
public static RequestThrottlingMiddleware CreateTestMiddleware_TailDrop(int maxConcurrentRequests, int requestQueueLimit, RequestDelegate onRejected = null, RequestDelegate next = null)
public static ConcurrencyLimiterMiddleware CreateTestMiddleware_TailDrop(int maxConcurrentRequests, int requestQueueLimit, RequestDelegate onRejected = null, RequestDelegate next = null)
{
return CreateTestMiddleware(
queue: CreateTailDropQueue(maxConcurrentRequests, requestQueueLimit),
@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests
);
}
public static RequestThrottlingMiddleware CreateTestMiddleware_StackPolicy(int maxConcurrentRequests, int requestQueueLimit, RequestDelegate onRejected = null, RequestDelegate next = null)
public static ConcurrencyLimiterMiddleware CreateTestMiddleware_StackPolicy(int maxConcurrentRequests, int requestQueueLimit, RequestDelegate onRejected = null, RequestDelegate next = null)
{
return CreateTestMiddleware(
queue: CreateStackPolicy(maxConcurrentRequests, requestQueueLimit),
@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests
);
}
internal static StackQueuePolicy CreateStackPolicy(int maxConcurrentRequests, int requestsQueuelimit = 100)
internal static LIFOQueuePolicy CreateStackPolicy(int maxConcurrentRequests, int requestsQueuelimit = 100)
{
var options = Options.Create(new QueuePolicyOptions
{
@ -53,10 +53,10 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests
RequestQueueLimit = requestsQueuelimit
});
return new StackQueuePolicy(options);
return new LIFOQueuePolicy(options);
}
internal static TailDropQueuePolicy CreateTailDropQueue(int maxConcurrentRequests, int requestQueueLimit = 100)
internal static FIFOQueuePolicy CreateTailDropQueue(int maxConcurrentRequests, int requestQueueLimit = 100)
{
var options = Options.Create(new QueuePolicyOptions
{
@ -64,7 +64,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests
RequestQueueLimit = requestQueueLimit
});
return new TailDropQueuePolicy(options);
return new FIFOQueuePolicy(options);
}
}

View File

@ -277,19 +277,19 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HeaderPropagationSample", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Server.IIS", "..\Servers\IIS\IIS\src\Microsoft.AspNetCore.Server.IIS.csproj", "{B9BE1823-B555-4AAB-AEBC-C8C3F48C8861}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RequestThrottling", "RequestThrottling", "{8C9AA8A2-9D1F-4450-9F8D-56BAB6F3D343}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ConcurrencyLimiter", "ConcurrencyLimiter", "{8C9AA8A2-9D1F-4450-9F8D-56BAB6F3D343}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RequestThrottlingSample", "RequestThrottling\sample\RequestThrottlingSample.csproj", "{6720919C-0DEA-49E1-90DC-F1883F7919CD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConcurrencyLimiterSample", "ConcurrencyLimiter\sample\ConcurrencyLimiterSample.csproj", "{6720919C-0DEA-49E1-90DC-F1883F7919CD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.RequestThrottling", "RequestThrottling\src\Microsoft.AspNetCore.RequestThrottling.csproj", "{4CE2384D-6B88-4824-ADD1-4183D180FEFF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.ConcurrencyLimiter", "ConcurrencyLimiter\src\Microsoft.AspNetCore.ConcurrencyLimiter.csproj", "{4CE2384D-6B88-4824-ADD1-4183D180FEFF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.RequestThrottling.Tests", "RequestThrottling\test\Microsoft.AspNetCore.RequestThrottling.Tests.csproj", "{353AA2B0-1013-486C-B5BD-9379385CA403}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.ConcurrencyLimiter.Tests", "ConcurrencyLimiter\test\Microsoft.AspNetCore.ConcurrencyLimiter.Tests.csproj", "{353AA2B0-1013-486C-B5BD-9379385CA403}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Metadata", "..\Http\Metadata\src\Microsoft.AspNetCore.Metadata.csproj", "{7E2EA6E2-31FE-418A-9AE4-955A4C708AE7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authorization", "..\Security\Authorization\Core\src\Microsoft.AspNetCore.Authorization.csproj", "{CDDD7C43-5BEB-4E3E-8A59-FCDC83C9FBCF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.RequestThrottling.Microbenchmarks", "RequestThrottling\perf\Microbenchmarks\Microsoft.AspNetCore.RequestThrottling.Microbenchmarks.csproj", "{737B26B4-CFC6-4B44-9070-DD36334E85B3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.ConcurrencyLimiter.Microbenchmarks", "ConcurrencyLimiter\perf\Microbenchmarks\Microsoft.AspNetCore.ConcurrencyLimiter.Microbenchmarks.csproj", "{737B26B4-CFC6-4B44-9070-DD36334E85B3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestDestination", "CORS\test\testassets\TestDestination\TestDestination.csproj", "{DFEB537A-2D35-4C62-8A13-42798DF66A80}"
EndProject

View File

@ -1,66 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace RequestThrottlingSample
{
public class Startup
{
static IConfiguration _config;
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddStackQueue((options) =>
{
options.MaxConcurrentRequests = Math.Max(1, _config.GetValue<int>("maxConcurrent"));
options.RequestQueueLimit = Math.Max(1, _config.GetValue<int>("maxQueue"));
});
services.AddLogging();
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseRequestThrottling();
app.Run(async context =>
{
await context.Response.WriteAsync("Hello Request Throttling! If you rapidly refresh this page, it will 503.");
await Task.Delay(400);
});
}
// Entry point for the application.
public static void Main(string[] args)
{
_config = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory()) // for the cert file
.ConfigureLogging(factory =>
{
factory.SetMinimumLevel(LogLevel.Debug);
factory.AddConsole();
})
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}

View File

@ -1,6 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.RequestThrottling.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]