Skip to content

Commit

Permalink
rework SocketsHttpHandler request queue handling for HTTP/1.1 and HTT…
Browse files Browse the repository at this point in the history
…P/2 (#53851)

* rework SocketsHttpHandler request queue handling for HTTP/1.1 and HTTP/2

Co-authored-by: Geoffrey Kizer <[email protected]>
  • Loading branch information
geoffkizer and Geoffrey Kizer authored Jul 15, 2021
1 parent e3d319b commit d3a8a19
Show file tree
Hide file tree
Showing 13 changed files with 1,200 additions and 867 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,20 @@ public async ValueTask<T> WaitWithCancellationAsync(CancellationToken cancellati
return await Task.ConfigureAwait(false);
}
}

public T WaitWithCancellation(CancellationToken cancellationToken)
{
using (cancellationToken.UnsafeRegister(static (s, cancellationToken) => ((TaskCompletionSourceWithCancellation<T>)s!).TrySetCanceled(cancellationToken), this))
{
return Task.GetAwaiter().GetResult();
}
}

public ValueTask<T> WaitWithCancellationAsync(bool async, CancellationToken cancellationToken)
{
return async ?
WaitWithCancellationAsync(cancellationToken) :
new ValueTask<T>(WaitWithCancellation(cancellationToken));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
lock (Console.Out)
{
string text = $"[{eventData.EventSource.Name}-{eventData.EventId}]{(eventData.Payload != null ? $" ({string.Join(", ", eventData.Payload)})." : "")}";
string text = $"[{eventData.EventSource.Name}-{eventData.EventName}]{(eventData.Payload != null ? $" ({string.Join(", ", eventData.Payload)})." : "")}";
if (_eventFilter != null && text.Contains(_eventFilter))
{
ConsoleColor origForeground = Console.ForegroundColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async url =>
using (HttpClient client = CreateHttpClient(handler))
{
client.DefaultRequestHeaders.ConnectionClose = true; // to avoid issues with connection pooling
await client.GetAsync(url1);
await client.GetAsync(url1);
}
},
async server =>
Expand Down
3 changes: 3 additions & 0 deletions src/libraries/System.Net.Http/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,9 @@
<data name="net_http_request_timedout" xml:space="preserve">
<value>The request was canceled due to the configured HttpClient.Timeout of {0} seconds elapsing.</value>
</data>
<data name="net_http_connect_timedout" xml:space="preserve">
<value>A connection could not be established within the configured ConnectTimeout.</value>
</data>
<data name="net_quic_connectionaborted" xml:space="preserve">
<value>Connection aborted by peer ({0}).</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ private static async Task<HttpResponseMessage> SendWithNtAuthAsync(HttpRequestMe
if (response.Headers.ConnectionClose.GetValueOrDefault())
{
// Server is closing the connection and asking us to authenticate on a new connection.

// First, detach the current connection from the pool. This means it will no longer count against the connection limit.
// Instead, it will be replaced by the new connection below.
connection.DetachFromPool();

connection = await connectionPool.CreateHttp11ConnectionAsync(request, async, cancellationToken).ConfigureAwait(false);
connectionPool.IncrementConnectionCount();
connection!.Acquire();
isNewConnection = true;
needDrain = false;
Expand Down
Loading

0 comments on commit d3a8a19

Please sign in to comment.