在什么情况下我的浏览器会尝试重新使用 TCP 连接来处理多个请求?
我使用的是 Firefox,但我想知道浏览器通常如何决定这一点。
似乎当我在短时间内两次访问相同的 URL 时,我的浏览器会尝试为两个请求重新使用相同的 TCP 连接(这称为保持活动)。但是,当我访问两个不同的 URL(但仍然由同一服务器提供服务)时,浏览器有时会决定为每个请求打开一个新连接。显然,浏览器不使用每个 URL 一个连接的策略。
我问这个问题是因为我正在尝试实现一个使用长轮询的网络服务。我可以想象用户可能希望在同一浏览器的多个选项卡中打开此服务。然而,使用 keep-alive 时,直到第一个长轮询请求完成后才会发送第二个长轮询请求(至少在 Firefox 中),因为浏览器试图将它们都推入同一个套接字,这是我没想到的。设计了服务。即使浏览器实现了管道传输,我也无法在响应第一个请求之前响应第二个请求,因为 HTTP 要求我按顺序完成响应。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 HTTP/1.1 时,默认情况下,TCP 连接保持打开状态以供重用。这是为了比每个请求启动一个新连接更好的性能。连接可以重复使用但是任何一方都可以随时关闭连接。
您应该阅读 HTTP1.1 和有关持久连接的部分。
在您的情况下,它甚至没有使用 HTTP 管道(未得到广泛支持),因为下一个请求是在第一个请求之后发送的。
浏览器有一个连接池,并按主机名重用它。一般来说,浏览器不应该为多个主机名重用单个连接,即使这些主机名实际上解析为相同的 IP 地址。
大多数浏览器允许用户配置或覆盖每台服务器的持久连接数量;大多数现代浏览器默认为六个。如果 Firefox 由于已经存在活动连接而真正阻止第二个请求,则这是 Firefox 中的错误,应在其错误跟踪系统中归档。但如果存在这样的错误,我想您会看到许多网站被破坏。
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.