-
Notifications
You must be signed in to change notification settings - Fork 63
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
feat: Use SocketsHttpHandler
to configure HttpClient
in .NET 6+
#2931
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2931 +/- ##
==========================================
+ Coverage 81.42% 81.44% +0.01%
==========================================
Files 465 465
Lines 29563 29603 +40
Branches 3278 3285 +7
==========================================
+ Hits 24073 24110 +37
- Misses 4697 4698 +1
- Partials 793 795 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
|
/// <typeparam name="TReturn"></typeparam> | ||
/// <param name="task"></param> | ||
/// <returns></returns> | ||
public static TReturn RunSync<TReturn>(Func<Task<TReturn>> task) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marty and I talked about removing this helper method.
@@ -65,7 +102,9 @@ public override IHttpResponse Send(IHttpRequest request) | |||
req.Content.Headers.Add(contentHeader.Key, contentHeader.Value); | |||
} | |||
|
|||
var response = _httpClientWrapper.SendAsync(req).GetAwaiter().GetResult(); | |||
Log.Finest($"Request({request.RequestGuid}: Sending"); | |||
var response = AsyncHelper.RunSync(() => _httpClientWrapper.SendAsync(req)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may not need this helper method, because ConfigureAwait(false) seems to help resolve the same problem in a simpler manner.
NRHttpClient
to dynamically create an instance ofSocketsHttpHandler
when the app is running in .NET 6 or later, and configuredPooledConnectionTimeout
andPooledConnectionIdleTimeout
to help the agent respond better to DNS changes. The connection timeout value is hard-coded to 5 minutes, which overrides the default of "never" and is probably sufficient for our needs. I didn't see a compelling reason to add a configuration setting for this value. Resolves Update HttpClient usage to better support DNS changes on .NET/.NET Core #1845.Additional refactoring:
AsyncHelper
) to safely execute async methods synchronously. Used inNRHttpClient.Send()
as an attempt to avoid deadlocks in the (eventual) call toHttpClient.SendAsync()
. Taken from https://stackoverflow.com/a/56928748/2078975 which seems to be modeled after https://learn.microsoft.com/en-us/archive/msdn-magazine/2015/july/async-programming-brownfield-async-development#the-thread-pool-hack.... harvest finished
message even if no events were sent. This will help in troubleshooting recently reported deadlocks when sending data.IHttpClientFactory.CreateClient()
toGetOrCreateClient()
to better reflect what the method does.IHttpClient
and the classes that implement itConnectionHandler.SendDataOverWire<T>()
to make exception handling a bit cleaner and more clear.throw
the exception again anyway.using
s inNRHttpClient
for objects that areIDisposable
(HttpRequestMessage
,ByteArrayContent
)