Skip to content

DefaultHTTPClientProvider/DefaultHTTPClient implementation can cause socket exhaustion #131

Open
@bcrispcvna

Description

@bcrispcvna

Describe the bug

_httpClient = new HttpClient(new HttpClientHandler

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:

  1. 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.
Image

Platform (please complete the following information):

  • Library Version in use: 2.x

Additional context
I also have concerns over how the setings are instantiated.


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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions