我跨多个多线程使用者使用.net HTTPClient,以每秒127.0.0.1的速度向本地服务发出一次GetAsync web请求。
web请求完成99.9%的时间,但偶尔会有一些请求(在3-4小时内)会卡在GetAsyc中,不会完成或超时。在同一时间段内对相同服务url/端口的请求将正常工作,并且新请求将正常完成。
GetAsync是在一种“即发即忘”模式下启动的,在这种模式下,完成时会调用一个回调来处理解析后的数据(因为它与一些不使用异步的旧代码集成在一起)。
public void Execute(Action<IAsyncCommand> onCompletion)
{
this.onAsyncCompletion = onCompletion;
try
{
// do not await as this is fire and forget
this.HandlRequestAysnc(this.Target, new StringContent(this.CommandPayload));
return;
}
catch(Exception e)
{
//log exception
}
}
private async Task HandlRequestAysnc(Uri uri, StringContent stringContent)
{
try
{
ConfiguredTaskAwaitable<HttpResponseMessage> request = stringContent != null ? webClient.PostAsync(uri, stringContent).ConfigureAwait(false) : webClient.GetAsync(uri).ConfigureAwait(false);
//this will never return or timeout 1 in 10000 times
using (HttpResponseMessage response = await request)
{
if (response.IsSuccessStatusCode)
{
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
//handle result
}
}
else
{
//handle failure
}
}
}
catch (Exception ex)
{
//log exception
}
if (this.onAsyncCompletion != null)
{
this.onAsyncCompletion(this);
}
}发布于 2015-09-09 18:07:41
GetAync的问题之一是,一旦会话启动,TCP堆栈就会得到控制。最近的一项实验室实验证明,旋转10,000个get请求(用于调查我们在prod环境中出现内存问题的原因),TCP堆栈需要5分钟以上(应用程序结束后)才能清理所有内容。
如果您发现套接字状态为Fin-Wait 1或2、Time-Wait或其他半生不熟的会话,这只是一个更大问题的症状,即两个系统中的一个(或两个)无法处理该速率的流量。一旦这种情况开始发生,事情就会迅速失控,因为双方都在努力维持会话,但却失去了足够快的资源。
这类问题的解决方案是找到另一种方法来增加吞吐量。
https://stackoverflow.com/questions/30402075
复制相似问题