HttpClient and the socket Exhaustion - clarification?

為{幸葍}努か 提交于 2020-07-08 11:44:06

问题


This article says that we should use a static HttpClient in order to reuse sockets.

But the first comment there says that there is a DNS changes recognition issue, and the solution is in another article here:

The second article suggested :

var client = new HttpClient();
client.DefaultRequestHeaders.ConnectionClose = true; 

Which controls the KeepAlive header. But suffers from preventing you to take advantage of benefits of re-using a socket

Another solution was :

var sp = ServicePointManager.FindServicePoint(new Uri("http://foo.bar/baz/123?a=ab"));
sp.ConnectionLeaseTimeout = 60*1000; // 1 minute

BUT:

He didn't say whether should we use new Httpclient each time we want to make a request, or should we still use the static one.

Question:

Say I want to use this solution :

var sp = ServicePointManager.FindServicePoint(new Uri("http://foo.bar/baz/123?a=ab"));
sp.ConnectionLeaseTimeout = 60*1000; // 1 minute 
  • Should I still use the static HttpClient approach ? or should I new HttpClient each time I want to make a call? Or - Should I create static/not static new HttpClient for each scheme://basedns ?

He showed the problem but his Conclusion doesn't show the whole right final solution.

Please notice - I'm asking about .net framework. not .net Core.


回答1:


That's a very old article that does explain why HttpClient should be reused, but doesn't explain how to handle DNS changes. That's explained in Singleton HttpClient? Beware of this serious behaviour and how to fix it. That still only deals with one connection.

The obvious answer is to avoid singleton HttpClients but still reuse them for some time. Besides, different sockets are used to connect to different servers, which means we'd really need to reuse (and recycle) sockets per server. The solution came later in the form of HttpClientFactory.

The nice thing is that HttpClientFactory is a .NET Standard 2.0 package, Microsoft.Extensions.Httpthat can be used by both .NET Core and .NET Old, not just ASP.NET Core applications. I use it in console applications for example.

A very good introduction is HttpClientFactory in ASP.NET Core 2.1 (PART 1) by Steve Gordon, and all his articles in that series.

In short, each time we want an HttpClient we ask the factory for an instance :

[Route("api/[controller]")]
public class ValuesController : Controller
{
    private readonly IHttpClientFactory _httpClientFactory;

    public ValuesController(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }

    [HttpGet]
    public async Task<ActionResult> Get()
    {
        var client = _httpClientFactory.CreateClient();
        var result = await client.GetStringAsync("http://www.google.com");
        return Ok(result);
    }
}

HttpClient delegates work to a SocketClientHandler. That's what needs to be reused. HttpClientFactory produces HttpClient instances that resuse Socket handlers from a pool of socket handlers. The handlers are recyclec periodically to take care of DNS changes.

Even better, HttpClientFactory can be combined with Polly to add retry logic to the HttpClient instances. It does this behind the scenes by configuring the handlers.




回答2:


ServicePointManager is flaky in my opinion and is only valid on Windows machines. Effectively be dead code if you go over to dotnet core from dotnet framework. I would not depend my implementation on it.

My suggestion is create a singleton service serving only one instance of HttpClient.

If you are using asp.net core they provide you with an implementation of the static client as well as re-describe the issue in this document



来源:https://stackoverflow.com/questions/58427764/httpclient-and-the-socket-exhaustion-clarification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!