Initialize ThreadCount as early as possible

This will allow you to inspect the property
in the Configure method.

Closes #404
This commit is contained in:
Kristian Hellang 2015-11-23 00:05:10 +01:00
parent 1c320d7a74
commit 5eaa7e51c8
3 changed files with 50 additions and 34 deletions

View File

@ -73,34 +73,13 @@ namespace Microsoft.AspNet.Server.Kestrel
_disposables.Push(engine); _disposables.Push(engine);
_disposables.Push(dateHeaderValueManager); _disposables.Push(dateHeaderValueManager);
// Actual core count would be a better number var threadCount = information.ThreadCount;
// rather than logical cores which includes hyper-threaded cores.
// Divide by 2 for hyper-threading, and good defaults (still need threads to do webserving).
// Can be user overriden using IKestrelServerInformation.ThreadCount
var threadCount = Environment.ProcessorCount >> 1;
if (threadCount < 1) if (threadCount <= 0)
{ {
// Ensure shifted value is at least one throw new ArgumentOutOfRangeException(nameof(threadCount),
threadCount = 1; threadCount,
} "ThreadCount must be positive.");
else if (threadCount > 16)
{
// Receive Side Scaling RSS Processor count currently maxes out at 16
// would be better to check the NIC's current hardware queues; but xplat...
threadCount = 16;
}
if (information.ThreadCount < 0)
{
throw new ArgumentOutOfRangeException(nameof(information.ThreadCount),
information.ThreadCount,
"ThreadCount cannot be negative");
}
else if (information.ThreadCount > 0)
{
// ThreadCount has been user set, use that value
threadCount = information.ThreadCount;
} }
engine.Start(threadCount); engine.Start(threadCount);

View File

@ -11,21 +11,33 @@ namespace Microsoft.AspNet.Server.Kestrel
{ {
public class KestrelServerInformation : IKestrelServerInformation, IServerAddressesFeature public class KestrelServerInformation : IKestrelServerInformation, IServerAddressesFeature
{ {
public ICollection<string> Addresses { get; } = new List<string>(); public KestrelServerInformation(IConfiguration configuration, int threadCount)
{
Addresses = GetAddresses(configuration);
ThreadCount = threadCount;
NoDelay = true;
}
public ICollection<string> Addresses { get; }
public int ThreadCount { get; set; } public int ThreadCount { get; set; }
public bool NoDelay { get; set; } = true; public bool NoDelay { get; set; }
public IConnectionFilter ConnectionFilter { get; set; } public IConnectionFilter ConnectionFilter { get; set; }
public void Initialize(IConfiguration configuration) private static ICollection<string> GetAddresses(IConfiguration configuration)
{ {
var urls = configuration["server.urls"] ?? string.Empty; var addresses = new List<string>();
foreach (var url in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
var urls = configuration["server.urls"];
if (!string.IsNullOrEmpty(urls))
{ {
Addresses.Add(url); addresses.AddRange(urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
} }
return addresses;
} }
} }
} }

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Hosting.Server; using Microsoft.AspNet.Hosting.Server;
using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features;
@ -26,12 +27,36 @@ namespace Microsoft.AspNet.Server.Kestrel
public IServer CreateServer(IConfiguration configuration) public IServer CreateServer(IConfiguration configuration)
{ {
var information = new KestrelServerInformation(); var threadCount = GetThreadCount();
information.Initialize(configuration); var information = new KestrelServerInformation(configuration, threadCount);
var serverFeatures = new FeatureCollection(); var serverFeatures = new FeatureCollection();
serverFeatures.Set<IKestrelServerInformation>(information); serverFeatures.Set<IKestrelServerInformation>(information);
serverFeatures.Set<IServerAddressesFeature>(information); serverFeatures.Set<IServerAddressesFeature>(information);
return new KestrelServer(serverFeatures, _appLifetime, _loggerFactory.CreateLogger("Microsoft.AspNet.Server.Kestrel")); return new KestrelServer(serverFeatures, _appLifetime, _loggerFactory.CreateLogger("Microsoft.AspNet.Server.Kestrel"));
} }
private static int GetThreadCount()
{
// Actual core count would be a better number
// rather than logical cores which includes hyper-threaded cores.
// Divide by 2 for hyper-threading, and good defaults (still need threads to do webserving).
// Can be user overriden using IKestrelServerInformation.ThreadCount
var threadCount = Environment.ProcessorCount >> 1;
if (threadCount < 1)
{
// Ensure shifted value is at least one
return 1;
}
if (threadCount > 16)
{
// Receive Side Scaling RSS Processor count currently maxes out at 16
// would be better to check the NIC's current hardware queues; but xplat...
return 16;
}
return threadCount;
}
} }
} }