You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The DefaultHTTPClientProvider/DefaultHTTPClient implementation is improperly using HttpClient.
When using Analytics as scoped, it ends up with a call to "new HttpClient(.." on every request, which is known to cause issues.
HttpClient is intended to be instantiated once and reused throughout the life of an application. In .NET Core and .NET 5+, HttpClient pools connections inside the handler instance and reuses a connection across multiple requests. If you instantiate an HttpClient class for every request, the number of sockets available under heavy loads will be exhausted. This exhaustion will result in SocketException errors. https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-net-http-httpclient
To Reproduce
Steps to reproduce the behavior:
make a ton of requests to Analytics when it's registered as scoped
Expected behavior
open sockets do not spike
Screenshots
If applicable, add screenshots to help explain your problem.
here's a view of what happens to open sockets when a burst of requests happen.
Platform (please complete the following information):
Library Version in use: 2.x
Additional context
I also have concerns over how the setings are instantiated.
hi @bcrispcvna, thanks for reporting this issue. we are working on some optimizations for server use cases. in the meanwhile, you can create your own httpclient and pass it to configuration as a workaround. also be aware of some other implications if you want to use this SDK as scoped for server use cases.
Does this potential exhaustion also occur with a singleton approach? Just curious if I should wait for this fix before moving forward with the migration from the older SDK.
Describe the bug
Analytics-CSharp/Analytics-CSharp/Segment/Analytics/Utilities/HTTPClient.cs
Line 181 in 4fe89cb
The DefaultHTTPClientProvider/DefaultHTTPClient implementation is improperly using HttpClient.
When using Analytics as scoped, it ends up with a call to "new HttpClient(.." on every request, which is known to cause issues.
HttpClient is intended to be instantiated once and reused throughout the life of an application. In .NET Core and .NET 5+, HttpClient pools connections inside the handler instance and reuses a connection across multiple requests. If you instantiate an HttpClient class for every request, the number of sockets available under heavy loads will be exhausted. This exhaustion will result in SocketException errors.
https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-net-http-httpclient
To Reproduce
Steps to reproduce the behavior:
Expected behavior
open sockets do not spike
Screenshots
If applicable, add screenshots to help explain your problem.
here's a view of what happens to open sockets when a burst of requests happen.

Platform (please complete the following information):
Additional context
I also have concerns over how the setings are instantiated.
Analytics-CSharp/Analytics-CSharp/Segment/Analytics/Analytics.cs
Line 243 in 4fe89cb
all of which can be fixed by properly following http client best practices.
https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines
The text was updated successfully, but these errors were encountered: