Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 4c68807

Browse files
committed
Split out ProcessorThreadCount, added InvariantCulture to TryParse
1 parent 8d6a999 commit 4c68807

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Globalization;
67
using Microsoft.AspNet.Server.Features;
78
using Microsoft.AspNet.Server.Kestrel.Filter;
89
using Microsoft.Extensions.Configuration;
@@ -26,6 +27,32 @@ public KestrelServerInformation(IConfiguration configuration)
2627

2728
public IConnectionFilter ConnectionFilter { get; set; }
2829

30+
private static int ProcessorThreadCount
31+
{
32+
get
33+
{
34+
// Actual core count would be a better number
35+
// rather than logical cores which includes hyper-threaded cores.
36+
// Divide by 2 for hyper-threading, and good defaults (still need threads to do webserving).
37+
var threadCount = Environment.ProcessorCount >> 1;
38+
39+
if (threadCount < 1)
40+
{
41+
// Ensure shifted value is at least one
42+
return 1;
43+
}
44+
45+
if (threadCount > 16)
46+
{
47+
// Receive Side Scaling RSS Processor count currently maxes out at 16
48+
// would be better to check the NIC's current hardware queues; but xplat...
49+
return 16;
50+
}
51+
52+
return threadCount;
53+
}
54+
}
55+
2956
private static ICollection<string> GetAddresses(IConfiguration configuration)
3057
{
3158
var addresses = new List<string>();
@@ -44,29 +71,18 @@ private static int GetThreadCount(IConfiguration configuration)
4471
{
4572
var threadCountString = configuration["kestrel.threadCount"];
4673

47-
int threadCount;
48-
if (string.IsNullOrEmpty(threadCountString) || !int.TryParse(threadCountString, out threadCount))
74+
if (string.IsNullOrEmpty(threadCountString))
4975
{
50-
// Actual core count would be a better number
51-
// rather than logical cores which includes hyper-threaded cores.
52-
// Divide by 2 for hyper-threading, and good defaults (still need threads to do webserving).
53-
threadCount = Environment.ProcessorCount >> 1;
54-
55-
if (threadCount < 1)
56-
{
57-
// Ensure shifted value is at least one
58-
return 1;
59-
}
76+
return ProcessorThreadCount;
77+
}
6078

61-
if (threadCount > 16)
62-
{
63-
// Receive Side Scaling RSS Processor count currently maxes out at 16
64-
// would be better to check the NIC's current hardware queues; but xplat...
65-
return 16;
66-
}
79+
int threadCount;
80+
if (int.TryParse(threadCountString, NumberStyles.Integer, CultureInfo.InvariantCulture, out threadCount))
81+
{
82+
return threadCount;
6783
}
6884

69-
return threadCount;
85+
return ProcessorThreadCount;
7086
}
7187

7288
private static bool GetNoDelay(IConfiguration configuration)

0 commit comments

Comments
 (0)