Skip to content

Commit 36e1456

Browse files
authored
Partition more ConcurrentQueues in Kestrel (#42237)
1 parent c381a6c commit 36e1456

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/Servers/Kestrel/Transport.Sockets/src/SocketConnectionContextFactory.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
1515
/// </summary>
1616
public sealed class SocketConnectionContextFactory : IDisposable
1717
{
18-
private readonly MemoryPool<byte> _memoryPool;
1918
private readonly SocketConnectionFactoryOptions _options;
2019
private readonly ILogger _logger;
2120
private readonly int _settingsCount;
@@ -43,7 +42,6 @@ public SocketConnectionContextFactory(SocketConnectionFactoryOptions options, IL
4342

4443
_options = options;
4544
_logger = logger;
46-
_memoryPool = _options.MemoryPoolFactory();
4745
_settingsCount = _options.IOQueueCount;
4846

4947
var maxReadBufferSize = _options.MaxReadBufferSize ?? 0;
@@ -56,33 +54,37 @@ public SocketConnectionContextFactory(SocketConnectionFactoryOptions options, IL
5654

5755
for (var i = 0; i < _settingsCount; i++)
5856
{
57+
var memoryPool = _options.MemoryPoolFactory();
5958
var transportScheduler = options.UnsafePreferInlineScheduling ? PipeScheduler.Inline : new IOQueue();
6059
// https://github.com/aspnet/KestrelHttpServer/issues/2573
6160
var awaiterScheduler = OperatingSystem.IsWindows() ? transportScheduler : PipeScheduler.Inline;
6261

6362
_settings[i] = new QueueSettings()
6463
{
6564
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,
6969
};
7070
}
7171
}
7272
else
7373
{
74+
var memoryPool = _options.MemoryPoolFactory();
7475
var transportScheduler = options.UnsafePreferInlineScheduling ? PipeScheduler.Inline : PipeScheduler.ThreadPool;
7576
// https://github.com/aspnet/KestrelHttpServer/issues/2573
7677
var awaiterScheduler = OperatingSystem.IsWindows() ? transportScheduler : PipeScheduler.Inline;
7778
_settings = new QueueSettings[]
7879
{
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+
}
8688
};
8789
_settingsCount = 1;
8890
}
@@ -98,7 +100,7 @@ public ConnectionContext Create(Socket socket)
98100
var setting = _settings[Interlocked.Increment(ref _settingsIndex) % _settingsCount];
99101

100102
var connection = new SocketConnection(socket,
101-
_memoryPool,
103+
setting.MemoryPool,
102104
setting.Scheduler,
103105
_logger,
104106
setting.SocketSenderPool,
@@ -113,13 +115,11 @@ public ConnectionContext Create(Socket socket)
113115
/// <inheritdoc />
114116
public void Dispose()
115117
{
116-
// Dispose the memory pool
117-
_memoryPool.Dispose();
118-
119-
// Dispose any pooled senders
118+
// Dispose any pooled senders and memory pools
120119
foreach (var setting in _settings)
121120
{
122121
setting.SocketSenderPool.Dispose();
122+
setting.MemoryPool.Dispose();
123123
}
124124
}
125125

@@ -129,5 +129,6 @@ private sealed class QueueSettings
129129
public PipeOptions InputOptions { get; init; } = default!;
130130
public PipeOptions OutputOptions { get; init; } = default!;
131131
public SocketSenderPool SocketSenderPool { get; init; } = default!;
132+
public MemoryPool<byte> MemoryPool { get; init; } = default!;
132133
}
133134
}

0 commit comments

Comments
 (0)