Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under what circumstances will my browser attempt to re-use a TCP connection for multiple requests?

I am using Firefox, but I'd like to know how browsers decide this in general.

It seems that when I access the same URL twice in a short amount of time, my browser tries to re-use the TCP same connection for both requests (this is called keep-alive). However, when I access two different URLs (but still served by the same server), the browser sometimes decides to open up a new connection for each request. Obviously, the browser does not use a one-connection-per-URL policy.

I am asking this because I am trying to implement a web service that uses long polling. I can imagine that a user might want to open this service in multiple tabs on the same browser. However, with keep-alive, the second long poll request does not get sent until the first one completes (at least in Firefox), because the browser is trying to shove both of them into the same socket, which I did not expect when I designed the service. Even if the browser implements pipe-lining, there is no way that I can respond to the second request before I respond to the first, because HTTP mandates that I complete the responses in order.

like image 430
Mark Avatar asked Sep 05 '11 15:09

Mark


People also ask

How does browser handle multiple requests?

For multiple requests (A, B, …, Z) fired (almost) at the same time in the browser, this means that the browser sends request A and queues the rest. After a while, the response for A comes and the request B can be sent. When the response for B comes, it sends the next one, and so on.

How does TCP manage multiple sessions?

If multiple applications or network processes are running, each may set up a connection to a different computer. For example, you can simultaneously open multiple Web Browsers and connect to multiple sites. TCP uses flow controls, sliding windows, and various other mechanisms to manage sessions.

Does HTTP use multiple TCP connections?

HTTP may use different TCP connection for different objects of a webpage if non-persistent connections are used. FTP uses two TCP connections, one for data and another control. So , option (1) is true .

Is every HTTP request a new TCP connection?

In HTTP/0.9 (not used anymore), each request used a separate TCP connection, and the end of a response was signalled by closing the connection. In HTTP/1.0, separate connections are still the official default.


1 Answers

When using HTTP/1.1, by default, the TCP connections are left open for reuse. This is for better performance than starting a new connection per request. The connection can be reused but the connection could close at any time by any of the parties.

You should read HTTP1.1 and the part on persistent connections.

In your case it is not even using HTTP pipelining (not broadly supported) because the next request is sent after the response of the first.

The browsers have a connection pool and reuse it per hostname. Generally speaking, a browser should not reuse a single connection for multiple hostnames, even if those hostnames actually resolve to the same IP address.

Most browsers allow the user to configure or override the number of persistent connections per server; most modern browsers default to six. If Firefox is truly blocking the second request because there's already a connection active, this is a bug in Firefox and should be filed in their bug tracking system. But if such a bug existed, I think you'd see many sites broken.

like image 114
Cratylus Avatar answered Sep 22 '22 06:09

Cratylus