最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - I have set the timeout to 10 seconds, but I'm still receiving a response after 10 seconds without a timeout occur

programmeradmin4浏览0评论

I have created a server that implements a queue on the server-side, where requests are intentionally held for 11 seconds before a response is sent. For testing purposes, I configured a client application to make requests to this server with a 10-second timeout.

Issue:

Despite setting the client-side timeout to 10 seconds, I am still receiving responses for some requests after the 10-second timeout threshold, even though the server is deliberately holding the request for 11 seconds.

    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: "+pr`enter code here`ocessingTime);
                }

                @Override
                public void failed(Exception ex) {
                
                }

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

OutPut:

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

             

I have created a server that implements a queue on the server-side, where requests are intentionally held for 11 seconds before a response is sent. For testing purposes, I configured a client application to make requests to this server with a 10-second timeout.

Issue:

Despite setting the client-side timeout to 10 seconds, I am still receiving responses for some requests after the 10-second timeout threshold, even though the server is deliberately holding the request for 11 seconds.

    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: "+pr`enter code here`ocessingTime);
                }

                @Override
                public void failed(Exception ex) {
                
                }

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

OutPut:

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

             
Share Improve this question edited 10 hours ago ok2c 27.6k5 gold badges65 silver badges73 bronze badges asked 2 days ago HiteshHitesh 2812 gold badges5 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
  1. The socket timeout is NOT a deadline. It represents a maximum period of inactivity between consecutive i/o events. If one sets to the socket timeout to 5 seconds and the opposite endpoints sends a packet every 4 seconds, the message exchange will never time out.
  2. The async (NIO based) message transport used by async HttpClient has a timeout precision of one seconds by default. In some cases, for instance under load, the timeout precision may be off by several seconds. One can increase the precision of timeout events at the cost of greater CPU utilization.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论