Skip to content

Commit cef755f

Browse files
authored
Add SocketTransportOption to enable/disable WaitForData (#19396)
1 parent 98ce127 commit cef755f

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

src/Servers/Kestrel/Transport.Sockets/ref/Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.netcoreapp.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ public SocketTransportOptions() { }
3131
public long? MaxReadBufferSize { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
3232
public long? MaxWriteBufferSize { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
3333
public bool NoDelay { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
34+
public bool WaitForDataBeforeAllocatingBuffer { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
3435
}
3536
}

src/Servers/Kestrel/Transport.Sockets/src/Client/SocketConnectionFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public async ValueTask<ConnectionContext> ConnectAsync(EndPoint endpoint, Cancel
6262
PipeScheduler.ThreadPool,
6363
_trace,
6464
_options.MaxReadBufferSize,
65-
_options.MaxWriteBufferSize);
65+
_options.MaxWriteBufferSize,
66+
_options.WaitForDataBeforeAllocatingBuffer);
6667

6768
socketConnection.Start();
6869
return socketConnection;

src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ internal sealed class SocketConnection : TransportConnection
3333
private Task _processingTask;
3434
private readonly TaskCompletionSource<object> _waitForConnectionClosedTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
3535
private bool _connectionClosed;
36+
private readonly bool _waitForData;
3637

3738
internal SocketConnection(Socket socket,
3839
MemoryPool<byte> memoryPool,
3940
PipeScheduler scheduler,
4041
ISocketsTrace trace,
4142
long? maxReadBufferSize = null,
42-
long? maxWriteBufferSize = null)
43+
long? maxWriteBufferSize = null,
44+
bool waitForData = true)
4345
{
4446
Debug.Assert(socket != null);
4547
Debug.Assert(memoryPool != null);
@@ -48,6 +50,7 @@ internal SocketConnection(Socket socket,
4850
_socket = socket;
4951
MemoryPool = memoryPool;
5052
_trace = trace;
53+
_waitForData = waitForData;
5154

5255
LocalEndPoint = _socket.LocalEndPoint;
5356
RemoteEndPoint = _socket.RemoteEndPoint;
@@ -186,8 +189,11 @@ private async Task ProcessReceives()
186189
var input = Input;
187190
while (true)
188191
{
189-
// Wait for data before allocating a buffer.
190-
await _receiver.WaitForDataAsync();
192+
if (_waitForData)
193+
{
194+
// Wait for data before allocating a buffer.
195+
await _receiver.WaitForDataAsync();
196+
}
191197

192198
// Ensure we have some reasonable amount of buffer space
193199
var buffer = input.GetMemory(MinAllocBufferSize);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public async ValueTask<ConnectionContext> AcceptAsync(CancellationToken cancella
112112
acceptSocket.NoDelay = _options.NoDelay;
113113
}
114114

115-
var connection = new SocketConnection(acceptSocket, _memoryPool, _schedulers[_schedulerIndex], _trace, _options.MaxReadBufferSize, _options.MaxWriteBufferSize);
115+
var connection = new SocketConnection(acceptSocket, _memoryPool, _schedulers[_schedulerIndex], _trace,
116+
_options.MaxReadBufferSize, _options.MaxWriteBufferSize, _options.WaitForDataBeforeAllocatingBuffer);
116117

117118
connection.Start();
118119

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ public class SocketTransportOptions
1616
/// </remarks>
1717
public int IOQueueCount { get; set; } = Math.Min(Environment.ProcessorCount, 16);
1818

19+
/// <summary>
20+
/// Wait until there is data available to allocate a buffer. Setting this to false can increase throughput at the cost of increased memory usage.
21+
/// </summary>
22+
/// <remarks>
23+
/// Defaults to true.
24+
/// </remarks>
25+
public bool WaitForDataBeforeAllocatingBuffer { get; set; } = true;
26+
1927
/// <summary>
2028
/// Set to false to enable Nagle's algorithm for all connections.
2129
/// </summary>

0 commit comments

Comments
 (0)