Sunday, January 20, 2019

Timeouts in Http Connection

This blog talks about how to configure timeout parameters in creating http connection. Irrespective of the the destination, the configuration is generic and applicable for any type of remote host internal or external.

http.connection.timeout-  Time to establish connection with the Remote host
http.socket.timeout: It's the waiting time for the data. Max time it will wait for between two packets
http.connection-manager.timeout: Time to wait for getting connection from connection manger/ Pool


Hard timeout: We don’t want the request to be executed for more than x sec. Eg. if file download request is taking time more than 5s

HttpGet getMethod = new HttpGet(
  "http://www.domainname/respurces");

int hardTimeout = 5; // seconds
TimerTask task = new TimerTask() {
    @Override
    public void run() {
        if (getMethod != null) {
            getMethod.abort();
        }
    }
};
new Timer(true).schedule(task, hardTimeout * 1000);

HttpResponse response = httpClient.execute(getMethod);
System.out.println(
  "HTTP Status of response: " + response.getStatusLine().getStatusCode());


Timeout and DNS Round Robin : when the same domain mapped to multiple IP addresses.
HttpClient gets the list of IP routes to that domain
It tries the first one – that times out (with the timeouts we configure)
It tries the second one – that also times out
- and so on …

DEBUG o.a.h.i.c.HttpClientConnectionOperator - Connecting to www.google.com/173.194.34.212:81
DEBUG o.a.h.i.c.HttpClientConnectionOperator -
Connect to www.google.com/173.194.34.212:81 timed out. Connection will be retried using another IP address

No comments:

Post a Comment