Resolve input/output via IDuplexPipe interface once (#6371)

This commit is contained in:
Ben Adams 2019-01-10 06:25:22 +01:00 committed by David Fowler
parent 0f65d44482
commit ad11f890ef
2 changed files with 11 additions and 6 deletions

View File

@ -54,10 +54,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
_context.TimeoutControl,
this);
Input = _context.Transport.Input;
Output = _http1Output;
}
public PipeReader Input => _context.Transport.Input;
public PipeReader Input { get; }
public bool RequestTimedOut => _requestTimedOut;

View File

@ -154,13 +154,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
private async Task ProcessReceives()
{
// Resolve `input` PipeWriter via the IDuplexPipe interface prior to loop start for performance.
var input = Input;
while (true)
{
// Wait for data before allocating a buffer.
await _receiver.WaitForDataAsync();
// Ensure we have some reasonable amount of buffer space
var buffer = Input.GetMemory(MinAllocBufferSize);
var buffer = input.GetMemory(MinAllocBufferSize);
var bytesReceived = await _receiver.ReceiveAsync(buffer);
@ -171,9 +173,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
break;
}
Input.Advance(bytesReceived);
input.Advance(bytesReceived);
var flushTask = Input.FlushAsync();
var flushTask = input.FlushAsync();
var paused = !flushTask.IsCompleted;
@ -238,9 +240,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
private async Task ProcessSends()
{
// Resolve `output` PipeReader via the IDuplexPipe interface prior to loop start for performance.
var output = Output;
while (true)
{
var result = await Output.ReadAsync();
var result = await output.ReadAsync();
if (result.IsCanceled)
{
@ -256,7 +260,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
await _sender.SendAsync(buffer);
}
Output.AdvanceTo(end);
output.AdvanceTo(end);
if (isCompleted)
{