我有一个拥有 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
字段的错误。
关于java - 为什么 async-http-client 不限制我的请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18351816/