Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android http keepalive solution, but is it a permanent one?

I have a search button which accesses a server every time I press it. The problem is that if I press it multiple times in a row, it will work fine. However, if I wait 5 seconds, the first press won't work. It just doesn't receive a response. The second press will work and so will the subsequent button presses until I wait a certain amount of time (~5 seconds)

So basically, unless you're constantly searching for new things within 5 seconds, your first button press won't do anything.

This is solved with the

          System.setProperty("http.keepAlive", "false");

However, I've read that this gets rid of persistent connections where a new TCP connection has to be made every time I click that button. This is slower but it's not buggy.

Is this a bad fix? I've read that this doesn't really fix anything. Would love some help, thanks.

like image 254
volk Avatar asked Nov 14 '11 06:11

volk


1 Answers

You said:

However, if I wait 5 seconds, the first press won't work. It just doesn't receive a response.

I think the server does not even receive a request in your case. I think setting the http.keepAlive to false is a good fix. I have spent a fair amount of time investigating this issue and it is more than obvious that this is a bug in Android library.

The reason is that the server does not want to keep all those connections open due to potentially large number of them so it closes some of them from time to time. However, the connection pool used in Android library does not want to accept this fact and tries using the old closed connection anyway.

Unsetting the http.keepAlive is a workaround rather than a solution, however you do get a reliable HTTP handling, albeit with some performance cost.

like image 66
paulius_l Avatar answered Sep 27 '22 18:09

paulius_l