gpt4 book ai didi

c# - IIS 中托管的 WCF 有很多打开的请求,服务速度变慢

转载 作者:行者123 更新时间:2023-11-30 16:46:01 26 4
gpt4 key购买 nike

我有一个基于 JSON 和 POST 方法的 WCF Web 服务,它有一个名为网站的功能。这个函数有一个简单的代码,只使用下面的代码调用另一个网络服务:

        using (var cli = new MyWebClient())
{
Task<string> t = cli.UploadStringTaskAsync(myURI, "POST", request);
if (t == await Task.WhenAny(t, Task.Delay(400)))
{
response = t.Result;
}
else
{
response = "";
}
cli.Dispose();
}

MyWebClient类实现为:

class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).KeepAlive = true;
(request as HttpWebRequest).ContentType = "application/json";
}
return request;
}
}

问题是我可以在 IIS 中看到大量请求保持打开状态超过 18 秒,对于我的 1 或 2 个工作进程甚至更多(如您在其中一个的附图中所见)! !!这使得服务速度非常慢。请注意,此服务每秒大约有 2K 个请求,此服务的应用程序池有一个包含 12 个工作进程的网络花园,队列限制为 10K。当有(例如)4 个工作进程在可预测的时间(大约 450 毫秒)内工作并且 IIS 显示其请求的最大运行时间约为 380 时,就会发生这种情况。

a large number of requests that remain open in the IIS

请注意,我使用了 cli.UploadStringTaskAsync,因此 cli 不考虑超时。因此,我必须实现类似 t == await Task.WhenAny(t, Task.Delay(400)) 的代码来模拟超时。

你觉得有什么问题?!使用 await 是否会导致多次上下文切换并且请求排队等待 cpu 执行?


编辑:

Here您可以找到一些有用的建议,但没有人可以帮助解决问题。我在我的应用程序的网络配置中设置了它们,但它无法解决我的问题。


更新:

作为附加信息注意网卡是1G的,我们最多有100Mgb/s的带宽使用。 Intel Xeon E5-1650 V3 3.5 Ghz 有 6 个核心和 12 个逻辑处理器。我们有 128GB RAM 和 480GB SSD。

最佳答案

我找到了解决问题的方法。关键点是“processModel 元素(ASP.NET 设置架构)”。正如我在问题中提到的:

This situation takes place when there are (for example) 4 worker processes working in a predictable time(about 450 ms) and IIS shows that the maximum elapsed time on their requests are about 380.

所以,我认为平衡工作进程之间的负载可能是个问题。通过手动配置 processModel 元素,我已经解决了这个问题。经过大量研究后,我发现 this关于 processModel 元素及其属性的有值(value)的链接。

还有 this链接描述了每个项目的所有属性和效果。正如此链接中提到的,有 2 个重要属性称为“requestLimit”和“requestQueueLimit”:

requestQueueLimit: Specifies the number of requests that are allowed in the queue before ASP.NET begins returning the message "503 – Server Too Busy" to new requests. The default is 5000.

requestLimit: Specifies the number of requests that are allowed before ASP.NET automatically launches a new worker process to take the place of the current one. The default is Infinite.

解决方案 是通过一个有理数来控制和限制 requestLimit,例如在我的例子中是 300。此外,通过将工作进程数与 requestLimit 相乘来限制 requestQueueLimit。我已经将worker进程的数量增加到20个,通过这个配置总共可以排队6000个请求,每个worker进程最多有300个请求。通过达到每个工作进程 300 个请求,ASP.NET 会自动启动一个新的工作进程来取代当前的工作进程。

因此负载在工作进程之间得到更好的平衡。我检查了所有队列,没有超过 400 时间的请求!!!

我认为通过使用这些属性(requestLimitrequestQueueLimitnumber,此解决方案可以用作 IIS 和工作进程的半负载平衡器算法工作进程数 ).

关于c# - IIS 中托管的 WCF 有很多打开的请求,服务速度变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41088393/

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