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.
if (ConnectionFilter == null)
if (ServerInformation.ConnectionFilter == null)
{
SocketInput = _rawSocketInput;
SocketOutput = _rawSocketOutput;
@ -81,7 +81,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
try
{
ConnectionFilter.OnConnectionAsync(_filterContext).ContinueWith((task, state) =>
ServerInformation.ConnectionFilter.OnConnectionAsync(_filterContext).ContinueWith((task, 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 _onCompletedSync = new Object();
protected bool _poolingPermitted = true;
private Headers _frameHeaders;
private Streams _frameStreams;
@ -279,7 +280,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
DuplexStream = null;
var frameStreams = _frameStreams;
_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)
{
// Error occurred, do not return components to pool
_poolingPermitted = false;
Log.LogWarning(0, ex, "Connection processing ended abnormally");
}
finally
{
// Error occurred, do not return components to pool
ResetComponents(poolingPermitted: false);
try
{
ResetComponents(poolingPermitted: _poolingPermitted);
_abortedCts = null;
// 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);
socket.Init(Thread.Loop, Thread.QueueCloseHandle);
socket.NoDelay(NoDelay);
socket.NoDelay(ServerInformation.NoDelay);
socket.Bind(ServerAddress);
socket.Listen(Constants.ListenBacklog, (stream, status, error, state) => ConnectionCallback(stream, status, error, state), this);
return socket;
@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Http
try
{
acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle);
acceptSocket.NoDelay(NoDelay);
acceptSocket.NoDelay(ServerInformation.NoDelay);
listenSocket.Accept(acceptSocket);
DispatchConnection(acceptSocket);

View File

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

View File

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

View File

@ -12,17 +12,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel
bool NoDelay { get; set; }
/// <summary>
/// Gets or sets a flag that instructs <seealso cref="KestrelServer"/> whether it is safe to
/// reuse the Request and Response <seealso cref="System.IO.Stream"/> objects
/// Gets or sets values that instruct <seealso cref="KestrelServer"/> whether it is safe to
/// pool the Request and Response <seealso cref="System.IO.Stream"/> objects, Headers etc
/// 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.
/// It is false by default.
/// When these values are greater than zero it is not safe to retain references to feature components after this event has fired.
/// They are zero by default.
/// </summary>
/// <remarks>
/// 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; }
KestrelServerPoolingParameters PoolingParameters { get; }
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.
using System.Collections.Concurrent;
using Microsoft.AspNetCore.Server.Kestrel.Http;
using System.Text;
using Microsoft.AspNetCore.Server.Kestrel.Http;
namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
{
class HttpComponentFactory : IHttpComponentFactory
{
private const int _maxPooledComponents = 128;
private ConcurrentQueue<Streams> _streamPool = new ConcurrentQueue<Streams>();
private ConcurrentQueue<Headers> _headerPool = new ConcurrentQueue<Headers>();
public IKestrelServerInformation ServerInformation { get; set; }
public HttpComponentFactory(IKestrelServerInformation serverInformation)
{
ServerInformation = serverInformation;
}
public Streams CreateStreams(FrameContext owner)
{
Streams streams;
@ -29,7 +36,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
public void DisposeStreams(Streams streams, bool poolingPermitted)
{
if (poolingPermitted && _streamPool.Count < _maxPooledComponents)
if (poolingPermitted && _streamPool.Count < ServerInformation.PoolingParameters.MaxPooledStreams)
{
streams.Uninitialize();
@ -53,7 +60,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
public void DisposeHeaders(Headers headers, bool poolingPermitted)
{
if (poolingPermitted && _headerPool.Count < _maxPooledComponents)
if (poolingPermitted && _headerPool.Count < ServerInformation.PoolingParameters.MaxPooledHeaders)
{
headers.Uninitialize();

View File

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

View File

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

View File

@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
Addresses = GetAddresses(configuration);
ThreadCount = GetThreadCount(configuration);
NoDelay = GetNoDelay(configuration);
ReuseStreams = GetReuseStreams(configuration);
PoolingParameters = new KestrelServerPoolingParameters(configuration);
}
public ICollection<string> Addresses { get; }
@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
public bool NoDelay { get; set; }
public bool ReuseStreams { get; set; }
public KestrelServerPoolingParameters PoolingParameters { get; }
public IConnectionFilter ConnectionFilter { get; set; }
@ -110,18 +110,5 @@ namespace Microsoft.AspNetCore.Server.Kestrel
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.Http.Features;
using Microsoft.AspNetCore.Server.Features;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
@ -28,8 +29,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel
public IServer CreateServer(IConfiguration configuration)
{
var information = new KestrelServerInformation(configuration);
var componentFactory = new HttpComponentFactory(information);
var serverFeatures = new FeatureCollection();
serverFeatures.Set<IKestrelServerInformation>(information);
serverFeatures.Set<IHttpComponentFactory>(componentFactory);
serverFeatures.Set<IServerAddressesFeature>(information);
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;
FrameFactory = context.FrameFactory;
DateHeaderValueManager = context.DateHeaderValueManager;
ConnectionFilter = context.ConnectionFilter;
NoDelay = context.NoDelay;
ReuseStreams = context.ReuseStreams;
ServerInformation = context.ServerInformation;
HttpComponentFactory = context.HttpComponentFactory;
}
@ -40,11 +38,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
public DateHeaderValueManager DateHeaderValueManager { get; set; }
public IConnectionFilter ConnectionFilter { get; set; }
public bool NoDelay { get; set; }
public bool ReuseStreams { get; set; }
public IKestrelServerInformation ServerInformation { get; set; }
internal IHttpComponentFactory HttpComponentFactory { get; set; }
}

View File

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

View File

@ -36,10 +36,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
public async Task CanReadAndWriteWithRewritingConnectionFilter()
{
var filter = new RewritingConnectionFilter();
var serviceContext = new TestServiceContext()
{
ConnectionFilter = filter
};
var serviceContext = new TestServiceContext(filter);
var sendString = "POST / HTTP/1.0\r\n\r\nHello World?";
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.")]
public async Task CanReadAndWriteWithAsyncConnectionFilter()
{
var serviceContext = new TestServiceContext()
{
ConnectionFilter = new AsyncConnectionFilter()
};
var serviceContext = new TestServiceContext(new AsyncConnectionFilter());
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.")]
public async Task ThrowingSynchronousConnectionFilterDoesNotCrashServer()
{
var serviceContext = new TestServiceContext()
{
ConnectionFilter = new ThrowingConnectionFilter()
};
var serviceContext = new TestServiceContext(new ThrowingConnectionFilter());
using (var server = new TestServer(App, serviceContext))
{

View File

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

View File

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

View File

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

View File

@ -86,19 +86,19 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
}
[Theory]
[InlineData(null, false)]
[InlineData("", false)]
[InlineData("false", false)]
[InlineData("False", false)]
[InlineData("true", true)]
[InlineData("True", true)]
[InlineData("Foo", false)]
[InlineData("FooBar", false)]
public void SetReuseStreamsUsingConfiguration(string input, bool expected)
[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 SetMaxPooledStreamsUsingConfiguration(string input, int expected)
{
var values = new Dictionary<string, string>
{
{ "kestrel.reuseStreams", input }
{ "kestrel.maxPooledStreams", input }
};
var configuration = new ConfigurationBuilder()
@ -107,7 +107,33 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
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)

View File

@ -3,8 +3,10 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel;
using Microsoft.AspNetCore.Server.Kestrel.Filter;
using Microsoft.AspNetCore.Server.Kestrel.Http;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Microsoft.Extensions.Configuration;
namespace Microsoft.AspNetCore.Server.KestrelTests
{
@ -18,7 +20,16 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
Log = new TestKestrelTrace();
ThreadPool = new LoggingThreadPool(Log);
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