From 595660984652ef857bbdcc9f1d8a8921b88601d7 Mon Sep 17 00:00:00 2001 From: Dylan Dmitri Gray Date: Thu, 16 May 2019 16:12:18 -0700 Subject: [PATCH 01/11] start of request queue --- eng/ProjectReferences.props | 1 + .../Microsoft.AspNetCore.RequestQueue.csproj | 14 +++++ ...t.AspNetCore.RequestQueue.netcoreapp3.0.cs | 51 +++++++++++++++++ .../sample/RequestQueueSample.csproj | 13 +++++ src/Middleware/RequestQueue/sample/Startup.cs | 50 +++++++++++++++++ .../Microsoft.AspNetCore.RequestQueue.csproj | 19 +++++++ .../RequestQueue/src/SemaphoreWrapper.cs | 33 +++++++++++ ...osoft.AspNetCore.RequestQueue.Tests.csproj | 10 ++++ .../test/SemaphoreWrapperTests.cs | 56 +++++++++++++++++++ src/Middleware/build.cmd | 3 + 10 files changed, 250 insertions(+) create mode 100644 src/Middleware/RequestQueue/ref/Microsoft.AspNetCore.RequestQueue.csproj create mode 100644 src/Middleware/RequestQueue/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs create mode 100644 src/Middleware/RequestQueue/sample/RequestQueueSample.csproj create mode 100644 src/Middleware/RequestQueue/sample/Startup.cs create mode 100644 src/Middleware/RequestQueue/src/Microsoft.AspNetCore.RequestQueue.csproj create mode 100644 src/Middleware/RequestQueue/src/SemaphoreWrapper.cs create mode 100644 src/Middleware/RequestQueue/test/Microsoft.AspNetCore.RequestQueue.Tests.csproj create mode 100644 src/Middleware/RequestQueue/test/SemaphoreWrapperTests.cs create mode 100644 src/Middleware/build.cmd diff --git a/eng/ProjectReferences.props b/eng/ProjectReferences.props index 067ba79788..e274d05a71 100644 --- a/eng/ProjectReferences.props +++ b/eng/ProjectReferences.props @@ -79,6 +79,7 @@ + diff --git a/src/Middleware/RequestQueue/ref/Microsoft.AspNetCore.RequestQueue.csproj b/src/Middleware/RequestQueue/ref/Microsoft.AspNetCore.RequestQueue.csproj new file mode 100644 index 0000000000..05f6a42637 --- /dev/null +++ b/src/Middleware/RequestQueue/ref/Microsoft.AspNetCore.RequestQueue.csproj @@ -0,0 +1,14 @@ + + + + netcoreapp3.0 + + + + + + + + + + diff --git a/src/Middleware/RequestQueue/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs b/src/Middleware/RequestQueue/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs new file mode 100644 index 0000000000..dc22bca076 --- /dev/null +++ b/src/Middleware/RequestQueue/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs @@ -0,0 +1,51 @@ +// 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. + +namespace Microsoft.AspNetCore.Builder +{ + public static partial class HstsBuilderExtensions + { + public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseHsts(this Microsoft.AspNetCore.Builder.IApplicationBuilder app) { throw null; } + } + public static partial class HstsServicesExtensions + { + public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddHsts(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action configureOptions) { throw null; } + } + public static partial class HttpsPolicyBuilderExtensions + { + public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseHttpsRedirection(this Microsoft.AspNetCore.Builder.IApplicationBuilder app) { throw null; } + } + public static partial class HttpsRedirectionServicesExtensions + { + public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddHttpsRedirection(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action configureOptions) { throw null; } + } +} +namespace Microsoft.AspNetCore.HttpsPolicy +{ + public partial class HstsMiddleware + { + public HstsMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions options) { } + public HstsMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions options, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { } + public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; } + } + public partial class HstsOptions + { + public HstsOptions() { } + public System.Collections.Generic.IList ExcludedHosts { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public bool IncludeSubDomains { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } + public System.TimeSpan MaxAge { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } + public bool Preload { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } + } + public partial class HttpsRedirectionMiddleware + { + public HttpsRedirectionMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions options, Microsoft.Extensions.Configuration.IConfiguration config, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { } + public HttpsRedirectionMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions options, Microsoft.Extensions.Configuration.IConfiguration config, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature serverAddressesFeature) { } + public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; } + } + public partial class HttpsRedirectionOptions + { + public HttpsRedirectionOptions() { } + public int? HttpsPort { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } + public int RedirectStatusCode { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } + } +} diff --git a/src/Middleware/RequestQueue/sample/RequestQueueSample.csproj b/src/Middleware/RequestQueue/sample/RequestQueueSample.csproj new file mode 100644 index 0000000000..57892c2249 --- /dev/null +++ b/src/Middleware/RequestQueue/sample/RequestQueueSample.csproj @@ -0,0 +1,13 @@ + + + + netcoreapp3.0 + + + + + + + + + diff --git a/src/Middleware/RequestQueue/sample/Startup.cs b/src/Middleware/RequestQueue/sample/Startup.cs new file mode 100644 index 0000000000..5768d045e6 --- /dev/null +++ b/src/Middleware/RequestQueue/sample/Startup.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.HttpsPolicy; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace RequestQueueSample +{ + public class Startup + { + // 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) + { + } + + public void Configure(IApplicationBuilder app, IWebHostEnvironment environment) + { + app.Run(async context => + { + await context.Response.WriteAsync("Hello world!"); + }); + } + + // Entry point for the application. + public static void Main(string[] args) + { + var host = new WebHostBuilder() + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) // for the cert file + .ConfigureLogging(factory => + { + factory.SetMinimumLevel(LogLevel.Debug); + factory.AddConsole(); + }) + .UseStartup() + .Build(); + + host.Run(); + } + } +} diff --git a/src/Middleware/RequestQueue/src/Microsoft.AspNetCore.RequestQueue.csproj b/src/Middleware/RequestQueue/src/Microsoft.AspNetCore.RequestQueue.csproj new file mode 100644 index 0000000000..ddc77f3682 --- /dev/null +++ b/src/Middleware/RequestQueue/src/Microsoft.AspNetCore.RequestQueue.csproj @@ -0,0 +1,19 @@ + + + + ASP.NET Core middleware for queuing incoming HTTP requests, to avoid threadpool starvation. + netcoreapp3.0 + $(NoWarn);CS1591 + true + true + $(WarningsNotAsErrors);CS1591 + + + + + + + + + + diff --git a/src/Middleware/RequestQueue/src/SemaphoreWrapper.cs b/src/Middleware/RequestQueue/src/SemaphoreWrapper.cs new file mode 100644 index 0000000000..aff2f66919 --- /dev/null +++ b/src/Middleware/RequestQueue/src/SemaphoreWrapper.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Microsoft.AspNetCore.RequestQueue +{ + internal class SemaphoreWrapper + { + private static SemaphoreSlim semaphore; + + public SemaphoreWrapper(int qlength) + { + semaphore = new SemaphoreSlim(qlength); + } + + public Task EnterQueue() + { + return semaphore.WaitAsync(); + } + + public void LeaveQueue() + { + semaphore.Release(); + } + + public int SpotsLeft + { + get => semaphore.CurrentCount; + } + } +} diff --git a/src/Middleware/RequestQueue/test/Microsoft.AspNetCore.RequestQueue.Tests.csproj b/src/Middleware/RequestQueue/test/Microsoft.AspNetCore.RequestQueue.Tests.csproj new file mode 100644 index 0000000000..b1559a97fe --- /dev/null +++ b/src/Middleware/RequestQueue/test/Microsoft.AspNetCore.RequestQueue.Tests.csproj @@ -0,0 +1,10 @@ + + + + netcoreapp3.0 + + + + + + diff --git a/src/Middleware/RequestQueue/test/SemaphoreWrapperTests.cs b/src/Middleware/RequestQueue/test/SemaphoreWrapperTests.cs new file mode 100644 index 0000000000..33bf6ea861 --- /dev/null +++ b/src/Middleware/RequestQueue/test/SemaphoreWrapperTests.cs @@ -0,0 +1,56 @@ +// 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 Xunit; +using System.Threading; +using System.Threading.Tasks; +using System; +using System.Runtime.CompilerServices; +using Microsoft.AspNetCore.Testing; + +namespace Microsoft.AspNetCore.RequestQueue.Tests +{ + public class SemaphoreWrapperTests + { + [Fact] + public async Task TestBehavior() + { + var s = new SemaphoreWrapper(1); + Assert.Equal(1, s.SpotsLeft); + + await s.EnterQueue(); + Assert.Equal(0, s.SpotsLeft); + + s.LeaveQueue(); + Assert.Equal(1, s.SpotsLeft); + } + + [Fact] + public void TestQueueLength() + { + var s = new SemaphoreWrapper(2); + + var t1 = s.EnterQueue(); + Assert.True(t1.IsCompleted); + + var t2 = s.EnterQueue(); + Assert.True(t2.IsCompleted); + + var t3 = s.EnterQueue(); + Assert.False(t3.IsCompleted); + } + + [Fact] + public async Task TestWaiting() + { + var s = new SemaphoreWrapper(1); + await s.EnterQueue(); + + var waitingTask = s.EnterQueue(); + Assert.False(waitingTask.IsCompleted); + + s.LeaveQueue(); + await waitingTask.TimeoutAfter(TimeSpan.FromSeconds(1)); + } + } +} diff --git a/src/Middleware/build.cmd b/src/Middleware/build.cmd new file mode 100644 index 0000000000..033fe6f614 --- /dev/null +++ b/src/Middleware/build.cmd @@ -0,0 +1,3 @@ +@ECHO OFF +SET RepoRoot=%~dp0..\.. +%RepoRoot%\build.cmd -projects %~dp0\**\*.*proj %* From 61f028ad322d5e0e84e2f0957090e3b856d68616 Mon Sep 17 00:00:00 2001 From: Dylan Dmitri Gray Date: Thu, 16 May 2019 16:34:37 -0700 Subject: [PATCH 02/11] Renamed project, cleaned sln file --- eng/ProjectReferences.props | 2 +- src/Middleware/Middleware.sln | 47 +++++++++++++++++++ .../Microsoft.AspNetCore.RequestQueue.csproj | 0 ...t.AspNetCore.RequestQueue.netcoreapp3.0.cs | 0 .../sample/RequestThrottlingSample.csproj} | 0 .../sample/Startup.cs | 0 ...osoft.AspNetCore.RequestThrottling.csproj} | 2 +- .../src/Properties/AssemblyInfo.cs | 6 +++ .../src/SemaphoreWrapper.cs | 0 ...AspNetCore.RequestThrottling.Tests.csproj} | 2 +- .../test/SemaphoreWrapperTests.cs | 0 11 files changed, 56 insertions(+), 3 deletions(-) rename src/Middleware/{RequestQueue => RequestThrottling}/ref/Microsoft.AspNetCore.RequestQueue.csproj (100%) rename src/Middleware/{RequestQueue => RequestThrottling}/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs (100%) rename src/Middleware/{RequestQueue/sample/RequestQueueSample.csproj => RequestThrottling/sample/RequestThrottlingSample.csproj} (100%) rename src/Middleware/{RequestQueue => RequestThrottling}/sample/Startup.cs (100%) rename src/Middleware/{RequestQueue/src/Microsoft.AspNetCore.RequestQueue.csproj => RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj} (95%) create mode 100644 src/Middleware/RequestThrottling/src/Properties/AssemblyInfo.cs rename src/Middleware/{RequestQueue => RequestThrottling}/src/SemaphoreWrapper.cs (100%) rename src/Middleware/{RequestQueue/test/Microsoft.AspNetCore.RequestQueue.Tests.csproj => RequestThrottling/test/Microsoft.AspNetCore.RequestThrottling.Tests.csproj} (71%) rename src/Middleware/{RequestQueue => RequestThrottling}/test/SemaphoreWrapperTests.cs (100%) diff --git a/eng/ProjectReferences.props b/eng/ProjectReferences.props index e274d05a71..97f1875b9e 100644 --- a/eng/ProjectReferences.props +++ b/eng/ProjectReferences.props @@ -79,7 +79,7 @@ - + diff --git a/src/Middleware/Middleware.sln b/src/Middleware/Middleware.sln index 5f16259900..a02c8c40a8 100644 --- a/src/Middleware/Middleware.sln +++ b/src/Middleware/Middleware.sln @@ -283,6 +283,14 @@ 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}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RequestThrottlingSample", "RequestThrottling\sample\RequestThrottlingSample.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}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.RequestThrottling.Tests", "RequestThrottling\test\Microsoft.AspNetCore.RequestThrottling.Tests.csproj", "{353AA2B0-1013-486C-B5BD-9379385CA403}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1541,6 +1549,42 @@ Global {B9BE1823-B555-4AAB-AEBC-C8C3F48C8861}.Release|x64.Build.0 = Release|Any CPU {B9BE1823-B555-4AAB-AEBC-C8C3F48C8861}.Release|x86.ActiveCfg = Release|Any CPU {B9BE1823-B555-4AAB-AEBC-C8C3F48C8861}.Release|x86.Build.0 = Release|Any CPU + {6720919C-0DEA-49E1-90DC-F1883F7919CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6720919C-0DEA-49E1-90DC-F1883F7919CD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6720919C-0DEA-49E1-90DC-F1883F7919CD}.Debug|x64.ActiveCfg = Debug|Any CPU + {6720919C-0DEA-49E1-90DC-F1883F7919CD}.Debug|x64.Build.0 = Debug|Any CPU + {6720919C-0DEA-49E1-90DC-F1883F7919CD}.Debug|x86.ActiveCfg = Debug|Any CPU + {6720919C-0DEA-49E1-90DC-F1883F7919CD}.Debug|x86.Build.0 = Debug|Any CPU + {6720919C-0DEA-49E1-90DC-F1883F7919CD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6720919C-0DEA-49E1-90DC-F1883F7919CD}.Release|Any CPU.Build.0 = Release|Any CPU + {6720919C-0DEA-49E1-90DC-F1883F7919CD}.Release|x64.ActiveCfg = Release|Any CPU + {6720919C-0DEA-49E1-90DC-F1883F7919CD}.Release|x64.Build.0 = Release|Any CPU + {6720919C-0DEA-49E1-90DC-F1883F7919CD}.Release|x86.ActiveCfg = Release|Any CPU + {6720919C-0DEA-49E1-90DC-F1883F7919CD}.Release|x86.Build.0 = Release|Any CPU + {4CE2384D-6B88-4824-ADD1-4183D180FEFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4CE2384D-6B88-4824-ADD1-4183D180FEFF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4CE2384D-6B88-4824-ADD1-4183D180FEFF}.Debug|x64.ActiveCfg = Debug|Any CPU + {4CE2384D-6B88-4824-ADD1-4183D180FEFF}.Debug|x64.Build.0 = Debug|Any CPU + {4CE2384D-6B88-4824-ADD1-4183D180FEFF}.Debug|x86.ActiveCfg = Debug|Any CPU + {4CE2384D-6B88-4824-ADD1-4183D180FEFF}.Debug|x86.Build.0 = Debug|Any CPU + {4CE2384D-6B88-4824-ADD1-4183D180FEFF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4CE2384D-6B88-4824-ADD1-4183D180FEFF}.Release|Any CPU.Build.0 = Release|Any CPU + {4CE2384D-6B88-4824-ADD1-4183D180FEFF}.Release|x64.ActiveCfg = Release|Any CPU + {4CE2384D-6B88-4824-ADD1-4183D180FEFF}.Release|x64.Build.0 = Release|Any CPU + {4CE2384D-6B88-4824-ADD1-4183D180FEFF}.Release|x86.ActiveCfg = Release|Any CPU + {4CE2384D-6B88-4824-ADD1-4183D180FEFF}.Release|x86.Build.0 = Release|Any CPU + {353AA2B0-1013-486C-B5BD-9379385CA403}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {353AA2B0-1013-486C-B5BD-9379385CA403}.Debug|Any CPU.Build.0 = Debug|Any CPU + {353AA2B0-1013-486C-B5BD-9379385CA403}.Debug|x64.ActiveCfg = Debug|Any CPU + {353AA2B0-1013-486C-B5BD-9379385CA403}.Debug|x64.Build.0 = Debug|Any CPU + {353AA2B0-1013-486C-B5BD-9379385CA403}.Debug|x86.ActiveCfg = Debug|Any CPU + {353AA2B0-1013-486C-B5BD-9379385CA403}.Debug|x86.Build.0 = Debug|Any CPU + {353AA2B0-1013-486C-B5BD-9379385CA403}.Release|Any CPU.ActiveCfg = Release|Any CPU + {353AA2B0-1013-486C-B5BD-9379385CA403}.Release|Any CPU.Build.0 = Release|Any CPU + {353AA2B0-1013-486C-B5BD-9379385CA403}.Release|x64.ActiveCfg = Release|Any CPU + {353AA2B0-1013-486C-B5BD-9379385CA403}.Release|x64.Build.0 = Release|Any CPU + {353AA2B0-1013-486C-B5BD-9379385CA403}.Release|x86.ActiveCfg = Release|Any CPU + {353AA2B0-1013-486C-B5BD-9379385CA403}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1663,6 +1707,9 @@ Global {179A159B-87EA-4353-BE92-4FB6CC05BC7D} = {0437D207-864E-429C-92B4-9D08D290188C} {CDE2E736-A034-4748-98C4-0DEDAAC8063D} = {179A159B-87EA-4353-BE92-4FB6CC05BC7D} {B9BE1823-B555-4AAB-AEBC-C8C3F48C8861} = {ACA6DDB9-7592-47CE-A740-D15BF307E9E0} + {6720919C-0DEA-49E1-90DC-F1883F7919CD} = {8C9AA8A2-9D1F-4450-9F8D-56BAB6F3D343} + {4CE2384D-6B88-4824-ADD1-4183D180FEFF} = {8C9AA8A2-9D1F-4450-9F8D-56BAB6F3D343} + {353AA2B0-1013-486C-B5BD-9379385CA403} = {8C9AA8A2-9D1F-4450-9F8D-56BAB6F3D343} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {83786312-A93B-4BB4-AB06-7C6913A59AFA} diff --git a/src/Middleware/RequestQueue/ref/Microsoft.AspNetCore.RequestQueue.csproj b/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestQueue.csproj similarity index 100% rename from src/Middleware/RequestQueue/ref/Microsoft.AspNetCore.RequestQueue.csproj rename to src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestQueue.csproj diff --git a/src/Middleware/RequestQueue/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs b/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs similarity index 100% rename from src/Middleware/RequestQueue/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs rename to src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs diff --git a/src/Middleware/RequestQueue/sample/RequestQueueSample.csproj b/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj similarity index 100% rename from src/Middleware/RequestQueue/sample/RequestQueueSample.csproj rename to src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj diff --git a/src/Middleware/RequestQueue/sample/Startup.cs b/src/Middleware/RequestThrottling/sample/Startup.cs similarity index 100% rename from src/Middleware/RequestQueue/sample/Startup.cs rename to src/Middleware/RequestThrottling/sample/Startup.cs diff --git a/src/Middleware/RequestQueue/src/Microsoft.AspNetCore.RequestQueue.csproj b/src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj similarity index 95% rename from src/Middleware/RequestQueue/src/Microsoft.AspNetCore.RequestQueue.csproj rename to src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj index ddc77f3682..9f410e75c5 100644 --- a/src/Middleware/RequestQueue/src/Microsoft.AspNetCore.RequestQueue.csproj +++ b/src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj @@ -1,4 +1,4 @@ - + ASP.NET Core middleware for queuing incoming HTTP requests, to avoid threadpool starvation. diff --git a/src/Middleware/RequestThrottling/src/Properties/AssemblyInfo.cs b/src/Middleware/RequestThrottling/src/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..1dcaedfaa6 --- /dev/null +++ b/src/Middleware/RequestThrottling/src/Properties/AssemblyInfo.cs @@ -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.RequestThrottling.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] diff --git a/src/Middleware/RequestQueue/src/SemaphoreWrapper.cs b/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs similarity index 100% rename from src/Middleware/RequestQueue/src/SemaphoreWrapper.cs rename to src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs diff --git a/src/Middleware/RequestQueue/test/Microsoft.AspNetCore.RequestQueue.Tests.csproj b/src/Middleware/RequestThrottling/test/Microsoft.AspNetCore.RequestThrottling.Tests.csproj similarity index 71% rename from src/Middleware/RequestQueue/test/Microsoft.AspNetCore.RequestQueue.Tests.csproj rename to src/Middleware/RequestThrottling/test/Microsoft.AspNetCore.RequestThrottling.Tests.csproj index b1559a97fe..8c0dd8e989 100644 --- a/src/Middleware/RequestQueue/test/Microsoft.AspNetCore.RequestQueue.Tests.csproj +++ b/src/Middleware/RequestThrottling/test/Microsoft.AspNetCore.RequestThrottling.Tests.csproj @@ -5,6 +5,6 @@ - + diff --git a/src/Middleware/RequestQueue/test/SemaphoreWrapperTests.cs b/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs similarity index 100% rename from src/Middleware/RequestQueue/test/SemaphoreWrapperTests.cs rename to src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs From ac305849e35e70f195d3e7164ec04ddf4e2f32a1 Mon Sep 17 00:00:00 2001 From: Dylan Dmitri Gray Date: Thu, 16 May 2019 16:55:46 -0700 Subject: [PATCH 03/11] cleanup from PR feedback --- .../RequestThrottling/sample/Startup.cs | 4 +-- ...rosoft.AspNetCore.RequestThrottling.csproj | 5 ++-- .../RequestThrottling/src/SemaphoreWrapper.cs | 16 ++++++------ .../test/SemaphoreWrapperTests.cs | 26 +++++++++++++------ 4 files changed, 30 insertions(+), 21 deletions(-) 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)); } } } From 04ce6ef4c80bd9749d3622cfd9b037c4aedb6bd6 Mon Sep 17 00:00:00 2001 From: Dylan Dmitri Gray Date: Thu, 16 May 2019 16:59:40 -0700 Subject: [PATCH 04/11] IDisposable fix --- .../RequestThrottling/src/SemaphoreWrapper.cs | 7 ++++++- .../RequestThrottling/test/SemaphoreWrapperTests.cs | 10 +++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs b/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs index 7bfb6d377a..4c79b94777 100644 --- a/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs +++ b/src/Middleware/RequestThrottling/src/SemaphoreWrapper.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Microsoft.AspNetCore.RequestThrottling { - internal class SemaphoreWrapper + internal class SemaphoreWrapper : IDisposable { private SemaphoreSlim _semaphore; @@ -29,5 +29,10 @@ namespace Microsoft.AspNetCore.RequestThrottling { get => _semaphore.CurrentCount; } + + public void Dispose() + { + _semaphore.Dispose(); + } } } diff --git a/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs b/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs index d6c990bf1f..368804c8cd 100644 --- a/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs +++ b/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests [Fact] public async Task TracksQueueLength() { - var s = new SemaphoreWrapper(1); + using var s = new SemaphoreWrapper(1); Assert.Equal(1, s.Count); await s.EnterQueue(); @@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests [Fact] public void DoesNotWaitIfSpaceAvailible() { - var s = new SemaphoreWrapper(2); + using var s = new SemaphoreWrapper(2); var t1 = s.EnterQueue(); Assert.True(t1.IsCompleted); @@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests [Fact] public async Task WaitsIfNoSpaceAvailible() { - var s = new SemaphoreWrapper(1); + using var s = new SemaphoreWrapper(1); await s.EnterQueue(); var waitingTask = s.EnterQueue(); @@ -56,8 +56,8 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests [Fact] public async Task IsEncapsulated() { - var s1 = new SemaphoreWrapper(1); - var s2 = new SemaphoreWrapper(1); + using var s1 = new SemaphoreWrapper(1); + using var s2 = new SemaphoreWrapper(1); await s1.EnterQueue(); await s2.EnterQueue().TimeoutAfter(TimeSpan.FromSeconds(30)); From 9363eff2a8261c511923c74065bbbfc7e12c2278 Mon Sep 17 00:00:00 2001 From: Dylan Dmitri Gray Date: Fri, 17 May 2019 10:40:19 -0700 Subject: [PATCH 05/11] OrTimeout Extension --- .../RequestThrottling/sample/Startup.cs | 2 ++ .../test/SemaphoreWrapperTests.cs | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Middleware/RequestThrottling/sample/Startup.cs b/src/Middleware/RequestThrottling/sample/Startup.cs index a9bc74e319..ea4969c9bd 100644 --- a/src/Middleware/RequestThrottling/sample/Startup.cs +++ b/src/Middleware/RequestThrottling/sample/Startup.cs @@ -26,7 +26,9 @@ namespace RequestThrottlingSample { app.Run(async context => { + Console.WriteLine("HEWWWO?"); await context.Response.WriteAsync("Hello world!"); + Console.WriteLine("GOODBWWYWE!"); }); } diff --git a/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs b/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs index 368804c8cd..f9ddd80420 100644 --- a/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs +++ b/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs @@ -10,6 +10,13 @@ using Microsoft.AspNetCore.Testing; namespace Microsoft.AspNetCore.RequestThrottling.Tests { + public static class TaskExtensions + { + public static Task OrTimeout(this Task task, int seconds = 30) + { + return task.TimeoutAfter(TimeSpan.FromSeconds(seconds)); + } + } public class SemaphoreWrapperTests { [Fact] @@ -18,7 +25,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests using var s = new SemaphoreWrapper(1); Assert.Equal(1, s.Count); - await s.EnterQueue(); + await s.EnterQueue().OrTimeout(); Assert.Equal(0, s.Count); s.LeaveQueue(); @@ -44,13 +51,13 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests public async Task WaitsIfNoSpaceAvailible() { using var s = new SemaphoreWrapper(1); - await s.EnterQueue(); + await s.EnterQueue().OrTimeout(); var waitingTask = s.EnterQueue(); Assert.False(waitingTask.IsCompleted); s.LeaveQueue(); - await waitingTask.TimeoutAfter(TimeSpan.FromSeconds(30)); + await waitingTask.OrTimeout(); } [Fact] @@ -59,8 +66,8 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests using var s1 = new SemaphoreWrapper(1); using var s2 = new SemaphoreWrapper(1); - await s1.EnterQueue(); - await s2.EnterQueue().TimeoutAfter(TimeSpan.FromSeconds(30)); + await s1.EnterQueue().OrTimeout(); + await s2.EnterQueue().OrTimeout(); } } } From 3e6f70d5e8527fd59b27d137ca0d66564f485945 Mon Sep 17 00:00:00 2001 From: Dylan Dmitri Gray Date: Fri, 17 May 2019 10:54:56 -0700 Subject: [PATCH 06/11] Justin fixed refs --- .../Microsoft.AspNetCore.RequestQueue.csproj | 14 ----- ...t.AspNetCore.RequestQueue.netcoreapp3.0.cs | 51 ------------------- ...rosoft.AspNetCore.RequestThrottling.csproj | 10 ++++ ...NetCore.RequestThrottling.netcoreapp3.0.cs | 3 ++ ...rosoft.AspNetCore.RequestThrottling.csproj | 7 --- 5 files changed, 13 insertions(+), 72 deletions(-) delete mode 100644 src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestQueue.csproj delete mode 100644 src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs create mode 100644 src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestThrottling.csproj create mode 100644 src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestThrottling.netcoreapp3.0.cs diff --git a/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestQueue.csproj b/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestQueue.csproj deleted file mode 100644 index 05f6a42637..0000000000 --- a/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestQueue.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - netcoreapp3.0 - - - - - - - - - - diff --git a/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs b/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs deleted file mode 100644 index dc22bca076..0000000000 --- a/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestQueue.netcoreapp3.0.cs +++ /dev/null @@ -1,51 +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. - -namespace Microsoft.AspNetCore.Builder -{ - public static partial class HstsBuilderExtensions - { - public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseHsts(this Microsoft.AspNetCore.Builder.IApplicationBuilder app) { throw null; } - } - public static partial class HstsServicesExtensions - { - public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddHsts(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action configureOptions) { throw null; } - } - public static partial class HttpsPolicyBuilderExtensions - { - public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseHttpsRedirection(this Microsoft.AspNetCore.Builder.IApplicationBuilder app) { throw null; } - } - public static partial class HttpsRedirectionServicesExtensions - { - public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddHttpsRedirection(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action configureOptions) { throw null; } - } -} -namespace Microsoft.AspNetCore.HttpsPolicy -{ - public partial class HstsMiddleware - { - public HstsMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions options) { } - public HstsMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions options, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { } - public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; } - } - public partial class HstsOptions - { - public HstsOptions() { } - public System.Collections.Generic.IList ExcludedHosts { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } - public bool IncludeSubDomains { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } - public System.TimeSpan MaxAge { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } - public bool Preload { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } - } - public partial class HttpsRedirectionMiddleware - { - public HttpsRedirectionMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions options, Microsoft.Extensions.Configuration.IConfiguration config, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { } - public HttpsRedirectionMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions options, Microsoft.Extensions.Configuration.IConfiguration config, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature serverAddressesFeature) { } - public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; } - } - public partial class HttpsRedirectionOptions - { - public HttpsRedirectionOptions() { } - public int? HttpsPort { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } - public int RedirectStatusCode { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } - } -} diff --git a/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestThrottling.csproj b/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestThrottling.csproj new file mode 100644 index 0000000000..0a1bcdd0b9 --- /dev/null +++ b/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestThrottling.csproj @@ -0,0 +1,10 @@ + + + + netcoreapp3.0 + + + + + + diff --git a/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestThrottling.netcoreapp3.0.cs b/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestThrottling.netcoreapp3.0.cs new file mode 100644 index 0000000000..618082bc4a --- /dev/null +++ b/src/Middleware/RequestThrottling/ref/Microsoft.AspNetCore.RequestThrottling.netcoreapp3.0.cs @@ -0,0 +1,3 @@ +// 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. + diff --git a/src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj b/src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj index c9b165fa16..3abc5a62f4 100644 --- a/src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj +++ b/src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj @@ -8,11 +8,4 @@ $(WarningsNotAsErrors);CS1591 - - - - - - - From 5e85b3773b4c46a24e27fa71d13fd431d9056e01 Mon Sep 17 00:00:00 2001 From: Dylan Dmitri Gray Date: Fri, 17 May 2019 14:27:06 -0700 Subject: [PATCH 07/11] ready for final review --- eng/ProjectReferences.props | 2 +- .../RequestThrottling/RequestThrottling.slnf | 25 +++++++ .../RequestThrottling/sample/Startup.cs | 4 +- .../test/SemaphoreWrapperTests.cs | 7 -- .../RequestThrottling/test/TaskExtensions.cs | 69 +++++++++++++++++++ 5 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 src/Middleware/RequestThrottling/RequestThrottling.slnf create mode 100644 src/Middleware/RequestThrottling/test/TaskExtensions.cs diff --git a/eng/ProjectReferences.props b/eng/ProjectReferences.props index 97f1875b9e..920e15a65e 100644 --- a/eng/ProjectReferences.props +++ b/eng/ProjectReferences.props @@ -79,10 +79,10 @@ - + diff --git a/src/Middleware/RequestThrottling/RequestThrottling.slnf b/src/Middleware/RequestThrottling/RequestThrottling.slnf new file mode 100644 index 0000000000..d434fbc862 --- /dev/null +++ b/src/Middleware/RequestThrottling/RequestThrottling.slnf @@ -0,0 +1,25 @@ +{ + "solution": { + "path": "..\\Middleware.sln", + "projects": [ + "..\\Hosting\\Abstractions\\src\\Microsoft.AspNetCore.Hosting.Abstractions.csproj", + "..\\Hosting\\Hosting\\src\\Microsoft.AspNetCore.Hosting.csproj", + "..\\Hosting\\Server.Abstractions\\src\\Microsoft.AspNetCore.Hosting.Server.Abstractions.csproj", + "..\\Http\\Http.Abstractions\\src\\Microsoft.AspNetCore.Http.Abstractions.csproj", + "..\\Http\\Http.Extensions\\src\\Microsoft.AspNetCore.Http.Extensions.csproj", + "..\\Http\\Http.Features\\src\\Microsoft.AspNetCore.Http.Features.csproj", + "..\\Http\\WebUtilities\\src\\Microsoft.AspNetCore.WebUtilities.csproj", + "..\\Servers\\Connections.Abstractions\\src\\Microsoft.AspNetCore.Connections.Abstractions.csproj", + "..\\Servers\\Kestrel\\Core\\src\\Microsoft.AspNetCore.Server.Kestrel.Core.csproj", + "..\\Servers\\Kestrel\\Kestrel\\src\\Microsoft.AspNetCore.Server.Kestrel.csproj", + "..\\Servers\\Kestrel\\Transport.Abstractions\\src\\Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.csproj", + "..\\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" + ] + } +} \ No newline at end of file diff --git a/src/Middleware/RequestThrottling/sample/Startup.cs b/src/Middleware/RequestThrottling/sample/Startup.cs index ea4969c9bd..8f0ba344c6 100644 --- a/src/Middleware/RequestThrottling/sample/Startup.cs +++ b/src/Middleware/RequestThrottling/sample/Startup.cs @@ -13,7 +13,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace RequestThrottlingSample -{ +{ public class Startup { // This method gets called by the runtime. Use this method to add services to the container. @@ -26,9 +26,7 @@ namespace RequestThrottlingSample { app.Run(async context => { - Console.WriteLine("HEWWWO?"); await context.Response.WriteAsync("Hello world!"); - Console.WriteLine("GOODBWWYWE!"); }); } diff --git a/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs b/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs index f9ddd80420..b5cdfce18f 100644 --- a/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs +++ b/src/Middleware/RequestThrottling/test/SemaphoreWrapperTests.cs @@ -10,13 +10,6 @@ using Microsoft.AspNetCore.Testing; namespace Microsoft.AspNetCore.RequestThrottling.Tests { - public static class TaskExtensions - { - public static Task OrTimeout(this Task task, int seconds = 30) - { - return task.TimeoutAfter(TimeSpan.FromSeconds(seconds)); - } - } public class SemaphoreWrapperTests { [Fact] diff --git a/src/Middleware/RequestThrottling/test/TaskExtensions.cs b/src/Middleware/RequestThrottling/test/TaskExtensions.cs new file mode 100644 index 0000000000..52ec0c4303 --- /dev/null +++ b/src/Middleware/RequestThrottling/test/TaskExtensions.cs @@ -0,0 +1,69 @@ +// 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.Diagnostics; +using System.Runtime.CompilerServices; +using Microsoft.AspNetCore.Testing; + +namespace System.Threading.Tasks +{ +#if TESTUTILS + public +#else + internal +#endif + static class TaskExtensions + { + private const int DefaultTimeout = 30 * 1000; + + public static Task OrTimeout(this Task task, int milliseconds = DefaultTimeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) + { + return OrTimeout(task, new TimeSpan(0, 0, 0, 0, milliseconds), memberName, filePath, lineNumber); + } + + public static Task OrTimeout(this Task task, TimeSpan timeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) + { + return task.TimeoutAfter(timeout, filePath, lineNumber ?? 0); + } + + public static Task OrTimeout(this ValueTask task, int milliseconds = DefaultTimeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) => + OrTimeout(task, new TimeSpan(0, 0, 0, 0, milliseconds), memberName, filePath, lineNumber); + + public static Task OrTimeout(this ValueTask task, TimeSpan timeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) => + task.AsTask().OrTimeout(timeout, memberName, filePath, lineNumber); + + public static Task OrTimeout(this Task task, int milliseconds = DefaultTimeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) + { + return OrTimeout(task, new TimeSpan(0, 0, 0, 0, milliseconds), memberName, filePath, lineNumber); + } + + public static Task OrTimeout(this Task task, TimeSpan timeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) + { + return task.TimeoutAfter(timeout, filePath, lineNumber ?? 0); + } + + public static async Task OrThrowIfOtherFails(this Task task, Task otherTask) + { + var completed = await Task.WhenAny(task, otherTask); + if (completed == otherTask && otherTask.IsFaulted) + { + // Manifest the exception + otherTask.GetAwaiter().GetResult(); + throw new Exception("Unreachable code"); + } + else + { + // Await the task we were asked to await. Either it's finished, or the otherTask finished successfully, and it's not our job to check that + await task; + } + } + + public static async Task OrThrowIfOtherFails(this Task task, Task otherTask) + { + await OrThrowIfOtherFails((Task)task, otherTask); + + // If we get here, 'task' is finished and succeeded. + return task.GetAwaiter().GetResult(); + } + } +} From 206c1e8a8a019119bf8a0d418427659ab0a8cfab Mon Sep 17 00:00:00 2001 From: Dylan Dmitri Gray Date: Fri, 17 May 2019 14:37:16 -0700 Subject: [PATCH 08/11] Removed error line --- .../src/Microsoft.AspNetCore.RequestThrottling.csproj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj b/src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj index 3abc5a62f4..5014e9cec5 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 true aspnetcore;queue;queuing - $(WarningsNotAsErrors);CS1591 From 68606a2d804a1873dcfc2eccf4c1d9ccaf1c67dd Mon Sep 17 00:00:00 2001 From: Dylan Dmitri Gray Date: Fri, 17 May 2019 16:56:17 -0700 Subject: [PATCH 09/11] removed redundant HttpsPolicy dependency --- .../RequestThrottling/sample/RequestThrottlingSample.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj b/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj index 57892c2249..fd9214e35a 100644 --- a/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj +++ b/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj @@ -7,7 +7,7 @@ - + From 7ff52931770c3fe1fa4f00b3a2db31a8d462085d Mon Sep 17 00:00:00 2001 From: Dylan Dmitri Gray Date: Sat, 18 May 2019 18:31:31 -0700 Subject: [PATCH 10/11] added HttpsPolicy back --- .../RequestThrottling/sample/RequestThrottlingSample.csproj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj b/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj index fd9214e35a..aba86dfadf 100644 --- a/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj +++ b/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.0 @@ -7,6 +7,7 @@ + From 54e5b15c24d743b29c1fdb63614dde7bef74fb36 Mon Sep 17 00:00:00 2001 From: Dylan Dmitri Gray Date: Mon, 20 May 2019 11:58:30 -0700 Subject: [PATCH 11/11] it's gone for good --- .../RequestThrottling/sample/RequestThrottlingSample.csproj | 1 - src/Middleware/RequestThrottling/sample/Startup.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj b/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj index aba86dfadf..0f80e6516a 100644 --- a/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj +++ b/src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj @@ -7,7 +7,6 @@ - diff --git a/src/Middleware/RequestThrottling/sample/Startup.cs b/src/Middleware/RequestThrottling/sample/Startup.cs index 8f0ba344c6..95a94be56d 100644 --- a/src/Middleware/RequestThrottling/sample/Startup.cs +++ b/src/Middleware/RequestThrottling/sample/Startup.cs @@ -7,7 +7,6 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging;