Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why `setConnectionRequestTimeout` doesn't stop my 1 min get request?

I have this code:

    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(40 * 1000)
            .setConnectionRequestTimeout(40 * 1000)
            .setSocketTimeout(40 * 1000)
            .build();
    client = HttpClientBuilder
            .create()
            .setDefaultRequestConfig(requestConfig)
            .build();
}

and

    try {

        Stopwatch stopWatch = Stopwatch.createStarted();
        response = client.execute(new HttpGet(routingRequestUrl));
        stopWatch.stop();

    } catch (Exception e) {
        answer.errorMsg = e.getMessage();
        answer.latency =  null;
    }

when my client configuration is as doesn't contain .setSocketTimeout(40 * 1000) - the stopWatch shows request can take more then 1 minute.

It happens when I try setConnectTimeout and setConnectionRequestTimeout each alone or all together.

Why is only .setSocketTimeout(40 * 1000) effectively checks timeout 40 seconds? and the other alone not?

These are the prints:

Read timed out

Timeout waiting for connection from pool

Is the first triggered by setConnectionRequestTimeout and the second by setSocketTimeout ?

like image 684
Elad Benda2 Avatar asked Jul 24 '15 13:07

Elad Benda2


1 Answers

https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.html

getConnectionRequestTimeout()

Returns the timeout in milliseconds used when requesting a connection from the connection manager.

getConnectTimeout()

Determines the timeout in milliseconds until a connection is established.

getSocketTimeout()

Defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets).

__

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

connectTimeout happens when establishing the connection. For instance while doing the tcp handshake.

socketTimeout like the description says, is the timeout while waiting for data. Usually happens when your server is slow.

like image 103
Paulo Santos Avatar answered Sep 28 '22 01:09

Paulo Santos