Skip to content

Commit d3a8a19

Browse files
geoffkizerGeoffrey Kizer
andauthored
rework SocketsHttpHandler request queue handling for HTTP/1.1 and HTTP/2 (#53851)
* rework SocketsHttpHandler request queue handling for HTTP/1.1 and HTTP/2 Co-authored-by: Geoffrey Kizer <geoffrek@windows.microsoft.com>
1 parent e3d319b commit d3a8a19

File tree

13 files changed

+1200
-867
lines changed

13 files changed

+1200
-867
lines changed

src/libraries/Common/src/System/Threading/Tasks/TaskCompletionSourceWithCancellation.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,20 @@ public async ValueTask<T> WaitWithCancellationAsync(CancellationToken cancellati
2121
return await Task.ConfigureAwait(false);
2222
}
2323
}
24+
25+
public T WaitWithCancellation(CancellationToken cancellationToken)
26+
{
27+
using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSourceWithCancellation<T>)s!).TrySetCanceled(cancellationToken), this))
28+
{
29+
return Task.GetAwaiter().GetResult();
30+
}
31+
}
32+
33+
public ValueTask<T> WaitWithCancellationAsync(bool async, CancellationToken cancellationToken)
34+
{
35+
return async ?
36+
WaitWithCancellationAsync(cancellationToken) :
37+
new ValueTask<T>(WaitWithCancellation(cancellationToken));
38+
}
2439
}
2540
}

src/libraries/Common/tests/System/Diagnostics/Tracing/ConsoleEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
3232
{
3333
lock (Console.Out)
3434
{
35-
string text = $"[{eventData.EventSource.Name}-{eventData.EventId}]{(eventData.Payload != null ? $" ({string.Join(", ", eventData.Payload)})." : "")}";
35+
string text = $"[{eventData.EventSource.Name}-{eventData.EventName}]{(eventData.Payload != null ? $" ({string.Join(", ", eventData.Payload)})." : "")}";
3636
if (_eventFilter != null && text.Contains(_eventFilter))
3737
{
3838
ConsoleColor origForeground = Console.ForegroundColor;

src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.Cookies.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async url =>
315315
using (HttpClient client = CreateHttpClient(handler))
316316
{
317317
client.DefaultRequestHeaders.ConnectionClose = true; // to avoid issues with connection pooling
318-
await client.GetAsync(url1);
318+
await client.GetAsync(url1);
319319
}
320320
},
321321
async server =>

src/libraries/System.Net.Http/src/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@
540540
<data name="net_http_request_timedout" xml:space="preserve">
541541
<value>The request was canceled due to the configured HttpClient.Timeout of {0} seconds elapsing.</value>
542542
</data>
543+
<data name="net_http_connect_timedout" xml:space="preserve">
544+
<value>A connection could not be established within the configured ConnectTimeout.</value>
545+
</data>
543546
<data name="net_quic_connectionaborted" xml:space="preserve">
544547
<value>Connection aborted by peer ({0}).</value>
545548
</data>

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/AuthenticationHelper.NtAuth.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ private static async Task<HttpResponseMessage> SendWithNtAuthAsync(HttpRequestMe
6363
if (response.Headers.ConnectionClose.GetValueOrDefault())
6464
{
6565
// Server is closing the connection and asking us to authenticate on a new connection.
66+
67+
// First, detach the current connection from the pool. This means it will no longer count against the connection limit.
68+
// Instead, it will be replaced by the new connection below.
69+
connection.DetachFromPool();
70+
6671
connection = await connectionPool.CreateHttp11ConnectionAsync(request, async, cancellationToken).ConfigureAwait(false);
67-
connectionPool.IncrementConnectionCount();
6872
connection!.Acquire();
6973
isNewConnection = true;
7074
needDrain = false;

0 commit comments

Comments
 (0)