Refactor FrameTests and rename SocketInput SocketOutput properties (#1229)

This commit is contained in:
Pavel Krymets 2016-11-22 16:36:36 -08:00 committed by GitHub
parent 2bf5a966cd
commit e55c62444b
10 changed files with 330 additions and 1120 deletions

View File

@ -56,8 +56,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
_bufferSizeControl = new BufferSizeControl(ServerOptions.Limits.MaxRequestBufferSize.Value, this, Thread); _bufferSizeControl = new BufferSizeControl(ServerOptions.Limits.MaxRequestBufferSize.Value, this, Thread);
} }
SocketInput = new SocketInput(Thread.Memory, ThreadPool, _bufferSizeControl); Input = new SocketInput(Thread.Memory, ThreadPool, _bufferSizeControl);
SocketOutput = new SocketOutput(Thread, _socket, this, ConnectionId, Log, ThreadPool); Output = new SocketOutput(Thread, _socket, this, ConnectionId, Log, ThreadPool);
var tcpHandle = _socket as UvTcpHandle; var tcpHandle = _socket as UvTcpHandle;
if (tcpHandle != null) if (tcpHandle != null)
@ -95,7 +95,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
} }
else else
{ {
_libuvStream = new LibuvStream(SocketInput, SocketOutput); _libuvStream = new LibuvStream(Input, Output);
_filterContext = new ConnectionFilterContext _filterContext = new ConnectionFilterContext
{ {
@ -136,7 +136,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
public Task StopAsync() public Task StopAsync()
{ {
_frame.StopAsync(); _frame.StopAsync();
_frame.SocketInput.CompleteAwaiting(); _frame.Input.CompleteAwaiting();
return _socketClosedTcs.Task; return _socketClosedTcs.Task;
} }
@ -169,7 +169,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
} }
}, this); }, this);
SocketInput.Dispose(); Input.Dispose();
_socketClosedTcs.TrySetResult(null); _socketClosedTcs.TrySetResult(null);
} }
@ -197,8 +197,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
{ {
_filteredStreamAdapter = new FilteredStreamAdapter(ConnectionId, _filterContext.Connection, Thread.Memory, Log, ThreadPool, _bufferSizeControl); _filteredStreamAdapter = new FilteredStreamAdapter(ConnectionId, _filterContext.Connection, Thread.Memory, Log, ThreadPool, _bufferSizeControl);
_frame.SocketInput = _filteredStreamAdapter.SocketInput; _frame.Input = _filteredStreamAdapter.SocketInput;
_frame.SocketOutput = _filteredStreamAdapter.SocketOutput; _frame.Output = _filteredStreamAdapter.SocketOutput;
_readInputTask = _filteredStreamAdapter.ReadInputAsync(); _readInputTask = _filteredStreamAdapter.ReadInputAsync();
} }
@ -214,7 +214,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
private Libuv.uv_buf_t OnAlloc(UvStreamHandle handle, int suggestedSize) private Libuv.uv_buf_t OnAlloc(UvStreamHandle handle, int suggestedSize)
{ {
var result = SocketInput.IncomingStart(); var result = Input.IncomingStart();
return handle.Libuv.buf_init( return handle.Libuv.buf_init(
result.DataArrayPtr + result.End, result.DataArrayPtr + result.End,
@ -234,7 +234,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
// there is no data to be read right now. // there is no data to be read right now.
// See the note at http://docs.libuv.org/en/v1.x/stream.html#c.uv_read_cb. // See the note at http://docs.libuv.org/en/v1.x/stream.html#c.uv_read_cb.
// We need to clean up whatever was allocated by OnAlloc. // We need to clean up whatever was allocated by OnAlloc.
SocketInput.IncomingDeferred(); Input.IncomingDeferred();
return; return;
} }
@ -276,7 +276,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
error = new IOException(uvError.Message, uvError); error = new IOException(uvError.Message, uvError);
} }
SocketInput.IncomingComplete(readCount, error); Input.IncomingComplete(readCount, error);
if (errorDone) if (errorDone)
{ {
@ -302,7 +302,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
// ReadStart() can throw a UvException in some cases (e.g. socket is no longer connected). // ReadStart() can throw a UvException in some cases (e.g. socket is no longer connected).
// This should be treated the same as OnRead() seeing a "normalDone" condition. // This should be treated the same as OnRead() seeing a "normalDone" condition.
Log.ConnectionReadFin(ConnectionId); Log.ConnectionReadFin(ConnectionId);
SocketInput.IncomingComplete(0, null); Input.IncomingComplete(0, null);
} }
} }
@ -316,7 +316,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
case ProduceEndType.SocketShutdown: case ProduceEndType.SocketShutdown:
case ProduceEndType.SocketDisconnect: case ProduceEndType.SocketDisconnect:
Log.ConnectionDisconnect(ConnectionId); Log.ConnectionDisconnect(ConnectionId);
((SocketOutput)SocketOutput).End(endType); ((SocketOutput)Output).End(endType);
break; break;
} }
} }

View File

@ -20,9 +20,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
public ListenerContext ListenerContext { get; set; } public ListenerContext ListenerContext { get; set; }
public SocketInput SocketInput { get; set; } public SocketInput Input { get; set; }
public ISocketOutput SocketOutput { get; set; } public ISocketOutput Output { get; set; }
public IConnectionControl ConnectionControl { get; set; } public IConnectionControl ConnectionControl { get; set; }

View File

@ -82,8 +82,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
public Frame(ConnectionContext context) public Frame(ConnectionContext context)
{ {
ConnectionContext = context; ConnectionContext = context;
SocketInput = context.SocketInput; Input = context.Input;
SocketOutput = context.SocketOutput; Output = context.Output;
ServerOptions = context.ListenerContext.ServiceContext.ServerOptions; ServerOptions = context.ListenerContext.ServiceContext.ServerOptions;
@ -95,8 +95,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
} }
public ConnectionContext ConnectionContext { get; } public ConnectionContext ConnectionContext { get; }
public SocketInput SocketInput { get; set; } public SocketInput Input { get; set; }
public ISocketOutput SocketOutput { get; set; } public ISocketOutput Output { get; set; }
public Action<IFeatureCollection> PrepareRequest public Action<IFeatureCollection> PrepareRequest
{ {
get get
@ -531,13 +531,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
public void Flush() public void Flush()
{ {
ProduceStartAndFireOnStarting().GetAwaiter().GetResult(); ProduceStartAndFireOnStarting().GetAwaiter().GetResult();
SocketOutput.Flush(); Output.Flush();
} }
public async Task FlushAsync(CancellationToken cancellationToken) public async Task FlushAsync(CancellationToken cancellationToken)
{ {
await ProduceStartAndFireOnStarting(); await ProduceStartAndFireOnStarting();
await SocketOutput.FlushAsync(cancellationToken); await Output.FlushAsync(cancellationToken);
} }
public void Write(ArraySegment<byte> data) public void Write(ArraySegment<byte> data)
@ -564,7 +564,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
} }
else else
{ {
SocketOutput.Write(data); Output.Write(data);
} }
} }
else else
@ -599,7 +599,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
} }
else else
{ {
return SocketOutput.WriteAsync(data, cancellationToken: cancellationToken); return Output.WriteAsync(data, cancellationToken: cancellationToken);
} }
} }
else else
@ -631,7 +631,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
} }
else else
{ {
await SocketOutput.WriteAsync(data, cancellationToken: cancellationToken); await Output.WriteAsync(data, cancellationToken: cancellationToken);
} }
} }
else else
@ -675,17 +675,17 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
private void WriteChunked(ArraySegment<byte> data) private void WriteChunked(ArraySegment<byte> data)
{ {
SocketOutput.Write(data, chunk: true); Output.Write(data, chunk: true);
} }
private Task WriteChunkedAsync(ArraySegment<byte> data, CancellationToken cancellationToken) private Task WriteChunkedAsync(ArraySegment<byte> data, CancellationToken cancellationToken)
{ {
return SocketOutput.WriteAsync(data, chunk: true, cancellationToken: cancellationToken); return Output.WriteAsync(data, chunk: true, cancellationToken: cancellationToken);
} }
private Task WriteChunkedResponseSuffix() private Task WriteChunkedResponseSuffix()
{ {
return SocketOutput.WriteAsync(_endChunkedResponseBytes); return Output.WriteAsync(_endChunkedResponseBytes);
} }
private static ArraySegment<byte> CreateAsciiByteArraySegment(string text) private static ArraySegment<byte> CreateAsciiByteArraySegment(string text)
@ -706,7 +706,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
RequestHeaders.TryGetValue("Expect", out expect) && RequestHeaders.TryGetValue("Expect", out expect) &&
(expect.FirstOrDefault() ?? "").Equals("100-continue", StringComparison.OrdinalIgnoreCase)) (expect.FirstOrDefault() ?? "").Equals("100-continue", StringComparison.OrdinalIgnoreCase))
{ {
SocketOutput.Write(_continueBytes); Output.Write(_continueBytes);
} }
} }
@ -809,7 +809,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
ProduceStart(appCompleted: true); ProduceStart(appCompleted: true);
// Force flush // Force flush
await SocketOutput.FlushAsync(); await Output.FlushAsync();
await WriteSuffix(); await WriteSuffix();
} }
@ -856,7 +856,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
var hasTransferEncoding = responseHeaders.HasTransferEncoding; var hasTransferEncoding = responseHeaders.HasTransferEncoding;
var transferCoding = FrameHeaders.GetFinalTransferCoding(responseHeaders.HeaderTransferEncoding); var transferCoding = FrameHeaders.GetFinalTransferCoding(responseHeaders.HeaderTransferEncoding);
var end = SocketOutput.ProducingStart(); var end = Output.ProducingStart();
if (_keepAlive && hasConnection) if (_keepAlive && hasConnection)
{ {
@ -944,7 +944,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
responseHeaders.CopyTo(ref end); responseHeaders.CopyTo(ref end);
end.CopyFrom(_bytesEndHeaders, 0, _bytesEndHeaders.Length); end.CopyFrom(_bytesEndHeaders, 0, _bytesEndHeaders.Length);
SocketOutput.ProducingComplete(end); Output.ProducingComplete(end);
} }
public RequestLineStatus TakeStartLine(SocketInput input) public RequestLineStatus TakeStartLine(SocketInput input)

View File

@ -41,20 +41,20 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
while (!_requestProcessingStopping) while (!_requestProcessingStopping)
{ {
requestLineStatus = TakeStartLine(SocketInput); requestLineStatus = TakeStartLine(Input);
if (requestLineStatus == RequestLineStatus.Done) if (requestLineStatus == RequestLineStatus.Done)
{ {
break; break;
} }
if (SocketInput.CheckFinOrThrow()) if (Input.CheckFinOrThrow())
{ {
// We need to attempt to consume start lines and headers even after // We need to attempt to consume start lines and headers even after
// SocketInput.RemoteIntakeFin is set to true to ensure we don't close a // SocketInput.RemoteIntakeFin is set to true to ensure we don't close a
// connection without giving the application a chance to respond to a request // connection without giving the application a chance to respond to a request
// sent immediately before the a FIN from the client. // sent immediately before the a FIN from the client.
requestLineStatus = TakeStartLine(SocketInput); requestLineStatus = TakeStartLine(Input);
if (requestLineStatus == RequestLineStatus.Empty) if (requestLineStatus == RequestLineStatus.Empty)
{ {
@ -69,20 +69,20 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
break; break;
} }
await SocketInput; await Input;
} }
InitializeHeaders(); InitializeHeaders();
while (!_requestProcessingStopping && !TakeMessageHeaders(SocketInput, FrameRequestHeaders)) while (!_requestProcessingStopping && !TakeMessageHeaders(Input, FrameRequestHeaders))
{ {
if (SocketInput.CheckFinOrThrow()) if (Input.CheckFinOrThrow())
{ {
// We need to attempt to consume start lines and headers even after // We need to attempt to consume start lines and headers even after
// SocketInput.RemoteIntakeFin is set to true to ensure we don't close a // SocketInput.RemoteIntakeFin is set to true to ensure we don't close a
// connection without giving the application a chance to respond to a request // connection without giving the application a chance to respond to a request
// sent immediately before the a FIN from the client. // sent immediately before the a FIN from the client.
if (!TakeMessageHeaders(SocketInput, FrameRequestHeaders)) if (!TakeMessageHeaders(Input, FrameRequestHeaders))
{ {
RejectRequest(RequestRejectionReason.MalformedRequestInvalidHeaders); RejectRequest(RequestRejectionReason.MalformedRequestInvalidHeaders);
} }
@ -90,7 +90,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
break; break;
} }
await SocketInput; await Input;
} }
if (!_requestProcessingStopping) if (!_requestProcessingStopping)

View File

@ -213,9 +213,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
private void ConsumedBytes(int count) private void ConsumedBytes(int count)
{ {
var scan = _context.SocketInput.ConsumingStart(); var scan = _context.Input.ConsumingStart();
scan.Skip(count); scan.Skip(count);
_context.SocketInput.ConsumingComplete(scan, scan); _context.Input.ConsumingComplete(scan, scan);
OnConsumedBytes(count); OnConsumedBytes(count);
} }
@ -305,7 +305,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
protected override ValueTask<ArraySegment<byte>> PeekAsync(CancellationToken cancellationToken) protected override ValueTask<ArraySegment<byte>> PeekAsync(CancellationToken cancellationToken)
{ {
return _context.SocketInput.PeekAsync(); return _context.Input.PeekAsync();
} }
} }
@ -330,7 +330,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
return new ValueTask<ArraySegment<byte>>(); return new ValueTask<ArraySegment<byte>>();
} }
var task = _context.SocketInput.PeekAsync(); var task = _context.Input.PeekAsync();
if (task.IsCompleted) if (task.IsCompleted)
{ {
@ -402,7 +402,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
: base(context) : base(context)
{ {
RequestKeepAlive = keepAlive; RequestKeepAlive = keepAlive;
_input = _context.SocketInput; _input = _context.Input;
_requestHeaders = headers; _requestHeaders = headers;
} }

View File

@ -172,7 +172,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
SocketInput = new SocketInput(MemoryPool, ThreadPool); SocketInput = new SocketInput(MemoryPool, ThreadPool);
var connectionContext = new MockConnection(new KestrelServerOptions()); var connectionContext = new MockConnection(new KestrelServerOptions());
connectionContext.SocketInput = SocketInput; connectionContext.Input = SocketInput;
Frame = new Frame<object>(application: null, context: connectionContext); Frame = new Frame<object>(application: null, context: connectionContext);
} }

View File

@ -48,7 +48,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
Libuv.uv_buf_t ignored; Libuv.uv_buf_t ignored;
mockLibuv.AllocCallback(socket.InternalGetHandle(), 2048, out ignored); mockLibuv.AllocCallback(socket.InternalGetHandle(), 2048, out ignored);
mockLibuv.ReadCallback(socket.InternalGetHandle(), 0, ref ignored); mockLibuv.ReadCallback(socket.InternalGetHandle(), 0, ref ignored);
Assert.False(connection.SocketInput.CheckFinOrThrow()); Assert.False(connection.Input.CheckFinOrThrow());
}, null); }, null);
connection.ConnectionControl.End(ProduceEndType.SocketDisconnect); connection.ConnectionControl.End(ProduceEndType.SocketDisconnect);

File diff suppressed because it is too large Load Diff

View File

@ -279,7 +279,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
// The block returned by IncomingStart always has at least 2048 available bytes, // The block returned by IncomingStart always has at least 2048 available bytes,
// so no need to bounds check in this test. // so no need to bounds check in this test.
var socketInput = input.FrameContext.SocketInput; var socketInput = input.FrameContext.Input;
var bytes = Encoding.ASCII.GetBytes(data[0]); var bytes = Encoding.ASCII.GetBytes(data[0]);
var block = socketInput.IncomingStart(); var block = socketInput.IncomingStart();
Buffer.BlockCopy(bytes, 0, block.Array, block.End, bytes.Length); Buffer.BlockCopy(bytes, 0, block.Array, block.End, bytes.Length);

View File

@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
FrameContext.ConnectionContext.ListenerContext.ServiceContext.Log = trace; FrameContext.ConnectionContext.ListenerContext.ServiceContext.Log = trace;
_memoryPool = new MemoryPool(); _memoryPool = new MemoryPool();
FrameContext.SocketInput = new SocketInput(_memoryPool, ltp); FrameContext.Input = new SocketInput(_memoryPool, ltp);
} }
public Frame FrameContext { get; set; } public Frame FrameContext { get; set; }
@ -49,10 +49,10 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
public void Add(string text, bool fin = false) public void Add(string text, bool fin = false)
{ {
var data = System.Text.Encoding.ASCII.GetBytes(text); var data = System.Text.Encoding.ASCII.GetBytes(text);
FrameContext.SocketInput.IncomingData(data, 0, data.Length); FrameContext.Input.IncomingData(data, 0, data.Length);
if (fin) if (fin)
{ {
FrameContext.SocketInput.IncomingFin(); FrameContext.Input.IncomingFin();
} }
} }
@ -116,7 +116,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
public void Dispose() public void Dispose()
{ {
FrameContext.SocketInput.Dispose(); FrameContext.Input.Dispose();
_memoryPool.Dispose(); _memoryPool.Dispose();
} }
} }