Pass ServerInformation as property of ServiceContext

This commit is contained in:
Ben Adams 2016-02-13 05:51:54 +00:00
parent 460dbb15c2
commit f8aa1a676a
21 changed files with 174 additions and 118 deletions

View File

@ -61,7 +61,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
} }
// Don't initialize _frame until SocketInput and SocketOutput are set to their final values. // Don't initialize _frame until SocketInput and SocketOutput are set to their final values.
if (ConnectionFilter == null) if (ServerInformation.ConnectionFilter == null)
{ {
SocketInput = _rawSocketInput; SocketInput = _rawSocketInput;
SocketOutput = _rawSocketOutput; SocketOutput = _rawSocketOutput;
@ -81,7 +81,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
try try
{ {
ConnectionFilter.OnConnectionAsync(_filterContext).ContinueWith((task, state) => ServerInformation.ConnectionFilter.OnConnectionAsync(_filterContext).ContinueWith((task, state) =>
{ {
var connection = (Connection)state; var connection = (Connection)state;

View File

@ -45,6 +45,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
private readonly object _onStartingSync = new Object(); private readonly object _onStartingSync = new Object();
private readonly object _onCompletedSync = new Object(); private readonly object _onCompletedSync = new Object();
protected bool _poolingPermitted = true;
private Headers _frameHeaders; private Headers _frameHeaders;
private Streams _frameStreams; private Streams _frameStreams;
@ -279,7 +280,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
DuplexStream = null; DuplexStream = null;
var frameStreams = _frameStreams; var frameStreams = _frameStreams;
_frameStreams = null; _frameStreams = null;
HttpComponentFactory.DisposeStreams(frameStreams, poolingPermitted: (poolingPermitted && ReuseStreams)); HttpComponentFactory.DisposeStreams(frameStreams, poolingPermitted);
} }
} }

View File

@ -131,14 +131,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
} }
catch (Exception ex) catch (Exception ex)
{ {
// Error occurred, do not return components to pool
_poolingPermitted = false;
Log.LogWarning(0, ex, "Connection processing ended abnormally"); Log.LogWarning(0, ex, "Connection processing ended abnormally");
} }
finally finally
{ {
// Error occurred, do not return components to pool
ResetComponents(poolingPermitted: false);
try try
{ {
ResetComponents(poolingPermitted: _poolingPermitted);
_abortedCts = null; _abortedCts = null;
// If _requestAborted is set, the connection has already been closed. // If _requestAborted is set, the connection has already been closed.

View File

@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
{ {
var socket = new UvTcpHandle(Log); var socket = new UvTcpHandle(Log);
socket.Init(Thread.Loop, Thread.QueueCloseHandle); socket.Init(Thread.Loop, Thread.QueueCloseHandle);
socket.NoDelay(NoDelay); socket.NoDelay(ServerInformation.NoDelay);
socket.Bind(ServerAddress); socket.Bind(ServerAddress);
socket.Listen(Constants.ListenBacklog, (stream, status, error, state) => ConnectionCallback(stream, status, error, state), this); socket.Listen(Constants.ListenBacklog, (stream, status, error, state) => ConnectionCallback(stream, status, error, state), this);
return socket; return socket;
@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
try try
{ {
acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle); acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle);
acceptSocket.NoDelay(NoDelay); acceptSocket.NoDelay(ServerInformation.NoDelay);
listenSocket.Accept(acceptSocket); listenSocket.Accept(acceptSocket);
DispatchConnection(acceptSocket); DispatchConnection(acceptSocket);

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
{ {
var socket = new UvTcpHandle(Log); var socket = new UvTcpHandle(Log);
socket.Init(Thread.Loop, Thread.QueueCloseHandle); socket.Init(Thread.Loop, Thread.QueueCloseHandle);
socket.NoDelay(NoDelay); socket.NoDelay(ServerInformation.NoDelay);
socket.Bind(ServerAddress); socket.Bind(ServerAddress);
socket.Listen(Constants.ListenBacklog, (stream, status, error, state) => ConnectionCallback(stream, status, error, state), this); socket.Listen(Constants.ListenBacklog, (stream, status, error, state) => ConnectionCallback(stream, status, error, state), this);
return socket; return socket;
@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
try try
{ {
acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle); acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle);
acceptSocket.NoDelay(NoDelay); acceptSocket.NoDelay(ServerInformation.NoDelay);
listenSocket.Accept(acceptSocket); listenSocket.Accept(acceptSocket);
DispatchConnection(acceptSocket); DispatchConnection(acceptSocket);

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
{ {
var acceptSocket = new UvTcpHandle(Log); var acceptSocket = new UvTcpHandle(Log);
acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle); acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle);
acceptSocket.NoDelay(NoDelay); acceptSocket.NoDelay(ServerInformation.NoDelay);
return acceptSocket; return acceptSocket;
} }
} }

View File

@ -12,17 +12,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel
bool NoDelay { get; set; } bool NoDelay { get; set; }
/// <summary> /// <summary>
/// Gets or sets a flag that instructs <seealso cref="KestrelServer"/> whether it is safe to /// Gets or sets values that instruct <seealso cref="KestrelServer"/> whether it is safe to
/// reuse the Request and Response <seealso cref="System.IO.Stream"/> objects /// pool the Request and Response <seealso cref="System.IO.Stream"/> objects, Headers etc
/// for another request after the Response's OnCompleted callback has fired. /// for another request after the Response's OnCompleted callback has fired.
/// When this is set to true it is not safe to retain references to these streams after this event has fired. /// When these values are greater than zero it is not safe to retain references to feature components after this event has fired.
/// It is false by default. /// They are zero by default.
/// </summary> /// </summary>
/// <remarks> KestrelServerPoolingParameters PoolingParameters { get; }
/// When this is set to true it is not safe to retain references to these streams after this event has fired.
/// It is false by default.
/// </remarks>
bool ReuseStreams { get; set; }
IConnectionFilter ConnectionFilter { get; set; } IConnectionFilter ConnectionFilter { get; set; }
} }

View File

@ -2,17 +2,24 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Concurrent; using System.Collections.Concurrent;
using Microsoft.AspNetCore.Server.Kestrel.Http;
using System.Text; using System.Text;
using Microsoft.AspNetCore.Server.Kestrel.Http;
namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
{ {
class HttpComponentFactory : IHttpComponentFactory class HttpComponentFactory : IHttpComponentFactory
{ {
private const int _maxPooledComponents = 128;
private ConcurrentQueue<Streams> _streamPool = new ConcurrentQueue<Streams>(); private ConcurrentQueue<Streams> _streamPool = new ConcurrentQueue<Streams>();
private ConcurrentQueue<Headers> _headerPool = new ConcurrentQueue<Headers>(); private ConcurrentQueue<Headers> _headerPool = new ConcurrentQueue<Headers>();
public IKestrelServerInformation ServerInformation { get; set; }
public HttpComponentFactory(IKestrelServerInformation serverInformation)
{
ServerInformation = serverInformation;
}
public Streams CreateStreams(FrameContext owner) public Streams CreateStreams(FrameContext owner)
{ {
Streams streams; Streams streams;
@ -29,7 +36,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
public void DisposeStreams(Streams streams, bool poolingPermitted) public void DisposeStreams(Streams streams, bool poolingPermitted)
{ {
if (poolingPermitted && _streamPool.Count < _maxPooledComponents) if (poolingPermitted && _streamPool.Count < ServerInformation.PoolingParameters.MaxPooledStreams)
{ {
streams.Uninitialize(); streams.Uninitialize();
@ -53,7 +60,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
public void DisposeHeaders(Headers headers, bool poolingPermitted) public void DisposeHeaders(Headers headers, bool poolingPermitted)
{ {
if (poolingPermitted && _headerPool.Count < _maxPooledComponents) if (poolingPermitted && _headerPool.Count < ServerInformation.PoolingParameters.MaxPooledHeaders)
{ {
headers.Uninitialize(); headers.Uninitialize();

View File

@ -7,6 +7,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
{ {
interface IHttpComponentFactory interface IHttpComponentFactory
{ {
IKestrelServerInformation ServerInformation { get; set; }
Streams CreateStreams(FrameContext owner); Streams CreateStreams(FrameContext owner);
void DisposeStreams(Streams streams, bool poolingPermitted); void DisposeStreams(Streams streams, bool poolingPermitted);

View File

@ -54,9 +54,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel
try try
{ {
var information = (KestrelServerInformation)Features.Get<IKestrelServerInformation>(); var information = (KestrelServerInformation)Features.Get<IKestrelServerInformation>();
var componentFactory = Features.Get<IHttpComponentFactory>();
var dateHeaderValueManager = new DateHeaderValueManager(); var dateHeaderValueManager = new DateHeaderValueManager();
var trace = new KestrelTrace(_logger); var trace = new KestrelTrace(_logger);
var componentFactory = new HttpComponentFactory();
var engine = new KestrelEngine(new ServiceContext var engine = new KestrelEngine(new ServiceContext
{ {
FrameFactory = (context, remoteEP, localEP, prepareRequest) => FrameFactory = (context, remoteEP, localEP, prepareRequest) =>
@ -67,9 +67,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
Log = trace, Log = trace,
ThreadPool = new LoggingThreadPool(trace), ThreadPool = new LoggingThreadPool(trace),
DateHeaderValueManager = dateHeaderValueManager, DateHeaderValueManager = dateHeaderValueManager,
ConnectionFilter = information.ConnectionFilter, ServerInformation = information,
NoDelay = information.NoDelay,
ReuseStreams = information.ReuseStreams,
HttpComponentFactory = componentFactory HttpComponentFactory = componentFactory
}); });

View File

@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
Addresses = GetAddresses(configuration); Addresses = GetAddresses(configuration);
ThreadCount = GetThreadCount(configuration); ThreadCount = GetThreadCount(configuration);
NoDelay = GetNoDelay(configuration); NoDelay = GetNoDelay(configuration);
ReuseStreams = GetReuseStreams(configuration); PoolingParameters = new KestrelServerPoolingParameters(configuration);
} }
public ICollection<string> Addresses { get; } public ICollection<string> Addresses { get; }
@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
public bool NoDelay { get; set; } public bool NoDelay { get; set; }
public bool ReuseStreams { get; set; } public KestrelServerPoolingParameters PoolingParameters { get; }
public IConnectionFilter ConnectionFilter { get; set; } public IConnectionFilter ConnectionFilter { get; set; }
@ -110,18 +110,5 @@ namespace Microsoft.AspNetCore.Server.Kestrel
return true; return true;
} }
private static bool GetReuseStreams(IConfiguration configuration)
{
var reuseStreamsString = configuration["kestrel.reuseStreams"];
bool reuseStreams;
if (bool.TryParse(reuseStreamsString, out reuseStreams))
{
return reuseStreams;
}
return false;
}
} }
} }

View File

@ -0,0 +1,43 @@
// 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.Globalization;
using Microsoft.Extensions.Configuration;
namespace Microsoft.AspNetCore.Server.Kestrel
{
public class KestrelServerPoolingParameters
{
public KestrelServerPoolingParameters(IConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
MaxPooledStreams = GetPooledCount(configuration["kestrel.maxPooledStreams"]);
MaxPooledHeaders = GetPooledCount(configuration["kestrel.maxPooledHeaders"]);
}
public int MaxPooledStreams { get; set; }
public int MaxPooledHeaders { get; set; }
private static int GetPooledCount(string countString)
{
if (string.IsNullOrEmpty(countString))
{
return 0;
}
int count;
if (int.TryParse(countString, NumberStyles.Integer, CultureInfo.InvariantCulture, out count))
{
return count;
}
return 0;
}
}
}

View File

@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Features; using Microsoft.AspNetCore.Server.Features;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -28,8 +29,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel
public IServer CreateServer(IConfiguration configuration) public IServer CreateServer(IConfiguration configuration)
{ {
var information = new KestrelServerInformation(configuration); var information = new KestrelServerInformation(configuration);
var componentFactory = new HttpComponentFactory(information);
var serverFeatures = new FeatureCollection(); var serverFeatures = new FeatureCollection();
serverFeatures.Set<IKestrelServerInformation>(information); serverFeatures.Set<IKestrelServerInformation>(information);
serverFeatures.Set<IHttpComponentFactory>(componentFactory);
serverFeatures.Set<IServerAddressesFeature>(information); serverFeatures.Set<IServerAddressesFeature>(information);
return new KestrelServer(serverFeatures, _appLifetime, _loggerFactory.CreateLogger("Microsoft.AspNetCore.Server.Kestrel")); return new KestrelServer(serverFeatures, _appLifetime, _loggerFactory.CreateLogger("Microsoft.AspNetCore.Server.Kestrel"));
} }

View File

@ -24,9 +24,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
ThreadPool = context.ThreadPool; ThreadPool = context.ThreadPool;
FrameFactory = context.FrameFactory; FrameFactory = context.FrameFactory;
DateHeaderValueManager = context.DateHeaderValueManager; DateHeaderValueManager = context.DateHeaderValueManager;
ConnectionFilter = context.ConnectionFilter; ServerInformation = context.ServerInformation;
NoDelay = context.NoDelay;
ReuseStreams = context.ReuseStreams;
HttpComponentFactory = context.HttpComponentFactory; HttpComponentFactory = context.HttpComponentFactory;
} }
@ -40,11 +38,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
public DateHeaderValueManager DateHeaderValueManager { get; set; } public DateHeaderValueManager DateHeaderValueManager { get; set; }
public IConnectionFilter ConnectionFilter { get; set; } public IKestrelServerInformation ServerInformation { get; set; }
public bool NoDelay { get; set; }
public bool ReuseStreams { get; set; }
internal IHttpComponentFactory HttpComponentFactory { get; set; } internal IHttpComponentFactory HttpComponentFactory { get; set; }
} }

View File

@ -132,7 +132,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
[ConditionalFact] [ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Test hangs after execution on Mono.")] [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Test hangs after execution on Mono.")]
public async Task RequestPathIsNormalized() public void RequestPathIsNormalized()
{ {
var port = PortManager.GetPort(); var port = PortManager.GetPort();
var config = new ConfigurationBuilder().AddInMemoryCollection( var config = new ConfigurationBuilder().AddInMemoryCollection(

View File

@ -36,10 +36,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
public async Task CanReadAndWriteWithRewritingConnectionFilter() public async Task CanReadAndWriteWithRewritingConnectionFilter()
{ {
var filter = new RewritingConnectionFilter(); var filter = new RewritingConnectionFilter();
var serviceContext = new TestServiceContext() var serviceContext = new TestServiceContext(filter);
{
ConnectionFilter = filter
};
var sendString = "POST / HTTP/1.0\r\n\r\nHello World?"; var sendString = "POST / HTTP/1.0\r\n\r\nHello World?";
using (var server = new TestServer(App, serviceContext)) using (var server = new TestServer(App, serviceContext))
@ -62,10 +60,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Test hangs after execution on Mono.")] [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Test hangs after execution on Mono.")]
public async Task CanReadAndWriteWithAsyncConnectionFilter() public async Task CanReadAndWriteWithAsyncConnectionFilter()
{ {
var serviceContext = new TestServiceContext() var serviceContext = new TestServiceContext(new AsyncConnectionFilter());
{
ConnectionFilter = new AsyncConnectionFilter()
};
using (var server = new TestServer(App, serviceContext)) using (var server = new TestServer(App, serviceContext))
{ {
@ -87,10 +82,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Test hangs after execution on Mono.")] [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Test hangs after execution on Mono.")]
public async Task ThrowingSynchronousConnectionFilterDoesNotCrashServer() public async Task ThrowingSynchronousConnectionFilterDoesNotCrashServer()
{ {
var serviceContext = new TestServiceContext() var serviceContext = new TestServiceContext(new ThrowingConnectionFilter());
{
ConnectionFilter = new ThrowingConnectionFilter()
};
using (var server = new TestServer(App, serviceContext)) using (var server = new TestServer(App, serviceContext))
{ {

View File

@ -35,10 +35,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
new TestServiceContext() new TestServiceContext()
}, },
{ {
new TestServiceContext new TestServiceContext(new PassThroughConnectionFilter())
{
ConnectionFilter = new PassThroughConnectionFilter()
}
} }
}; };
} }
@ -188,7 +185,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[MemberData(nameof(ConnectionFilterData))] [MemberData(nameof(ConnectionFilterData))]
public async Task ReuseStreamsOn(ServiceContext testContext) public async Task ReuseStreamsOn(ServiceContext testContext)
{ {
testContext.ReuseStreams = true; testContext.ServerInformation.PoolingParameters.MaxPooledStreams = 120;
var streamCount = 0; var streamCount = 0;
var loopCount = 20; var loopCount = 20;
@ -231,7 +228,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[MemberData(nameof(ConnectionFilterData))] [MemberData(nameof(ConnectionFilterData))]
public async Task ReuseStreamsOff(ServiceContext testContext) public async Task ReuseStreamsOff(ServiceContext testContext)
{ {
testContext.ReuseStreams = false; testContext.ServerInformation.PoolingParameters.MaxPooledStreams = 0;
var streamCount = 0; var streamCount = 0;
var loopCount = 20; var loopCount = 20;

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using Microsoft.AspNetCore.Server.Kestrel; using Microsoft.AspNetCore.Server.Kestrel;
using Microsoft.AspNetCore.Server.Kestrel.Http; using Microsoft.AspNetCore.Server.Kestrel.Http;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
using Xunit; using Xunit;
@ -16,12 +17,15 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[Fact] [Fact]
public void InitialDictionaryContainsServerAndDate() public void InitialDictionaryContainsServerAndDate()
{ {
var configuration = new ConfigurationBuilder().Build();
var serverInformation = new KestrelServerInformation(configuration);
var connectionContext = new ConnectionContext var connectionContext = new ConnectionContext
{ {
DateHeaderValueManager = new DateHeaderValueManager(), DateHeaderValueManager = new DateHeaderValueManager(),
ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerAddress = ServerAddress.FromUrl("http://localhost:5000"),
HttpComponentFactory = new HttpComponentFactory() ServerInformation = serverInformation,
}; HttpComponentFactory = new HttpComponentFactory(serverInformation)
};
var frame = new Frame<object>(application: null, context: connectionContext) var frame = new Frame<object>(application: null, context: connectionContext)
.InitializeHeaders(); .InitializeHeaders();
@ -47,11 +51,14 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[Fact] [Fact]
public void InitialEntriesCanBeCleared() public void InitialEntriesCanBeCleared()
{ {
var configuration = new ConfigurationBuilder().Build();
var serverInformation = new KestrelServerInformation(configuration);
var connectionContext = new ConnectionContext var connectionContext = new ConnectionContext
{ {
DateHeaderValueManager = new DateHeaderValueManager(), DateHeaderValueManager = new DateHeaderValueManager(),
ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerAddress = ServerAddress.FromUrl("http://localhost:5000"),
HttpComponentFactory = new HttpComponentFactory() ServerInformation = serverInformation,
HttpComponentFactory = new HttpComponentFactory(serverInformation)
}; };
var frame = new Frame<object>(application: null, context: connectionContext) var frame = new Frame<object>(application: null, context: connectionContext)
.InitializeHeaders(); .InitializeHeaders();

View File

@ -59,13 +59,11 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
handler.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; handler.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
#endif #endif
var serverAddress = $"https://localhost:{TestServer.GetNextPort()}/"; var serverAddress = $"https://localhost:{TestServer.GetNextPort()}/";
var serviceContext = new TestServiceContext() var serviceContext = new TestServiceContext(new HttpsConnectionFilter(
{
ConnectionFilter = new HttpsConnectionFilter(
new HttpsConnectionFilterOptions new HttpsConnectionFilterOptions
{ ServerCertificate = new X509Certificate2(@"TestResources/testCert.pfx", "testPassword")}, { ServerCertificate = new X509Certificate2(@"TestResources/testCert.pfx", "testPassword") },
new NoOpConnectionFilter()) new NoOpConnectionFilter())
}; );
using (var server = new TestServer(App, serviceContext, serverAddress)) using (var server = new TestServer(App, serviceContext, serverAddress))
{ {
@ -108,16 +106,14 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
#endif #endif
var serverAddress = $"https://localhost:{TestServer.GetNextPort()}/"; var serverAddress = $"https://localhost:{TestServer.GetNextPort()}/";
var serviceContext = new TestServiceContext() var serviceContext = new TestServiceContext(new HttpsConnectionFilter(
{
ConnectionFilter = new HttpsConnectionFilter(
new HttpsConnectionFilterOptions new HttpsConnectionFilterOptions
{ {
ServerCertificate = new X509Certificate2(@"TestResources/testCert.pfx", "testPassword"), ServerCertificate = new X509Certificate2(@"TestResources/testCert.pfx", "testPassword"),
ClientCertificateMode = ClientCertificateMode.RequireCertificate ClientCertificateMode = ClientCertificateMode.RequireCertificate
}, },
new NoOpConnectionFilter()) new NoOpConnectionFilter())
}; );
using (var server = new TestServer(App, serviceContext, serverAddress)) using (var server = new TestServer(App, serviceContext, serverAddress))
{ {
@ -157,16 +153,14 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
#endif #endif
var serverAddress = $"https://localhost:{TestServer.GetNextPort()}/"; var serverAddress = $"https://localhost:{TestServer.GetNextPort()}/";
var serviceContext = new TestServiceContext() var serviceContext = new TestServiceContext(new HttpsConnectionFilter(
{ new HttpsConnectionFilterOptions
ConnectionFilter = new HttpsConnectionFilter( {
new HttpsConnectionFilterOptions ServerCertificate = new X509Certificate2(@"TestResources/testCert.pfx", "testPassword"),
{ ClientCertificateMode = ClientCertificateMode.AllowCertificate
ServerCertificate = new X509Certificate2(@"TestResources/testCert.pfx", "testPassword"), },
ClientCertificateMode = ClientCertificateMode.AllowCertificate new NoOpConnectionFilter())
}, );
new NoOpConnectionFilter())
};
RequestDelegate app = context => RequestDelegate app = context =>
{ {
@ -209,17 +203,15 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
#endif #endif
var serverAddress = $"https://localhost:{TestServer.GetNextPort()}/"; var serverAddress = $"https://localhost:{TestServer.GetNextPort()}/";
var serviceContext = new TestServiceContext() var serviceContext = new TestServiceContext(new HttpsConnectionFilter(
{ new HttpsConnectionFilterOptions
ConnectionFilter = new HttpsConnectionFilter( {
new HttpsConnectionFilterOptions ServerCertificate = new X509Certificate2(@"TestResources/testCert.pfx", "testPassword"),
{ ClientCertificateMode = ClientCertificateMode.RequireCertificate,
ServerCertificate = new X509Certificate2(@"TestResources/testCert.pfx", "testPassword"), ClientCertificateValidation = (certificate, chain, sslPolicyErrors) => true
ClientCertificateMode = ClientCertificateMode.RequireCertificate, },
ClientCertificateValidation = (certificate, chain, sslPolicyErrors) => true new NoOpConnectionFilter())
}, );
new NoOpConnectionFilter())
};
RequestDelegate app = context => RequestDelegate app = context =>
{ {
@ -281,15 +273,14 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
#endif #endif
var serverAddress = $"https://localhost:{TestServer.GetNextPort()}/"; var serverAddress = $"https://localhost:{TestServer.GetNextPort()}/";
var serviceContext = new TestServiceContext() var serviceContext = new TestServiceContext(
{ new HttpsConnectionFilter(
ConnectionFilter = new HttpsConnectionFilter(
new HttpsConnectionFilterOptions new HttpsConnectionFilterOptions
{ {
ServerCertificate = new X509Certificate2(@"TestResources/testCert.pfx", "testPassword") ServerCertificate = new X509Certificate2(@"TestResources/testCert.pfx", "testPassword")
}, },
new NoOpConnectionFilter()) new NoOpConnectionFilter())
}; );
RequestDelegate app = context => context.Response.WriteAsync(context.Request.Scheme); RequestDelegate app = context => context.Response.WriteAsync(context.Request.Scheme);

View File

@ -86,19 +86,19 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
} }
[Theory] [Theory]
[InlineData(null, false)] [InlineData(null, 0)]
[InlineData("", false)] [InlineData("", 0)]
[InlineData("false", false)] [InlineData("0", 0)]
[InlineData("False", false)] [InlineData("00", 0)]
[InlineData("true", true)] [InlineData("0.0", 0)]
[InlineData("True", true)] [InlineData("1", 1)]
[InlineData("Foo", false)] [InlineData("16", 16)]
[InlineData("FooBar", false)] [InlineData("1000", 1000)]
public void SetReuseStreamsUsingConfiguration(string input, bool expected) public void SetMaxPooledStreamsUsingConfiguration(string input, int expected)
{ {
var values = new Dictionary<string, string> var values = new Dictionary<string, string>
{ {
{ "kestrel.reuseStreams", input } { "kestrel.maxPooledStreams", input }
}; };
var configuration = new ConfigurationBuilder() var configuration = new ConfigurationBuilder()
@ -107,7 +107,33 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
var information = new KestrelServerInformation(configuration); var information = new KestrelServerInformation(configuration);
Assert.Equal(expected, information.ReuseStreams); Assert.Equal(expected, information.PoolingParameters.MaxPooledStreams);
}
[Theory]
[InlineData(null, 0)]
[InlineData("", 0)]
[InlineData("0", 0)]
[InlineData("00", 0)]
[InlineData("0.0", 0)]
[InlineData("1", 1)]
[InlineData("16", 16)]
[InlineData("1000", 1000)]
public void SetMaxPooledHeadersUsingConfiguration(string input, int expected)
{
var values = new Dictionary<string, string>
{
{ "kestrel.maxPooledHeaders", input }
};
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(values)
.Build();
var information = new KestrelServerInformation(configuration);
Assert.Equal(expected, information.PoolingParameters.MaxPooledHeaders);
} }
private static int Clamp(int value, int min, int max) private static int Clamp(int value, int min, int max)

View File

@ -3,8 +3,10 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel; using Microsoft.AspNetCore.Server.Kestrel;
using Microsoft.AspNetCore.Server.Kestrel.Filter;
using Microsoft.AspNetCore.Server.Kestrel.Http; using Microsoft.AspNetCore.Server.Kestrel.Http;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Microsoft.Extensions.Configuration;
namespace Microsoft.AspNetCore.Server.KestrelTests namespace Microsoft.AspNetCore.Server.KestrelTests
{ {
@ -18,7 +20,16 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
Log = new TestKestrelTrace(); Log = new TestKestrelTrace();
ThreadPool = new LoggingThreadPool(Log); ThreadPool = new LoggingThreadPool(Log);
DateHeaderValueManager = new TestDateHeaderValueManager(); DateHeaderValueManager = new TestDateHeaderValueManager();
HttpComponentFactory = new HttpComponentFactory();
var configuration = new ConfigurationBuilder().Build();
ServerInformation = new KestrelServerInformation(configuration);
HttpComponentFactory = new HttpComponentFactory(ServerInformation);
}
public TestServiceContext(IConnectionFilter filter)
: base()
{
ServerInformation.ConnectionFilter = filter;
} }
public RequestDelegate App public RequestDelegate App