From 566a587126a1a091f05dba2804d5baf6ecea4118 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Tue, 25 Apr 2017 00:37:09 -0700 Subject: [PATCH] Added ApplicationSchedulingMode to KestrelServerOptions (#1759) * Added ApplicationSchedulingMode to KestrelServerOptions - Made default mode Default instead of ThreadPool --- samples/SampleApp/Startup.cs | 4 ++-- .../KestrelServer.cs | 20 ++++++++++++------- .../KestrelServerOptions.cs | 7 +++++++ .../SchedulingMode.cs | 12 +++++++++++ 4 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/SchedulingMode.cs diff --git a/samples/SampleApp/Startup.cs b/samples/SampleApp/Startup.cs index 8bf9350872..6c776817c1 100644 --- a/samples/SampleApp/Startup.cs +++ b/samples/SampleApp/Startup.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Server.Kestrel.Core.Internal; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.Extensions.Logging; namespace SampleApp @@ -45,7 +45,7 @@ namespace SampleApp .UseKestrel(options => { // Run callbacks on the transport thread - options.UseTransportThread = true; + options.ApplicationSchedulingMode = SchedulingMode.Inline; options.Listen(IPAddress.Loopback, 5000, listenOptions => { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs index a442d30e93..b325ab723a 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs @@ -72,14 +72,20 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core var systemClock = new SystemClock(); var dateHeaderValueManager = new DateHeaderValueManager(systemClock); - IThreadPool threadPool; - if (serverOptions.UseTransportThread) + // TODO: This logic will eventually move into the IConnectionHandler and off + // the service context once we get to https://github.com/aspnet/KestrelHttpServer/issues/1662 + IThreadPool threadPool = null; + switch (serverOptions.ApplicationSchedulingMode) { - threadPool = new InlineLoggingThreadPool(trace); - } - else - { - threadPool = new LoggingThreadPool(trace); + case SchedulingMode.Default: + case SchedulingMode.ThreadPool: + threadPool = new LoggingThreadPool(trace); + break; + case SchedulingMode.Inline: + threadPool = new InlineLoggingThreadPool(trace); + break; + default: + throw new NotSupportedException($"Unknown transport mode {serverOptions.ApplicationSchedulingMode}"); } return new ServiceContext diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServerOptions.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServerOptions.cs index 9eb5084fd3..f97e5c3dd3 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServerOptions.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServerOptions.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Net; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; namespace Microsoft.AspNetCore.Server.Kestrel.Core { @@ -33,6 +34,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core /// public bool UseTransportThread { get; set; } + /// + /// Gets or sets a value that determines how Kestrel should schedule user callbacks. + /// + /// The default mode is + public SchedulingMode ApplicationSchedulingMode { get; set; } = SchedulingMode.Default; + /// /// Enables the Listen options callback to resolve and use services registered by the application during startup. /// Typically initialized by UseKestrel()"/>. diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/SchedulingMode.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/SchedulingMode.cs new file mode 100644 index 0000000000..5408bc1d11 --- /dev/null +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/SchedulingMode.cs @@ -0,0 +1,12 @@ +// 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.Server.Kestrel.Transport.Abstractions +{ + public enum SchedulingMode + { + Default, + ThreadPool, + Inline + } +}