AsyncHttpClient是否知道要为所有HTTP请求分配多少个线程 [英] Does AsyncHttpClient knows how many threads to allocate for all the HTTP requests

查看:238
本文介绍了AsyncHttpClient是否知道要为所有HTTP请求分配多少个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估 AsyncHttpClient 用于大负载(〜1M HTTP请求). 对于每个请求,我想使用AsyncCompletionHandler调用回调,该回调会将结果插入到阻塞队列中

我的问题是:如果我要在一个紧密的循环中发送异步请求,那么AsyncHttpClient将使用多少个线程? (我知道您可以设置最大值,但显然您有丢失请求的风险,我已经看到它此处)

我目前在以下版本中使用Netty实现:

  • async-http-client v1.9.33
  • netty v3.10.5.Final

如果以后的版本有任何优化,我不介意使用其他版本

我了解到Netty使用反应器模式来响应HTTP响应,这意味着它分配很少数量的线程作为选择器.这也意味着分配的线程数不会随着请求量的增加而增加.但是,这与设置最大连接数相矛盾.

任何人都可以解释我所缺少的吗?

预先感谢

解决方案

AsyncHttpClient客户端(以及与此相关的其他非阻塞IO客户端),不需要为每个请求分配线程,并且客户端需要即使您用请求轰炸它,也不要调整其线程池的大小.如果您不使用HTTP保持活动状态或调用多个主机,则确实会发起许多连接,但是所有连接都可以由单个线程客户端处理(可能有多个IO线程,具体取决于实现方式).

但是,最好始终限制每个主机的最大请求和每个域的最大请求,以避免特定主机或站点上的服务过载,并避免被阻塞.这就是HTTP客户端添加maxConnectionsPerXxx设置的原因.

I'm evaluating AsyncHttpClient for big loads (~1M HTTP requests). For each request I would like to invoke a callback using the AsyncCompletionHandler which will just insert the result into a blocking queue

My question is: if I'm sending asynchronous requests in a tight loop, how many threads will the AsyncHttpClient use? (I know you can set the max but apparently you take a risk of losing requests, I've seen it here)

I'm currently using the Netty implementation with these versions:

  • async-http-client v1.9.33
  • netty v3.10.5.Final

I don't mind using other versions if there are any optimization in later versions

EDIT:

I read that Netty uses the reactor pattern for reacting to HTTP responses which means it allocates a very small number of threads to act as selectors. This also means that the number of allocated threads doesn't increase with high requests volume. However that contradicts the need to set the max number of connections.

Can anyone explain what I'm missing?

Thanks in advance

解决方案

The AsyncHttpClient client (and other non-blocking IO clients for the matter), don't need to allocate a thread per request, and the client need not resize its thread pool even if you bombard it with requests. You do initiate many connections if you don't use HTTP keep-alive, or call multiple hosts, but it can all be handled by a single threaded client (there may be more than one IO thread, depending on the implementation).

However, it's always a good idea to limit the max requests per host, and max requests per domain, to avoid overloading a service on a specific host, or a site, and avoid getting blocked. This is why HTTP clients add a maxConnectionsPerXxx setting.

这篇关于AsyncHttpClient是否知道要为所有HTTP请求分配多少个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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