Skip to content

Commit 88b134f

Browse files
authored
Reuse Http2MessageBody (#19629)
1 parent 8ad0f1f commit 88b134f

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

src/Servers/Kestrel/Core/src/Internal/Http/Http1MessageBody.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ internal abstract class Http1MessageBody : MessageBody
1616
protected readonly Http1Connection _context;
1717
protected bool _completed;
1818

19-
protected Http1MessageBody(Http1Connection context)
20-
: base(context)
19+
protected Http1MessageBody(Http1Connection context) : base(context)
2120
{
2221
_context = context;
2322
}

src/Servers/Kestrel/Core/src/Internal/Http/MessageBody.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ public virtual Task StopAsync()
7373

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

76+
public virtual void Reset()
77+
{
78+
_send100Continue = true;
79+
_consumedBytes = 0;
80+
_stopped = false;
81+
_timingEnabled = false;
82+
_backpressure = false;
83+
_alreadyTimedBytes = 0;
84+
_examinedUnconsumedBytes = 0;
85+
}
86+
7687
protected void TryProduceContinue()
7788
{
7889
if (_send100Continue)

src/Servers/Kestrel/Core/src/Internal/Http2/Http2MessageBody.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal sealed class Http2MessageBody : MessageBody
1515
private readonly Http2Stream _context;
1616
private ReadResult _readResult;
1717

18-
private Http2MessageBody(Http2Stream context)
18+
public Http2MessageBody(Http2Stream context)
1919
: base(context)
2020
{
2121
_context = context;
@@ -46,14 +46,10 @@ protected override void OnDataRead(long bytesRead)
4646
AddAndCheckConsumedBytes(bytesRead);
4747
}
4848

49-
public static MessageBody For(Http2Stream context)
49+
public override void Reset()
5050
{
51-
if (context.ReceivedEmptyRequestBody)
52-
{
53-
return ZeroContentLengthClose;
54-
}
55-
56-
return new Http2MessageBody(context);
51+
base.Reset();
52+
_readResult = default;
5753
}
5854

5955
public override void AdvanceTo(SequencePosition consumed)

src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal abstract partial class Http2Stream : HttpProtocol, IThreadPoolWorkItem,
2323
private Http2OutputProducer _http2Output;
2424
private StreamInputFlowControl _inputFlowControl;
2525
private StreamOutputFlowControl _outputFlowControl;
26+
private Http2MessageBody _messageBody;
2627

2728
private bool _decrementCalled;
2829

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

172173
protected override MessageBody CreateMessageBody()
173-
=> Http2MessageBody.For(this);
174+
{
175+
if (ReceivedEmptyRequestBody)
176+
{
177+
return MessageBody.ZeroContentLengthClose;
178+
}
179+
180+
if (_messageBody != null)
181+
{
182+
_messageBody.Reset();
183+
}
184+
else
185+
{
186+
_messageBody = new Http2MessageBody(this);
187+
}
188+
189+
return _messageBody;
190+
}
174191

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

0 commit comments

Comments
 (0)