gpt4 book ai didi

java - 使用 Async HTTP Client netty 客户端会在高负载下爆炸?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:11:18 24 4
gpt4 key购买 nike

我在使用 AsyncHTTPClient 测试的某些代码时遇到了问题。这是一般配置

    this.config = (new AsyncHttpClientConfig.Builder())
.setAllowPoolingConnection(true)
.setAllowSslConnectionPool(true)
.addRequestFilter(new ThrottleRequestFilter(10))
.setMaximumConnectionsPerHost(20)
//.setMaximumConnectionsTotal(20)
.setRequestTimeoutInMs(100000)
.build();
this.client = new AsyncHttpClient(new NettyAsyncHttpProvider(config));

(请注意,由于使用 ThrottleRequestFilter 时出现了一些奇怪的错误,最大连接数被注释掉了,请参见此处 https://groups.google.com/forum/?fromgroups=#!topic/asynchttpclient/nEnQnPtCP2g)

下面是一些测试代码,

FileInputStream fstream = new FileInputStream("import.txt");
DataInputStream dstream = new DataInputStream(fstream);
BufferedReader reader = new BufferedReader(new InputStreamReader(dstream));
while ((line = reader.readLine()) != null) {
//Some code to build and create a request using the build function and then perform a GET operation. Request is built using Java's Future object from java.concurrent
}

当import.txt文件少于100行这样的简单文本

 196    242 3   881250949

一切正常,所有请求都通过,当您检查响应时一切正常。如果超过 100,我就会开始超时,如果超过 1000,我实际上会开始遇到 permGen 内存错误。

我认为 ThrottleRequestFilter 应该将最大线程数限制为 10,并且一次只能处理 10 个。为什么当文本文件超过 100 行时它会爆炸?

我也尝试过改用 Grizzly 实现,但同样以同样的方式失败。我开始怀疑这是我编写测试代码的方式,或者 Async HTTP Client 在创建大量请求时实际上存在一些问题。如果是这样,还有其他好的 java 异步 http 客户端吗?

最佳答案

您的问题是您在开始读取文件之前没有设置任何缓冲区大小。看看这个例子

private static final int EXT_DEFAULT_BUFFER_SIZE = 1024 * 8;
InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("Resource_Name");
public static String copyLargeExt(InputStream input) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[EXT_DEFAULT_BUFFER_SIZE];
int n = 0;
while(-1 != (n = input.read(buffer))) {
baos.write(buffer, 0, n);
}
return baos.toString();
}

关于java - 使用 Async HTTP Client netty 客户端会在高负载下爆炸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13280043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com