Skip to content

Commit

Permalink
Dispose streams that don't fit in pool (#30995)
Browse files Browse the repository at this point in the history
* Dispose streams that don't fit in pool

* PR feedback
  • Loading branch information
JamesNK authored Mar 17, 2021
1 parent 8481b93 commit 6adb596
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/Servers/Kestrel/Core/src/Internal/Http2/Http2Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,21 +668,6 @@ private Http2StreamContext CreateHttp2StreamContext()
return streamContext;
}

private void ReturnStream(Http2Stream stream)
{
// We're conservative about what streams we can reuse.
// If there is a chance the stream is still in use then don't attempt to reuse it.
Debug.Assert(stream.CanReuse);

if (StreamPool.Count < MaxStreamPoolSize)
{
// This property is used to remove unused streams from the pool
stream.DrainExpirationTicks = SystemClock.UtcNowTicks + StreamPoolExpiryTicks;

StreamPool.Push(stream);
}
}

private Task ProcessPriorityFrameAsync()
{
if (_currentHeadersStream != null)
Expand Down Expand Up @@ -1181,12 +1166,19 @@ private void UpdateCompletedStreams()
private void RemoveStream(Http2Stream stream)
{
_streams.Remove(stream.StreamId);
if (stream.CanReuse)

if (stream.CanReuse && StreamPool.Count < MaxStreamPoolSize)
{
ReturnStream(stream);
// Pool and reuse the stream if it finished in a graceful state and there is space in the pool.

// This property is used to remove unused streams from the pool
stream.DrainExpirationTicks = SystemClock.UtcNowTicks + StreamPoolExpiryTicks;

StreamPool.Push(stream);
}
else
{
// Stream didn't complete gracefully or pool is full.
stream.Dispose();
}
}
Expand Down

0 comments on commit 6adb596

Please sign in to comment.