在什么情况下我的浏览器会尝试为多个请求重用一个 TCP 连接? [英] Under what circumstances will my browser attempt to re-use a TCP connection for multiple requests?

查看:36
本文介绍了在什么情况下我的浏览器会尝试为多个请求重用一个 TCP 连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Firefox,但我想知道一般浏览器是如何决定这一点的.

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

似乎当我在短时间内访问同一个 URL 两次时,我的浏览器尝试为两个请求重新使用 TCP 相同连接(这称为保持活动).但是,当我访问两个不同的 URL(但仍由同一服务器提供)时,浏览器有时决定为每个请求打开一个新连接.显然,浏览器没有使用一个连接每个 URL 的策略.

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.

我问这个是因为我正在尝试实现一个使用长轮询的网络服务.我可以想象用户可能希望在同一浏览器的多个选项卡中打开此服务.但是,使用保持活动状态,第二个长轮询请求在第一个完成之前不会发送(至少在 Firefox 中),因为浏览器试图将它们都推入同一个套接字,这是我没想到的设计了服务.即使浏览器实现了流水线,我也无法在响应第一个请求之前响应第二个请求,因为 HTTP 要求我按顺序完成响应.

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.

推荐答案

在使用 HTTP/1.1 时,默认情况下,TCP 连接保持打开状态以供重用.这是为了比每个请求启动一个新连接更好的性能.连接可以重复使用连接可以随时被任何一方关闭.

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.

您应该阅读 HTTP1.1 和有关持久连接的部分.

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

在您的情况下,它甚至不使用 HTTP 流水线(未得到广泛支持),因为下一个请求是在第一个响应之后发送的.

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.

浏览器有一个连接池,可以按主机名重用它.一般来说,浏览器不应为多个主机名重用单个连接,即使这些主机名实际上解析为相同的 IP 地址.

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.

大多数浏览器允许用户配置或覆盖每台服务器的持久连接数;大多数现代浏览器默认为 6.如果 Firefox 真正 阻止第二个请求,因为已经有一个连接处于活动状态,这是 Firefox 中的一个错误,应该在他们的错误跟踪系统中提交.但是,如果存在这样的错误,我想您会看到许多站点被破坏.

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.

这篇关于在什么情况下我的浏览器会尝试为多个请求重用一个 TCP 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆