3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . Globalization ;
6
7
using Microsoft . AspNet . Server . Features ;
7
8
using Microsoft . AspNet . Server . Kestrel . Filter ;
8
9
using Microsoft . Extensions . Configuration ;
@@ -26,6 +27,32 @@ public KestrelServerInformation(IConfiguration configuration)
26
27
27
28
public IConnectionFilter ConnectionFilter { get ; set ; }
28
29
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
+
29
56
private static ICollection < string > GetAddresses ( IConfiguration configuration )
30
57
{
31
58
var addresses = new List < string > ( ) ;
@@ -44,29 +71,18 @@ private static int GetThreadCount(IConfiguration configuration)
44
71
{
45
72
var threadCountString = configuration [ "kestrel.threadCount" ] ;
46
73
47
- int threadCount ;
48
- if ( string . IsNullOrEmpty ( threadCountString ) || ! int . TryParse ( threadCountString , out threadCount ) )
74
+ if ( string . IsNullOrEmpty ( threadCountString ) )
49
75
{
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
+ }
60
78
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 ;
67
83
}
68
84
69
- return threadCount ;
85
+ return ProcessorThreadCount ;
70
86
}
71
87
72
88
private static bool GetNoDelay ( IConfiguration configuration )
0 commit comments