@@ -15,7 +15,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
15
15
/// </summary>
16
16
public sealed class SocketConnectionContextFactory : IDisposable
17
17
{
18
- private readonly MemoryPool < byte > _memoryPool ;
19
18
private readonly SocketConnectionFactoryOptions _options ;
20
19
private readonly ILogger _logger ;
21
20
private readonly int _settingsCount ;
@@ -43,7 +42,6 @@ public SocketConnectionContextFactory(SocketConnectionFactoryOptions options, IL
43
42
44
43
_options = options ;
45
44
_logger = logger ;
46
- _memoryPool = _options . MemoryPoolFactory ( ) ;
47
45
_settingsCount = _options . IOQueueCount ;
48
46
49
47
var maxReadBufferSize = _options . MaxReadBufferSize ?? 0 ;
@@ -56,33 +54,37 @@ public SocketConnectionContextFactory(SocketConnectionFactoryOptions options, IL
56
54
57
55
for ( var i = 0 ; i < _settingsCount ; i ++ )
58
56
{
57
+ var memoryPool = _options . MemoryPoolFactory ( ) ;
59
58
var transportScheduler = options . UnsafePreferInlineScheduling ? PipeScheduler . Inline : new IOQueue ( ) ;
60
59
// https://github.com/aspnet/KestrelHttpServer/issues/2573
61
60
var awaiterScheduler = OperatingSystem . IsWindows ( ) ? transportScheduler : PipeScheduler . Inline ;
62
61
63
62
_settings [ i ] = new QueueSettings ( )
64
63
{
65
64
Scheduler = transportScheduler ,
66
- InputOptions = new PipeOptions ( _memoryPool , applicationScheduler , transportScheduler , maxReadBufferSize , maxReadBufferSize / 2 , useSynchronizationContext : false ) ,
67
- OutputOptions = new PipeOptions ( _memoryPool , transportScheduler , applicationScheduler , maxWriteBufferSize , maxWriteBufferSize / 2 , useSynchronizationContext : false ) ,
68
- SocketSenderPool = new SocketSenderPool ( awaiterScheduler )
65
+ InputOptions = new PipeOptions ( memoryPool , applicationScheduler , transportScheduler , maxReadBufferSize , maxReadBufferSize / 2 , useSynchronizationContext : false ) ,
66
+ OutputOptions = new PipeOptions ( memoryPool , transportScheduler , applicationScheduler , maxWriteBufferSize , maxWriteBufferSize / 2 , useSynchronizationContext : false ) ,
67
+ SocketSenderPool = new SocketSenderPool ( awaiterScheduler ) ,
68
+ MemoryPool = memoryPool ,
69
69
} ;
70
70
}
71
71
}
72
72
else
73
73
{
74
+ var memoryPool = _options . MemoryPoolFactory ( ) ;
74
75
var transportScheduler = options . UnsafePreferInlineScheduling ? PipeScheduler . Inline : PipeScheduler . ThreadPool ;
75
76
// https://github.com/aspnet/KestrelHttpServer/issues/2573
76
77
var awaiterScheduler = OperatingSystem . IsWindows ( ) ? transportScheduler : PipeScheduler . Inline ;
77
78
_settings = new QueueSettings [ ]
78
79
{
79
- new QueueSettings ( )
80
- {
81
- Scheduler = transportScheduler ,
82
- InputOptions = new PipeOptions ( _memoryPool , applicationScheduler , transportScheduler , maxReadBufferSize , maxReadBufferSize / 2 , useSynchronizationContext : false ) ,
83
- OutputOptions = new PipeOptions ( _memoryPool , transportScheduler , applicationScheduler , maxWriteBufferSize , maxWriteBufferSize / 2 , useSynchronizationContext : false ) ,
84
- SocketSenderPool = new SocketSenderPool ( awaiterScheduler )
85
- }
80
+ new QueueSettings ( )
81
+ {
82
+ Scheduler = transportScheduler ,
83
+ InputOptions = new PipeOptions ( memoryPool , applicationScheduler , transportScheduler , maxReadBufferSize , maxReadBufferSize / 2 , useSynchronizationContext : false ) ,
84
+ OutputOptions = new PipeOptions ( memoryPool , transportScheduler , applicationScheduler , maxWriteBufferSize , maxWriteBufferSize / 2 , useSynchronizationContext : false ) ,
85
+ SocketSenderPool = new SocketSenderPool ( awaiterScheduler ) ,
86
+ MemoryPool = memoryPool ,
87
+ }
86
88
} ;
87
89
_settingsCount = 1 ;
88
90
}
@@ -98,7 +100,7 @@ public ConnectionContext Create(Socket socket)
98
100
var setting = _settings [ Interlocked . Increment ( ref _settingsIndex ) % _settingsCount ] ;
99
101
100
102
var connection = new SocketConnection ( socket ,
101
- _memoryPool ,
103
+ setting . MemoryPool ,
102
104
setting . Scheduler ,
103
105
_logger ,
104
106
setting . SocketSenderPool ,
@@ -113,13 +115,11 @@ public ConnectionContext Create(Socket socket)
113
115
/// <inheritdoc />
114
116
public void Dispose ( )
115
117
{
116
- // Dispose the memory pool
117
- _memoryPool . Dispose ( ) ;
118
-
119
- // Dispose any pooled senders
118
+ // Dispose any pooled senders and memory pools
120
119
foreach ( var setting in _settings )
121
120
{
122
121
setting . SocketSenderPool . Dispose ( ) ;
122
+ setting . MemoryPool . Dispose ( ) ;
123
123
}
124
124
}
125
125
@@ -129,5 +129,6 @@ private sealed class QueueSettings
129
129
public PipeOptions InputOptions { get ; init ; } = default ! ;
130
130
public PipeOptions OutputOptions { get ; init ; } = default ! ;
131
131
public SocketSenderPool SocketSenderPool { get ; init ; } = default ! ;
132
+ public MemoryPool < byte > MemoryPool { get ; init ; } = default ! ;
132
133
}
133
134
}
0 commit comments