我在多个多线程消费者中使用 .net HTTPClient,每秒一次向本地服务发出 GetAsync Web 请求,速度为 127.0.0.1。
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);
}
}