最近同事"神经病"同学的项目中, 发现一个问题.

用firefox打开多个tab, 每个tab都去请求同一个URL(尽量同时), 会发现, 这些请求, 会被浏览器串行化.

也就是说, 浏览器会在第一个页面请求结束以后, 再发起第二个请求,

比如对于如下脚本:

<?php
error_log("start . " . getmypid(). "\n");
sleep(5);
error_log("end . " . getmypid() . "\n");


用firefox打开多个tab同时请求这个脚本, 观察error_log

start . 3734\n
end . 3734\n
start . 3733\n
end . 3733\n
start . 3733\n
end . 3733\n


多方求证这个问题, 但无果, 得到的都是对这个现象的描述, 但没有得到为什么浏览器会这么做, 或者是什么让它们这么设计, 后来得到在mod_perl中的一段叙说:

> Certain browsers will serialize requests to the same URL if accessed from different windows. For example if you have a CGI script that does:
>
> for (1..100) {
>
> print "$$: $_\n";
>
> warn "$$: $_\n";
>
> sleep 1;
>
> }
>
> And two concurrent requests are issued from different windows of the same browser (for those browsers that have this bug/feature), the browser will actually issue only one request and won't run the second request till the first one is finished. The debug printing to the error_log file helps to understand the serialization issue.
>
> Solution? Find a UA that doesn't have this feature, especially if a command line UA will do (LWP comes to mind). As of this writing, opera 6, mozilla 1.0 on linux have this problem, whereas konqueror 3 and lynx don't.

随后, 我验证了chrome也有这个问题, 不过上面的文字也说了, 这些是特定浏览器的特定行为, 比如命令行的wget, ab之类的就没有这个特定行为.

我也尝试翻看HTTP 1.1RFC和webkit的源代码, 不过也无奈找不到线索...

bitbull说可能是为了复用一个链接, 不过此时通过netstat, 会发现本地确实是打开了多个链接, 所以应该不是为了复用一个链接而这么设置的.

各位如果有对这个特性详细描述, 并且解释为什么这么设计的文档, 请一定转发一个给我.

thanks