gpt4 book ai didi

c# - 执行超过 1000 个 HTTP 请求任务失败

转载 作者:行者123 更新时间:2023-11-30 14:27:55 25 4
gpt4 key购买 nike

我正在尝试对 API 进行负载测试。我同时执行一个任务,每个任务执行一个 HTTP 请求。我使用 Task.WhenAll(mytasks) 等待所有任务完成。请求如下所示:

using (var response = await client.SendAsync(request).ConfigureAwait(false))
{
using (var jsonResponse = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
var jsonSerializer = new DataContractJsonSerializer(typeof(Borders));
var borders = (Borders)jsonSerializer.ReadObject(jsonResponse);
return borders;
}
}

这至少适用于数千个任务。但是,如果我开始的任务超过几千个,我就会遇到 HttpRequestExceptions:

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at BusinessLogic.<GetBorder>d__6d.MoveNext() in c:\BusinessLogic.cs:line 757

所以我的问题是:为什么会发生这种情况(超过 1000 个任务)?我怎样才能防止这种情况发生?我显然可以将我的任务 block 分成 <1000 个 block ,但我想把它留给底层系统......

最佳答案

I would like to leave this to the underlying system...

这不是个好主意。 .NET Framework 在确定 IO 的最佳并行度方面的能力为零。

并行发出那么多请求通常不是一个好主意,因为这里强调的资源可能在此之前就已经用完了。显然,您的后端服务器无法处理这种程度的并行性。它根据消息强行切断你。

仅仅因为我们现在通过 await 实现了简单的异步 IO,并不意味着您可以使用 1000 个并行请求向您的资源发送垃圾邮件。

使用一种常见的解决方案来执行一系列具有一定并行度的异步操作。我喜欢http://blogs.msdn.com/b/pfxteam/archive/2012/03/05/10278165.aspx但有基于 ActionBlock 的解决方案

关于c# - 执行超过 1000 个 HTTP 请求任务失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30886943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com