We have an app with a long polling scheme over HTTP (although this question could apply to any TCP-based protocol). Our timeout is fairly high, 30 minutes or so.
What we see sometimes is mobile devices hop from IP to IP fairly often, every minute or so, and this causes dozens of long-lived sockets to pile up on the server. Can’t help but think this is causing more load than neccessary.
So I am guessing that some IP gateways are better than others at closing connections when a device hops off. The strategies I can think of to deal with this are:
- Decrease the timeout (increasing battery life on the device)
- Close the last active connection when a user reconnects (requires cookie or user ID tracking)
I would look into closing the last active connection using a cookie or some sort of ID in you’re server. Yes it’s more work, but as soon as the user hops addresses, you can find the old socket and clean up the resources right of way. It should be fairly easy to tie to a username or something like that.
The other problem you may run into even if the user equipment isn’t hopping addresses, some mobile networks and maybe you’re own network may have a statefull firewall that will clean up unused sockets, which will cause connectivity problems since a new connection will require the syn/syn-ack again. Just something to keep in mind if you’re noticing connectivity problems.
If you do decide to play with keep alives, please don’t be too aggressive, chatty applications are the plague of Mobile networks, and ones that hammer the network when it lose connection to the server can cause all sorts of problems for the network (and you if the carrier catches on). Atleast have a sort of backoff mechanism to retrying connectivity, and maybe even try to find out why the device is switching IP addresses every minute. If it’s functioning properly that shouldn’t occur.
***I work for a mobile operator in Canada, however, my comments do not reflect the position of my employer.
If you can, turn on TCP keepalive on the sockets, and give them a fairly low timer (e.g. every 1-5 minute). As long as you’re reading from the socket, you’ll detect an unreachable peer faster – and with less resources utiilization on the phone than decreasing your 30 minute application timeout.