HttpClient在重负载下无法执行

时间:2017-09-27 14:32:14

标签: .net .net-core

我在严重负载下遇到严重的.NET if问题。我创建了一个GitHub repo来演示这个问题。非常感谢任何帮助。是的,我每个权限只使用一个HttpClient。 http和https也有同样的问题。这不是服务器端问题,因为服务器始终保持响应。

https://github.com/erikbra/HttpClientProblems/issues/1

通常会收到以下错误。 HttpClient跟踪日志说它在连接上调用了System.Net。但很难获得更多信息。

::Abort

[UPDATE]

确切地说,我想了解发生了什么。显然连接正在被削减,但为什么呢?服务器仍在响应,因此它不是服务器端的限制。瓶颈在哪里?

如果我发生了什么?将System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: The connection with the server was terminated abnormally at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Threading.Tasks.RendezvousAwaitable`1.GetResult() at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext() --- End of inner exception stack trace --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at UnitTests.Call_Api.<CallApi>d__22.MoveNext() in C:\Users\ErikBrandstadmoen\Source\Repos\HttpClientProblems\UnitTests.NetCore\Call_Api.cs:line 204 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at UnitTests.Call_Api.<Measure>d__23.MoveNext() in C:\Users\ErikBrandstadmoen\Source\Repos\HttpClientProblems\UnitTests.NetCore\Call_Api.cs:line 219 设置为2,并启动4个任务?不会轮流送他们吗?与128和1000相似,但在某处它会停止缩放。我怎样才能进一步调查瓶颈是什么?我只得到ServicePointManager.DefaultConnectionLimit,而且没有更多细节。有什么提示吗?

2 个答案:

答案 0 :(得分:3)

我没有进入你的github解决方案的代码细节,但这有点像预期的行为。每个ServicePoint都有一个最大连接数的设置,并且当您执行并行异步执行时,对于运行时间较长的请求,您的连接会更快地运行。

这可能是一个很好的起点: https://docs.microsoft.com/en-us/dotnet/framework/network-programming/managing-connections

关于有多少连接没有明确,统一的答案,但您应该将MaxDegreeOfParalelism保持在允许的最大连接数之下,并且您是安全的。

<强> [UPDATE]

另外,您可能需要查看的内容是System.Net.Http.WinHttpHandler

https://msdn.microsoft.com/en-us/library/system.net.http.winhttphandler(v=vs.105).aspx

当您创建HttpClient的实例时,您可以执行以下操作:

var httpMessageHandler = new System.Net.Http.WinHttpHandler();
//here you set different handler options (see docs link above)
var httpClient = new HttpClient(httpMessageHandler);

答案 1 :(得分:0)

更准确地说,表现不佳的是HttpClient使用的HttpMessageHandler。

我已经将.NET Core 2.1 SocketsHttpHandler移植到了.NET Framework,并且当同时发出多个请求时,与.NET Framework HttpClientHandler相比,我发现backported implementation的性能有了显着提高。