Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Long Polling - Timeout best practice

I play with Javascript AJAX and long-polling. Try to find best value for server response timeout.

I read many docs but couldn't find a detailed explanation for timeout.

Someone choose 20 secs, other 30 secs...

I use logic like on diagram HTTP Diagram

How can I choose better value for timeout? Can I use 5 minutes? Is it normal practice?

PS: Possible Ajax client internet connections: Ethernet RJ-45, WiFi, 3G, 4G, also, with NAT, Proxy.

I worry about connection can be dropped by third party in some cases by long timeout.

like image 710
Dmitry Avatar asked May 23 '16 12:05

Dmitry


People also ask

What is timeout in long polling?

Timeouts: Long polling requests need to remain pending or "hanging" until the server has something to send to the client. The timeout issues related to these pending requests are discussed under Section 5.5 . Caching: Caching mechanisms implemented by intermediate entities can interfere with long polling requests.

Is long polling scalable?

This model can be seen as a server-side local-polling model. Kafka is an example of technology implementing long polling model, and that's the reason why it is more scalable compared with RabbitMQ when there is a massive amount of messages and clients.

Is long polling good?

Long polling takes HTTP request/response polling and makes it more efficient, since repeated requests to a server wastes resources. For example, establishing a new connection, parsing the HTTP headers, a query for new data, response generation and delivery, and finally connection closure and clean up.


1 Answers

Maybe its your grasp of English which is the problem, but its the lifetime of the connection (time between connection opening and closing) you need to worry about more than the timeout (length of time with no activity after which the connection will be terminated).

Despite the existence of websockets, there is still a lot of deployed hardware which will drop connections regardless of activity (and some which will look for inactivity) where it thinks the traffic is HTTP or HTTPS - sometimes as a design fault, sometimes as a home-grown mitigation to sloloris attacks. That you have 3G and 4G clients means you can probably expect problems with a 5 minute lifespan.

Unfortunately there's no magic solution to knowing what will work universally. The key thing is to know how widely distributed your users are. If they're all on your LAN and connecting directly to the server, then you should be able to use a relatively large value, however setting the duration to unlimited will reveal any memory leaks in your app - sometimes its better to do refresh every now and again anyway.

Taking the case where there is infrastructure other than hubs and switches between your server and the clients, you need to provide a mechanism for detecting and re-establishing a dropped connection regardless of the length of time. When you have worked out how to do this, then:

  1. dropped connections are only a minor performance glitch and do not have a significant effect on the functionality

  2. it's trivial to then add the capability to log dropped connections and thereby determine the optimal connection time to eliminate the small problem described in (1)

like image 76
symcbean Avatar answered Sep 21 '22 23:09

symcbean