-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Added an additional yield point prior to possibly long running initialization #115688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5abde36
b1b935c
76ae05f
9744b46
a0b46a4
4f3c847
9cba438
00ef15b
a6b81dc
99512b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ public sealed class SocketsHttpHandler : HttpMessageHandler | |
{ | ||
private readonly HttpConnectionSettings _settings = new HttpConnectionSettings(); | ||
private HttpMessageHandlerStage? _handler; | ||
private Task<HttpMessageHandlerStage>? _handlerChainSetupTask; | ||
private Func<HttpConnectionSettings, HttpMessageHandlerStage, HttpMessageHandlerStage>? _decompressionHandlerFactory; | ||
private bool _disposed; | ||
|
||
|
@@ -598,14 +599,14 @@ protected internal override HttpResponseMessage Send(HttpRequestMessage request, | |
|
||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
HttpMessageHandlerStage handler = _handler ?? SetupHandlerChain(); | ||
|
||
Exception? error = ValidateAndNormalizeRequest(request); | ||
if (error != null) | ||
{ | ||
throw error; | ||
} | ||
|
||
HttpMessageHandlerStage handler = _handler ?? SetupHandlerChain(); | ||
|
||
return handler.Send(request, cancellationToken); | ||
} | ||
|
||
|
@@ -620,15 +621,25 @@ protected internal override Task<HttpResponseMessage> SendAsync(HttpRequestMessa | |
return Task.FromCanceled<HttpResponseMessage>(cancellationToken); | ||
} | ||
|
||
HttpMessageHandlerStage handler = _handler ?? SetupHandlerChain(); | ||
|
||
Exception? error = ValidateAndNormalizeRequest(request); | ||
if (error != null) | ||
{ | ||
return Task.FromException<HttpResponseMessage>(error); | ||
} | ||
|
||
return handler.SendAsync(request, cancellationToken); | ||
return _handler is { } handler | ||
? handler.SendAsync(request, cancellationToken) | ||
: CreateHandlerAndSendAsync(request, cancellationToken); | ||
MihaZupan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// SetupHandlerChain may block for a few seconds in some environments. | ||
// E.g. during the first access of HttpClient.DefaultProxy - https://github.com/dotnet/runtime/issues/115301. | ||
// The setup procedure is enqueued to thread pool to prevent the caller from blocking. | ||
async Task<HttpResponseMessage> CreateHandlerAndSendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
MihaZupan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
_handlerChainSetupTask ??= Task.Run(SetupHandlerChain); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to optimize this a bit more and also remove the chance of double-setup you could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds reasonable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's worth the extra logic. |
||
HttpMessageHandlerStage handler = await _handlerChainSetupTask.ConfigureAwait(false); | ||
return await handler.SendAsync(request, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
private static Exception? ValidateAndNormalizeRequest(HttpRequestMessage request) | ||
|
Uh oh!
There was an error while loading. Please reload this page.