【问题标题】:Using Async HTTP Client netty client bombs out with high load?使用异步 HTTP 客户端 netty 客户端会在高负载下崩溃?
【发布时间】:2023-03-22 01:20:02
【问题描述】:

我在使用 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 客户端吗?

【问题讨论】:

    标签: java netty asynchttpclient


    【解决方案1】:

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

    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();
        }
    

    【讨论】:

    • 我认为缓冲区大小已初始化为某个默认值?还是不是这样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    相关资源
    最近更新 更多