Add SocketTransportOption to enable/disable WaitForData (#19396)

This commit is contained in:
Tom Deseyn 2020-03-18 22:57:01 +01:00 committed by GitHub
parent 98ce12714a
commit cef755fd82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 5 deletions

View File

@ -31,5 +31,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
public long? MaxReadBufferSize { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public long? MaxReadBufferSize { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
public long? MaxWriteBufferSize { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public long? MaxWriteBufferSize { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
public bool NoDelay { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public bool NoDelay { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
public bool WaitForDataBeforeAllocatingBuffer { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
} }
} }

View File

@ -62,7 +62,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
PipeScheduler.ThreadPool, PipeScheduler.ThreadPool,
_trace, _trace,
_options.MaxReadBufferSize, _options.MaxReadBufferSize,
_options.MaxWriteBufferSize); _options.MaxWriteBufferSize,
_options.WaitForDataBeforeAllocatingBuffer);
socketConnection.Start(); socketConnection.Start();
return socketConnection; return socketConnection;

View File

@ -33,13 +33,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
private Task _processingTask; private Task _processingTask;
private readonly TaskCompletionSource<object> _waitForConnectionClosedTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously); private readonly TaskCompletionSource<object> _waitForConnectionClosedTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
private bool _connectionClosed; private bool _connectionClosed;
private readonly bool _waitForData;
internal SocketConnection(Socket socket, internal SocketConnection(Socket socket,
MemoryPool<byte> memoryPool, MemoryPool<byte> memoryPool,
PipeScheduler scheduler, PipeScheduler scheduler,
ISocketsTrace trace, ISocketsTrace trace,
long? maxReadBufferSize = null, long? maxReadBufferSize = null,
long? maxWriteBufferSize = null) long? maxWriteBufferSize = null,
bool waitForData = true)
{ {
Debug.Assert(socket != null); Debug.Assert(socket != null);
Debug.Assert(memoryPool != null); Debug.Assert(memoryPool != null);
@ -48,6 +50,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
_socket = socket; _socket = socket;
MemoryPool = memoryPool; MemoryPool = memoryPool;
_trace = trace; _trace = trace;
_waitForData = waitForData;
LocalEndPoint = _socket.LocalEndPoint; LocalEndPoint = _socket.LocalEndPoint;
RemoteEndPoint = _socket.RemoteEndPoint; RemoteEndPoint = _socket.RemoteEndPoint;
@ -186,8 +189,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
var input = Input; var input = Input;
while (true) while (true)
{ {
// Wait for data before allocating a buffer. if (_waitForData)
await _receiver.WaitForDataAsync(); {
// Wait for data before allocating a buffer.
await _receiver.WaitForDataAsync();
}
// Ensure we have some reasonable amount of buffer space // Ensure we have some reasonable amount of buffer space
var buffer = input.GetMemory(MinAllocBufferSize); var buffer = input.GetMemory(MinAllocBufferSize);

View File

@ -112,7 +112,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
acceptSocket.NoDelay = _options.NoDelay; acceptSocket.NoDelay = _options.NoDelay;
} }
var connection = new SocketConnection(acceptSocket, _memoryPool, _schedulers[_schedulerIndex], _trace, _options.MaxReadBufferSize, _options.MaxWriteBufferSize); var connection = new SocketConnection(acceptSocket, _memoryPool, _schedulers[_schedulerIndex], _trace,
_options.MaxReadBufferSize, _options.MaxWriteBufferSize, _options.WaitForDataBeforeAllocatingBuffer);
connection.Start(); connection.Start();

View File

@ -16,6 +16,14 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
/// </remarks> /// </remarks>
public int IOQueueCount { get; set; } = Math.Min(Environment.ProcessorCount, 16); public int IOQueueCount { get; set; } = Math.Min(Environment.ProcessorCount, 16);
/// <summary>
/// Wait until there is data available to allocate a buffer. Setting this to false can increase throughput at the cost of increased memory usage.
/// </summary>
/// <remarks>
/// Defaults to true.
/// </remarks>
public bool WaitForDataBeforeAllocatingBuffer { get; set; } = true;
/// <summary> /// <summary>
/// Set to false to enable Nagle's algorithm for all connections. /// Set to false to enable Nagle's algorithm for all connections.
/// </summary> /// </summary>