java - 为什么 async-http-client 不限制我的请求?

标签 java queue akka future asynchttpclient

我有一个拥有 AsyncHttpClient 的 Akka actor。这个 actor 必须处理很多异步请求。因为我的系统不能同时处理数千个请求,所以我需要限制并发请求数。

现在,我正在这样做:

AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().setAllowPoolingConnection(true)
                                                                  .addRequestFilter(new ThrottleRequestFilter(32))
                                                                  .setMaximumConnectionsPerHost(16)
                                                                  .setMaxRequestRetry(5)
                                                                  .build();
final AsyncHttpClient httpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(config));

当我的 Actor 收到一条消息时,我会像这样使用客户端:

Future<Integer> f = httpClient.prepareGet(url).execute(
   new AsyncCompletionHandler<Integer>() {
      @Override
      public Integer onCompleted(Response response) throws Exception {
         // handle successful request
      }

      @Override
      public void onThrowable(Throwable t){
         // handle failed request
      }
   }
);

问题是请求永远不会被放入客户端队列,并且所有的处理都像配置无关紧要一样。为什么这不能正常工作?

最佳答案

来自维护者:

setMaxConnectionsPerHost only caps the number of connections that can be open to a given host. There's no built-in queuing mechanism for requests that might need a connection while there's none available.

所以基本上,这是一个硬性限制。此外,我认为在 1.9.10 之前的库版本中,maximumConnectionsPerHost 字段未被代码正确使用以限制每个主机的并发连接数。相反,存在客户端仅查看 maximumConnectionsTotal 字段的错误。

Link to issue referenced on GitHub

关于java - 为什么 async-http-client 不限制我的请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18351816/

相关文章:

java - 安卓 : execute() method of AsyncTask cannot be applied. 为什么?

ios - 如何在同一线程上调度异步以进行串行处理

c# - 留在队列中的项目的替代方案(线程消费者生产者)

scala - 如何在 Akka actor 中等待文件上传流完成

scala - Akka 流 : Can not write to file sink

java - Spring 数据休息: Null Pointer If-Modified-Since Header Present and Resource Not Auditable

Java 控制台日志未使用 Applet 写入磁盘

java - 我无法弄清楚我的代码、队列和java中的某些部分

scala - Akka FSM actor 和循环路由

java - 使用 Spring-mvc 和 thymeleaf 从实体填充下拉列表