Skip to content
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

Reuse HttpClient #459

Closed
gtoombs-microsaas opened this issue Dec 16, 2021 · 4 comments
Closed

Reuse HttpClient #459

gtoombs-microsaas opened this issue Dec 16, 2021 · 4 comments
Assignees

Comments

@gtoombs-microsaas
Copy link

Currently, HtmlWeb does not reuse a client, and spins one up on every call:

      HttpClient httpClient = new HttpClient((HttpMessageHandler) httpClientHandler);

This is in contravention of the recommendations from .NET:

HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors.

@JonathanMagnan JonathanMagnan self-assigned this Dec 17, 2021
@JonathanMagnan
Copy link
Member

Hello @gtoombs-microsaas ,

Thank you for reporting, you are right.

However, there is also some customization in the HttpClient such as https://github.com/zzzprojects/html-agility-pack/blob/def26b2d2f5ea8e6a077d1dd8ec145004a6cf424/src/HtmlAgilityPack.Shared/HtmlWeb.cs#L1833 and the handler which currently make it impossible to re-use the same HttpClient in some scenario.

However, we will look at if we can re-use it when none of those setting are configured which is pretty much the case probably 99% of the time.

Best Regards,

Jon

@gtoombs-microsaas
Copy link
Author

gtoombs-microsaas commented Dec 17, 2021

@JonathanMagnan Good news! The specific line you referenced:

client.DefaultRequestHeaders.Add("User-Agent", UserAgent);

is fairly easy to convert to a per-request setting of headers. There are many convenient ways to do this, including holding those headers in a member variable and applying them en-masse to a request. Said another way: even for the 1% of requests that require customisation, it should still not be necessary to have a separate Client instance if the customisation is only for HTTP headers.

@JonathanMagnan
Copy link
Member

Hello @gtoombs-microsaas ,

The v1.11.40 has been released.

The same HttpClient (per UserAgent) should now be re-used whenever is possible.

Let me know if everything works as expected.

Best Regards,

Jon

@gtoombs-microsaas
Copy link
Author

Amazing, thanks! I'll check it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants