Apache HttpClient 4.3 - setting connection idle timeout

后端 未结 2 453
没有蜡笔的小新
没有蜡笔的小新 2021-02-07 08:24

What\'s the shortest way to configure connection idle timeout on Apache HttpClient 4.3 version?

I\'ve looked in the documentation and couldn\'t find anything. My goal is

2条回答
  •  旧时难觅i
    2021-02-07 09:10

    You can't set an idle connection timeout in the config for Apache HTTP Client. The reason is that there is a performance overhead in doing so.

    The documentation clearly states why, and gives an example of an idle connection monitor implementation you can copy. Essentially this is another thread that you run to periodically call closeIdleConnections on HttpClientConnectionManager

    http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html

    One of the major shortcomings of the classic blocking I/O model is that the network socket can react to I/O events only when blocked in an I/O operation. When a connection is released back to the manager, it can be kept alive however it is unable to monitor the status of the socket and react to any I/O events. If the connection gets closed on the server side, the client side connection is unable to detect the change in the connection state (and react appropriately by closing the socket on its end). HttpClient tries to mitigate the problem by testing whether the connection is 'stale', that is no longer valid because it was closed on the server side, prior to using the connection for executing an HTTP request. The stale connection check is not 100% reliable and adds 10 to 30 ms overhead to each request execution. The only feasible solution that does not involve a one thread per socket model for idle connections is a dedicated monitor thread used to evict connections that are considered expired due to a long period of inactivity. The monitor thread can periodically call ClientConnectionManager#closeExpiredConnections() method to close all expired connections and evict closed connections from the pool. It can also optionally call ClientConnectionManager#closeIdleConnections() method to close all connections that have been idle over a given period of time.

提交回复
热议问题