如何禁用.NET HttpClient的流水线 [英] How to disable pipelining for the .NET HttpClient

查看:83
本文介绍了如何禁用.NET HttpClient的流水线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我想禁用流水线功能,但仍然要使用连接池(因此,不能选择KeepAlive = False)。原因是较长的请求可能会阻止较短的请求快速执行,但是为每个请求打开新连接的速度明显较慢。



我正在这样做:

  _webRequestHandler = new WebRequestHandler(); 
_webRequestHandler.AllowPipelining = false;

_httpClient =新的HttpClient(_webRequestHandler);
等待_httpClient.SendAsync(request);

servicePoint = ServicePointManager.FindServicePoint(request.RequestUri);

这似乎没有效果:ServicePoint.SupportsPipelining属性仍然为true,仅设置了WebRequestHandler HttpWebRequest的Pipelined属性,但HttpRequestMessage上没有任何内容,因此基本上在WebRequestHandler上设置AllowPipelining无效(?)。



我在这里缺少什么吗?我确定这是一个常见情况-如何实现?

解决方案

情况仍然存在:




  • 在HttpClient上将ConnectionClose设置为true会发出KeepAlive:false,并为每个请求建立一个新连接。

  • 将ConnectionClose设置为false会重用连接-但仅当它们是空闲的(无待处理的请求)时,才对连接产生重用。拥有大量同时请求仍然会打开大量连接。

  • 将HttpClient上的ConnectionLimit设置为Environment.ProcessorCount * 12(由MS建议)限制了将连接打开到指定值。不幸的是,这导致随机的无响应性,从而关闭了整个系统!我认为这是在使用所有打开的连接,激活流水线然后发生某些情况时发生的。这就是为什么我想关闭管道传输(只是等到任何打开的连接变为免费并重用它)。



不能以这种方式设置,至少我找不到它,没有人可以帮助我。并期望MS解决实际问题是...好吧,不太可能。发生者(在我们的系统中)。不过,这不是真正的解决方法-如果您有更好的解决方案,请随时发布...


As the title says I'd like to disable the pipelining functionality BUT still make use of the connection pool (so KeepAlive=False is not an option). The reason is that longer requests may prevent shorter requests from executing fast, but opening up a new connection for every requests is significantly slower.

I'm doing this:

_webRequestHandler = new WebRequestHandler();
_webRequestHandler.AllowPipelining = false;

_httpClient = new HttpClient(_webRequestHandler);
await _httpClient.SendAsync(request);

servicePoint = ServicePointManager.FindServicePoint(request.RequestUri);

This seems to have no effect: The ServicePoint.SupportsPipelining property is still true and the WebRequestHandler is only setting the Pipelined property of the HttpWebRequest but nothing on the HttpRequestMessage so basically setting AllowPipelining on the WebRequestHandler has no effect (?).

Am I missing something here? I'm sure this is a common scenario - how can I achieve that?

解决方案

The situation remains:

  • Setting ConnectionClose to true on the HttpClient issues a KeepAlive: false and establishes a new connection for every request. This has a significant negativ performance impact and is therefore undesired.
  • Setting ConnectionClose to false reuses the connections - but only when they are free (no pending request). Having a large number of simultanous requests will still open a large number of connections.
  • Setting the ConnectionLimit on the HttpClient to Environment.ProcessorCount * 12 (as suggested by MS) is limiting the number of open connections to the specified value. Unfortunately this is resulting in random unresponsiveness shutting the whole system down! I assume this happens when all open connections are used, pipelining is activated and then... something happens. This is why I wanted to turn pipelining off (just wait until any open connection becomes free and reuse it).

But it seems this can not be set this way, at least I couldn't find it and no one could help me. And expecting MS to fix the real problem is... well, unlikely.

So I ended up setting the ConnectionLimit to 1000 which seems to be high enough that the problem never occurrs (in our system). Not a real fix though - feel free to post if you have a better solution...

这篇关于如何禁用.NET HttpClient的流水线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆