You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache HttpAsyncClient设置10秒超时后仍收到超时时长响应的问题求助

Apache HttpAsyncClient设置10秒超时后仍收到超时时长响应的问题求助

大家好,我遇到一个关于Apache HttpAsyncClient超时配置的问题,想请各位帮忙分析下。

我搭建了一个测试用的服务器,它会将收到的请求故意延迟11秒再返回响应。为了测试超时逻辑,我在客户端用HttpAsyncClient配置了10秒的超时时间,本以为所有超过10秒的请求都会触发超时异常,但实际运行后发现,部分请求居然在超出10秒阈值后依然收到了200成功响应,比如输出里的14286毫秒、16330毫秒这类情况,完全不符合预期。

以下是我的客户端代码:

PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create()
              .setTlsStrategy(ClientTlsStrategyBuilder.create()
                      .setSslContext(SSLContexts.createSystemDefault())
                      .setTlsVersions(TLS.V_1_3)
                      .build())
              .setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT)
              .setConnPoolPolicy(PoolReusePolicy.LIFO)
              .setDefaultConnectionConfig(ConnectionConfig.custom()
                     .setSocketTimeout(Timeout.ofMilliseconds(10000))
                     .setConnectTimeout(Timeout.ofMilliseconds(10000))
                     .setTimeToLive(TimeValue.ofMilliseconds(10000))
                     .build())
              .setDefaultTlsConfig(TlsConfig.custom()
                     .setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
                     .setHandshakeTimeout(Timeout.ofMinutes(1))
                     .build())
              .build();
      connectionManager.setDefaultMaxPerRoute(100);
      connectionManager.setMaxTotal(100);

    CloseableHttpAsyncClient asyncclient = HttpAsyncClients.custom()
          .setConnectionManager(connectionManager)
          .setIOReactorConfig(IOReactorConfig.custom()
                  .setSoTimeout(Timeout.ofMilliseconds(10000))
                  .build())
          .build();

    asyncclient.start();

 futures.add(asyncclient.execute(httpRequest, new FutureCallback<>() {
            @Override
            public void completed(SimpleHttpResponse response) {
                long processingTime = System.currentTimeMillis() - startTime;
                System.out.println("Request "+ requestId+" completed with status: "+response.getCode()+" processingTime: "+processingTime);
            }

            @Override
            public void failed(Exception ex) {
            
            }

            @Override
            public void cancelled() {
            }
        }));

实际运行输出:

Request 33 completed with status: 200 processingTime: 2181
Request 29 completed with status: 200 processingTime: 2193
Request 551 completed with status: 200 processingTime: 9175
Request 935 completed with status: 200 processingTime: 14286
Request 1006 completed with status: 200 processingTime: 14284
Request 1122 completed with status: 200 processingTime: 16330
Request 1076 completed with status: 200 processingTime: 16332
Request 1084 completed with status: 200 processingTime: 16333

我已经在连接管理器的默认连接配置里设置了Socket超时和连接超时,同时也在IOReactorConfig里配置了SoTimeout,但为什么还是会出现请求超时后仍收到响应的情况呢?麻烦各位帮忙找找问题所在,非常感谢!

备注:内容来源于stack exchange,提问作者Hitesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 10:35:27