Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse Http2MessageBody #19629

Merged
merged 4 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ internal abstract class Http1MessageBody : MessageBody
protected readonly Http1Connection _context;
protected bool _completed;

protected Http1MessageBody(Http1Connection context)
: base(context)
protected Http1MessageBody(Http1Connection context) : base(context)
{
_context = context;
}
Expand Down
11 changes: 11 additions & 0 deletions src/Servers/Kestrel/Core/src/Internal/Http/MessageBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ public virtual Task StopAsync()

protected virtual Task OnStopAsync() => Task.CompletedTask;

public virtual void Reset()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to call Reset from the constructor to ensure consistent state the first time it's used.

{
_send100Continue = true;
_consumedBytes = 0;
_stopped = false;
_timingEnabled = false;
_backpressure = false;
_alreadyTimedBytes = 0;
_examinedUnconsumedBytes = 0;
}

protected void TryProduceContinue()
{
if (_send100Continue)
Expand Down
12 changes: 4 additions & 8 deletions src/Servers/Kestrel/Core/src/Internal/Http2/Http2MessageBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal sealed class Http2MessageBody : MessageBody
private readonly Http2Stream _context;
private ReadResult _readResult;

private Http2MessageBody(Http2Stream context)
public Http2MessageBody(Http2Stream context)
: base(context)
{
_context = context;
Expand Down Expand Up @@ -46,14 +46,10 @@ protected override void OnDataRead(long bytesRead)
AddAndCheckConsumedBytes(bytesRead);
}

public static MessageBody For(Http2Stream context)
public override void Reset()
{
if (context.ReceivedEmptyRequestBody)
{
return ZeroContentLengthClose;
}

return new Http2MessageBody(context);
base.Reset();
_readResult = default;
}

public override void AdvanceTo(SequencePosition consumed)
Expand Down
19 changes: 18 additions & 1 deletion src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal abstract partial class Http2Stream : HttpProtocol, IThreadPoolWorkItem
private Http2OutputProducer _http2Output;
private StreamInputFlowControl _inputFlowControl;
private StreamOutputFlowControl _outputFlowControl;
private Http2MessageBody _messageBody;

private bool _decrementCalled;

Expand Down Expand Up @@ -170,7 +171,23 @@ protected override string CreateRequestId()
=> StringUtilities.ConcatAsHexSuffix(ConnectionId, ':', (uint)StreamId);

protected override MessageBody CreateMessageBody()
=> Http2MessageBody.For(this);
{
if (ReceivedEmptyRequestBody)
{
return MessageBody.ZeroContentLengthClose;
}

if (_messageBody != null)
{
_messageBody.Reset();
}
else
{
_messageBody = new Http2MessageBody(this);
}

return _messageBody;
}

// Compare to Http1Connection.OnStartLine
protected override bool TryParseRequest(ReadResult result, out bool endConnection)
Expand Down