4

I get strange behavior from HttpClient with parameter CoreConnectionPNames.CONNECTION_TIMEOUT set to 1. I would expect that HttpGet request would fail, throwing connection timeout exception and yet they are successful. This seems irrational as it actually means that TCP handshake finished in less then 1 millisecond.

The httpclient version I'm using as can be seen in this pom.xml is

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.0.1</version>
        <type>jar</type>
    </dependency>

Here is the code:

import java.io.IOException;
import java.util.Random;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

public class TestNodeAliveness {
    private static Logger log = Logger.getLogger(TestNodeAliveness.class);

    public static boolean nodeBIT(String elasticIP) throws ClientProtocolException, IOException {
        try {
            HttpClient client = new DefaultHttpClient();

            // The time it takes to open TCP connection.
            client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1);

            // Timeout when server does not send data.
            client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);

            // Some tuning that is not required for bit tests.
            client.getParams().setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
            client.getParams().setParameter(CoreConnectionPNames.TCP_NODELAY, true);


            HttpUriRequest request = new HttpGet("http://" + elasticIP);
            HttpResponse response = client.execute(request);

            HttpEntity entity = response.getEntity();
            if(entity == null) { 
                return false;
            } else {
                System.out.println(EntityUtils.toString(entity));
            }

            // Close just in case.
            request.abort();

        } catch (Throwable e) {
            log.warn("BIT Test failed for " + elasticIP);
            e.printStackTrace();

            return false;
        }

        return true;
    }


    public static void main(String[] args) throws ClientProtocolException, IOException {
        nodeBIT("google.com?cant_cache_this=" + (new Random()).nextInt());
    }
}

How is this possible? Thank you.

2 Answers 2

5

The effective granularity of time-outs in all JVMs I have worked with is approximately 15-30ms. Even if the timeout is set to 1 socket I/O and connect requests often succeed if they take less than 15-30ms.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok so how do you effectively set a timeout that when if you dont recieve a response from the server after 5seconds you throw an exception?
1

If you set the timeout of the HttpGet,instead of the HttpClient, it seems to work much better.

    HttpGet httpget = new HttpGet("http://www.apache.org/");

    RequestConfig Default = RequestConfig.DEFAULT;

    RequestConfig requestConfig = RequestConfig.copy(Default)
            .setSocketTimeout(5000)
            .setConnectTimeout(5000)
            .setConnectionRequestTimeout(5000)
            .build();
    httpget.setConfig(requestConfig);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.