Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I still set ConnectionRequestTimeout on Apache HttpClient if I don't use a custom connection manager?

I am using Apache RequestConfig to configure some timeouts on my HttpClient.

RequestConfig config = RequestConfig.custom()
        .setConnectTimeout(timeout)
        .setSocketTimeout(timeout)
        .setConnectionRequestTimeout(timeout) // Can I leave this out..
        .build();

CloseableHttpClient httpClient = HttpClients.custom()
        //.setConnectionManager(connectionManager) // ..if I don't use this
        .setDefaultRequestConfig(config)
        .build();

Does it make any sense to call setConnectionRequestTimeout(timeout) even I don't have a custom Connection Manager / Pool set up?

As far as I understand, setConnectionRequestTimeout(timeout) is used to set the time to wait for a connection from the connection manager/pool.

Note that I am not setting a Connection Manager on the httpClient (see commented line).

like image 401
Diyarbakir Avatar asked Jun 16 '16 11:06

Diyarbakir


People also ask

Do we need to close HttpClient connection?

If you are processing HTTP responses manually instead of using a response handler, you need to close all the http connections by yourself.

What is Connectionrequesttimeout?

request timeout — a time period required to process an HTTP call: from sending a request to receiving a response. connection timeout — a time period in which a client should establish a connection with a server. socket timeout — a maximum time of inactivity between two data packets when exchanging data with a server.

What is default connection timeout for HttpClient?

The default value is 100,000 milliseconds (100 seconds). To set an infinite timeout, set the property value to InfiniteTimeSpan.

What is the difference between HttpClient and CloseableHttpClient?

CloseableHttpClient is the base class of the httpclient library, the one all implementations use. Other subclasses are for the most part deprecated. The HttpClient is an interface for this class and other classes. You should then use the CloseableHttpClient in your code, and create it using the HttpClientBuilder .


2 Answers

connectionRequestTimeout happens when you have a pool of connections and they are all busy, not allowing the connection manager to give you a connection to make the request.

So, The answer to your question of:

Does it make any sense to call setConnectionRequestTimeout(timeout) even I don't have a custom Connection Manager / Pool set up?

is YES.

This is because the default implementation has an internal connection pool. So, yes it makes sense to specify a connection request timeout. Actually it is a good, safe practice.

like image 142
Sampath Avatar answered Oct 23 '22 07:10

Sampath


Isuru's answer is mostly correct. The default connection manager is a PoolingHttpClientConnectionManager.
However, by default it will only have one connection in it's pool. If you are using your HttpClient synchronously from the same thread you should never encounter a situation where the ConnectionRequestTimeout will take effect.
If you are using the HttpClient from multiple threads then you might want to set it, but you would probably also want to increase the pool size, among other things.
For single-threaded httpclient use it is safe to leave it out.

like image 27
Magnus Avatar answered Oct 23 '22 07:10

Magnus