如何停止出站HTTP连接超时 [英] How to stop outbound HTTP connections from timing out

查看:129
本文介绍了如何停止出站HTTP连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我目前正在使用以下规范在Azure中托管ASP.NET应用程序:

I'm currently hosting an ASP.NET application in Azure with the following specs:

  • ASP .Net Core 2.2
  • 使用Flurl进行HTTP请求
  • Kestrel Web服务器
  • Docker(Linux-mcr.microsoft.com/dotnet/core/aspnet:2.2运行时)
  • 有关P2V2层应用程序服务计划的Azure App Service

我有几个在该服务上运行的后台作业,该作业对第三方服务进行了很多出站HTTP调用.

I have a a couple of background jobs that run on the service that makes a lot of outbound HTTP calls to a 3rd party service.

问题:

在较小的负载(每10秒大约1个呼叫)下,所有请求都将在不到一秒钟的时间内完成,而不会出现任何问题.我遇到的问题是,在负载沉重的情况下,当服务可以在10秒的时间内完成3/4个呼叫时,某些请求将随机超时并引发异常.当我使用RestSharp时,异常将显示为操作已超时".现在,我正在使用Flurl,该异常显示为呼叫超时".

Under a small load (approximately 1 call per 10 seconds), all requests are completed in under a second with no issue. The issue I'm having is that under a heavy load, when service can make up to 3/4 calls in a 10 second span, some of the requests will randomly timeout and throw an exception. When I was using RestSharp the exception would read "The operation has timed out". Now that I'm using Flurl, the exception reads "The call timed out".

这是关键-如果我从运行Windows 10/Visual Studios 2017的笔记本电脑运行同一作业,则不会发生此问题.这使我相信自己的托管环境正在达到一定极限或用尽了一些资源.不清楚是否与连接/套接字或线程相关.

Here's the kicker - If I run the same job from my laptop running Windows 10 / Visual Studios 2017, this problem does NOT occur. This leads me to believe I'm hitting some limit or running out of some resource in my hosted environment. Unclear if that is connection/socket or thread related.

我尝试过的事情:

  • 确保请求的所有代码路径都使用async/await来防止锁定
  • 确保Kestrel默认值允许无限制的连接(默认情况下确实如此)
  • 确保Docker的默认连接限制足够(默认情况下为2000,足够)
  • 为连接限制配置ServicePointManager设置
  • Ensure all code paths to the request are using async/await to prevent lockouts
  • Ensure Kestrel Defaults allow unlimited connections (it does by default)
  • Ensure Dockers default connection limits are sufficient (2000 by default, more than enough)
  • Configuring ServicePointManager settings for connection limits

这是我当前用来尝试防止出现此问题的startup.cs中的代码:

Here is the code in my startup.cs that I'm currently using to try and prevent this issue:

public class Startup
{
    public Startup(IHostingEnvironment hostingEnvironment)
    {
        ...

        // ServicePointManager setup
        ServicePointManager.UseNagleAlgorithm = false;
        ServicePointManager.Expect100Continue = false;
        ServicePointManager.DefaultConnectionLimit = int.MaxValue;
        ServicePointManager.EnableDnsRoundRobin = true;
        ServicePointManager.ReusePort = true;

        // Set Service point timeouts
        var sp = ServicePointManager.FindServicePoint(new Uri("https://placeholder.thirdparty.com"));
        sp.ConnectionLeaseTimeout = 15 * 1000; // 15 seconds  

        FlurlHttp.ConfigureClient("https://placeholder.thirdparty.com", cli => cli.Settings.ConnectionLeaseTimeout = new TimeSpan(0, 0, 15));
    }
}

还有其他人遇到与此类似的问题吗?我愿意就如何最好地调试这种情况或解决此问题的可能方法提供任何建议.经过几天的研究,我完全不知所措.

Has anyone else run into a similar issue to this? I'm open to any suggestions on how to best debug this situation, or possible methods to correct the issue. I'm at a complete loss after researching this for several days.

谢谢.

推荐答案

我遇到了类似的问题.看看 Asp.net核心HttpClient具有许多TIME_WAIT或CLOSE_WAIT连接.通过netstat进行调试有助于为我确定问题.作为一种可能的解决方案.我建议您使用IHttpClientFactory.您可以从 https:获取更多信息://docs.microsoft.com/zh-CN/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2 ASP.Net Core 2.1和IHttpClientFactory中的flurl客户端生存期

I had similar issues. Take a look at Asp.net Core HttpClient has many TIME_WAIT or CLOSE_WAIT connections . Debugging via netstat helped identify the problem for me. As one possible solution. I suggest you use IHttpClientFactory. You can get more info from https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2 It should be fairly easy to use as described in Flurl client lifetime in ASP.Net Core 2.1 and IHttpClientFactory

这篇关于如何停止出站HTTP连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆