From 4a92aad3bc79b2dfe88ffa54ae364c345aef6bd3 Mon Sep 17 00:00:00 2001 From: Ahmet Ibrahim Aksoy Date: Fri, 2 Aug 2024 15:44:05 +0200 Subject: [PATCH 1/2] Move away marking idle/non-idle from HttpConnectionPool --- .../Http/SocketsHttpHandler/Http2Connection.cs | 17 ++++++++++------- .../tests/FunctionalTests/MetricsTest.cs | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs index 5224adcfa36a6e..862ac35498719a 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs @@ -288,11 +288,6 @@ public bool TryReserveStream() if (_streamsInUse < _maxConcurrentStreams) { - if (_streamsInUse == 0) - { - MarkConnectionAsNotIdle(); - } - _streamsInUse++; return true; } @@ -324,8 +319,6 @@ public void ReleaseStream() if (_streamsInUse == 0) { - MarkConnectionAsIdle(); - if (_shutdown) { FinalTeardown(); @@ -1606,6 +1599,11 @@ private void AddStream(Http2Stream http2Stream) ThrowRetry(SR.net_http_request_aborted); } + if (_httpStreams.Count == 0) + { + MarkConnectionAsNotIdle(); + } + // Now that we're holding the lock, configure the stream. The lock must be held while // assigning the stream ID to ensure only one stream gets an ID, and it must be held // across setting the initial window size (available credit) and storing the stream into @@ -2072,6 +2070,11 @@ private void RemoveStream(Http2Stream http2Stream) Debug.Fail($"Stream {http2Stream.StreamId} not found in dictionary during RemoveStream???"); return; } + + if (_httpStreams.Count == 0) + { + MarkConnectionAsIdle(); + } } ReleaseStream(); diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs index 759f24b8f74de9..c007428d0152cc 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs @@ -719,7 +719,7 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri => Action check2 = connectionNoLongerIdle; Action check3 = connectionIsActive; - if (UseVersion.Major > 1) + if (UseVersion.Major > 2) { // With HTTP/2 and HTTP/3, the idle state change is emitted before RequestsQueueDuration. check1 = connectionNoLongerIdle; From bb3e057b59c697725cf0df844c7f862fc68bdbf0 Mon Sep 17 00:00:00 2001 From: Ahmet Ibrahim Aksoy Date: Fri, 2 Aug 2024 15:54:57 +0200 Subject: [PATCH 2/2] Fix comment --- .../System.Net.Http/tests/FunctionalTests/MetricsTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs index c007428d0152cc..161b1e793b78bc 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs @@ -721,7 +721,7 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri => if (UseVersion.Major > 2) { - // With HTTP/2 and HTTP/3, the idle state change is emitted before RequestsQueueDuration. + // With HTTP/3, the idle state change is emitted before RequestsQueueDuration. check1 = connectionNoLongerIdle; check2 = connectionIsActive; check3 = requestsQueueDuration;