流行浏览器中允许多少并发 AJAX (XmlHttpRequest) 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/561046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?
提问by Michael Gundlach
In Firefox 3, the answer is 6 per domain: as soon as a 7th XmlHttpRequest (on any tab) to the same domain is fired, it is queued until one of the other 6 finish.
在 Firefox 3 中,答案是每个域 6 个:只要向同一个域发出第 7 个 XmlHttpRequest(在任何选项卡上),它就会排队,直到其他 6 个中的一个完成。
What are the numbers for the other major browsers?
其他主要浏览器的数字是多少?
Also, are there ways around these limits without having my users modify their browser settings? For example, are there limits to the number of jsonp requests (which use script tag injection rather than an XmlHttpRequest object)?
另外,有没有办法绕过这些限制,而不必让我的用户修改他们的浏览器设置?例如,jsonp 请求(使用脚本标记注入而不是 XmlHttpRequest 对象)的数量是否有限制?
Background: My users can make XmlHttpRequests from a web page to the server, asking the server to run ssh commands on remote hosts. If the remote hosts are down, the ssh command takes a few minutes to fail, eventually preventing my users from performing any further commands.
背景:我的用户可以从网页向服务器发出 XmlHttpRequest,要求服务器在远程主机上运行 ssh 命令。如果远程主机关闭,ssh 命令需要几分钟才能失败,最终阻止我的用户执行任何进一步的命令。
采纳答案by Bob
One trick you can use to increase the number of concurrent connections is to host your images from a different sub domain. These will be treated as separate requests, each domain is what will be limited to the concurrent maximum.
您可以用来增加并发连接数的一个技巧是托管来自不同子域的图像。这些将被视为单独的请求,每个域都将被限制为并发最大值。
IE6, IE7 - have a limit of two. IE8 is 6 if you have a broadband - 2 (if it's a dial up).
IE6、IE7 - 限制为两个。如果您有宽带,IE8 是 6 - 2(如果是拨号上网)。
回答by Kevin Hakanson
The network results at Browserscopewill give you both Connections per Hostnameand Max Connectionsfor popular browsers. The data is gathered by running tests on users "in the wild," so it will stay up to date.
Browserscope的网络结果将为您提供每个主机名的连接数和流行浏览器的最大连接数。数据是通过对“野外”用户进行测试来收集的,因此它会保持最新状态。
回答by brianegge
With IE6 / IE7 one can tweak the number of concurrent requests in the registry. Here's how to set it to four each.
使用 IE6 / IE7 可以调整注册表中并发请求的数量。以下是如何将其设置为四个。
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"MaxConnectionsPerServer"=dword:00000004
"MaxConnectionsPer1_0Server"=dword:00000004
回答by xmorera
I just checked with www.browserscope.organd with IE9 and Chrome 24 you can have 6 concurrent connections to a single domain, and up to 17 to multiple ones.
我刚刚查看了www.browserscope.org和 IE9 和 Chrome 24,你可以有 6 个并发连接到单个域,最多 17 个并发连接到多个。
回答by Luis Siquot
I have writen a single file AJAX tester. Enjoy it!!! Just because I have had problems with my hosting provider
我写了一个单文件 AJAX 测试器。好好享受!!!仅仅因为我的托管服务提供商有问题
<?php /*
Author: Luis Siquot
Purpose: Check ajax performance and errors
License: GPL
site5: Please don't drop json requests (nor delay)!!!!
*/
$r = (int)$_GET['r'];
$w = (int)$_GET['w'];
if($r) {
sleep($w);
echo json_encode($_GET);
die ();
} //else
?><head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var _settimer;
var _timer;
var _waiting;
$(function(){
clearTable();
$('#boton').bind('click', donow);
})
function donow(){
var w;
var estim = 0;
_waiting = $('#total')[0].value * 1;
clearTable();
for(var r=1;r<=_waiting;r++){
w = Math.floor(Math.random()*6)+2;
estim += w;
dodebug({r:r, w:w});
$.ajax({url: '<?php echo $_SERVER['SCRIPT_NAME']; ?>',
data: {r:r, w:w},
dataType: 'json', // 'html',
type: 'GET',
success: function(CBdata, status) {
CBdebug(CBdata);
}
});
}
doStat(estim);
timer(estim+10);
}
function doStat(what){
$('#stat').replaceWith(
'<table border="0" id="stat"><tr><td>Request Time Sum=<th>'+what+
'<td> /2=<th>'+Math.ceil(what/2)+
'<td> /3=<th>'+Math.ceil(what/3)+
'<td> /4=<th>'+Math.ceil(what/4)+
'<td> /6=<th>'+Math.ceil(what/6)+
'<td> /8=<th>'+Math.ceil(what/8)+
'<td> (seconds)</table>'
);
}
function timer(what){
if(what) {_timer = 0; _settimer = what;}
if(_waiting==0) {
$('#showTimer')[0].innerHTML = 'completed in <b>' + _timer + ' seconds</b> (aprox)';
return ;
}
if(_timer<_settimer){
$('#showTimer')[0].innerHTML = _timer;
setTimeout("timer()",1000);
_timer++;
return;
}
$('#showTimer')[0].innerHTML = '<b>don\'t wait any more!!!</b>';
}
function CBdebug(what){
_waiting--;
$('#req'+what.r)[0].innerHTML = 'x';
}
function dodebug(what){
var tt = '<tr><td>' + what.r + '<td>' + what.w + '<td id=req' + what.r + '> '
$('#debug').append(tt);
}
function clearTable(){
$('#debug').replaceWith('<table border="1" id="debug"><tr><td>Request #<td>Wait Time<td>Done</table>');
}
</script>
</head>
<body>
<center>
<input type="button" value="start" id="boton">
<input type="text" value="80" id="total" size="2"> concurrent json requests
<table id="stat"><tr><td> </table>
Elapsed Time: <span id="showTimer"></span>
<table id="debug"></table>
</center>
</body>
Edit:
r means row and w waiting time.
When you initially press start button 80 (or any other number) of concurrent ajax request are launched by javascript, but as is known they are spooled by the browser. Also they are requested to the server in parallel (limited to certain number, this is the fact of this question). Here the requests are solved server side with a random delay (established by w). At start time all the time needed to solve all ajax calls is calculated. When test is finished, you can see if it took half, took third, took a quarter, etc of the total time, deducting which was the parallelism on the calls to the server. This is not strict, nor precise, but is nice to see in real time how ajaxs calls are completed (seeing the incoming cross). And is a very simple self contained script to show ajax basics.
Of course, this assumes, that server side is not introducing any extra limit.
Preferably use in conjunction with firebug net panel (or your browser's equivalent)
编辑:
r 表示行,w 表示等待时间。
当您最初按下开始按钮时,80 个(或任何其他数量的)并发 ajax 请求由 javascript 启动,但众所周知,它们是由浏览器假脱机的。它们也被并行请求到服务器(仅限于一定数量,这是这个问题的事实)。这里请求是在服务器端以随机延迟(由 w 建立)解决的。在开始时,计算解决所有 ajax 调用所需的所有时间。测试完成后,您可以查看是否花费了总时间的一半、三分之一、四分之一等,减去对服务器调用的并行度。这并不严格,也不精确,但很高兴实时查看 ajaxs 调用是如何完成的(查看传入的交叉)。并且是一个非常简单的自包含脚本,用于显示 ajax 基础知识。
当然,这是假设服务器端没有引入任何额外限制。
最好与萤火虫网面板(或您的浏览器的等效物)结合使用
回答by Kevin Hakanson
According to IE 9 – What's Changed?on the HttpWatch blog, IE9 still has a 2 connection limit when over VPN.
根据IE 9 – 有什么变化?在 HttpWatch 博客上,IE9 在通过 VPN 时仍然有 2 个连接限制。
Using a VPN Still Clobbers IE 9 Performance
We previously reported about the scaling back of the maximum number of concurrent connections in IE 8 when your PC uses a VPN connection. This happened even if the browser traffic didn't go over that connection.
Unfortunately, IE 9 is affected by VPN connections in the same way:
使用 VPN 仍然破坏 IE 9 性能
我们之前曾报道过当您的 PC 使用 VPN 连接时 IE 8 中最大并发连接数的缩减。即使浏览器流量没有通过该连接,也会发生这种情况。
不幸的是,IE 9 以同样的方式受到 VPN 连接的影响:
回答by Endless
Wrote my own test. tested the code on stackoverflow, works fine tells me that chrome/FF can do 6
自己写的测试。在 stackoverflow 上测试了代码,工作正常告诉我 chrome/FF 可以做 6
var change = 0;
var simultanius = 0;
var que = 20; // number of tests
Array(que).join(0).split(0).forEach(function(a,i){
var xhr = new XMLHttpRequest;
xhr.open("GET", "/?"+i); // cacheBust
xhr.onreadystatechange = function() {
if(xhr.readyState == 2){
change++;
simultanius = Math.max(simultanius, change);
}
if(xhr.readyState == 4){
change--;
que--;
if(!que){
console.log(simultanius);
}
}
};
xhr.send();
});
it works for most websites that can trigger readystate change event at different times. (aka: flushing)
它适用于大多数可以在不同时间触发就绪状态更改事件的网站。(又名:潮红)
I notice on my node.js server that i had to output at least 1025 bytes to trigger the event/flush. otherwise the events would just trigger all three state at once when the request is complete so here is my backend:
我注意到在我的 node.js 服务器上我必须输出至少 1025 个字节才能触发事件/刷新。否则,当请求完成时,事件只会立即触发所有三个状态,所以这是我的后端:
var app = require('express')();
app.get("/", function(req,res) {
res.write(Array(1025).join("a"));
setTimeout(function() {
res.end("a");
},500);
});
app.listen(80);
Update
更新
I notice that You can now have up to 2x request if you are using both xhr and fetch api at the same time
我注意到如果您同时使用 xhr 和 fetch api,您现在最多可以有 2x 请求
var change = 0;
var simultanius = 0;
var que = 30; // number of tests
Array(que).join(0).split(0).forEach(function(a,i){
fetch("/?b"+i).then(r => {
change++;
simultanius = Math.max(simultanius, change);
return r.text()
}).then(r => {
change--;
que--;
if(!que){
console.log(simultanius);
}
});
});
Array(que).join(0).split(0).forEach(function(a,i){
var xhr = new XMLHttpRequest;
xhr.open("GET", "/?a"+i); // cacheBust
xhr.onreadystatechange = function() {
if(xhr.readyState == 2){
change++;
simultanius = Math.max(simultanius, change);
}
if(xhr.readyState == 4){
change--;
que--;
if(!que){
document.body.innerHTML = simultanius;
}
}
};
xhr.send();
});
回答by cbp
I believe there is a maximum number of concurrent http requests that browsers will make to the same domain, which is in the order of 4-8 requests depending on the user's settings and browser.
我相信浏览器会对同一个域发出的并发 http 请求的最大数量是 4-8 个请求,具体取决于用户的设置和浏览器。
You could set up your requests to go to different domains, which may or may not be feasible. The Yahoo guys did a lot of research in this area, which you can read about (here). Remember that every new domain you add also requires a DNS lookup. The YSlow guys recommend between 2 and 4 domains to achieve a good compromise between parallel requests and DNS lookups, although this is focusing on the page's loading time, not subsequent AJAX requests.
您可以设置访问不同域的请求,这可能可行,也可能不可行。雅虎人在这个领域做了很多研究,你可以阅读(这里)。请记住,您添加的每个新域还需要进行 DNS 查找。YSlow 建议使用 2 到 4 个域来实现并行请求和 DNS 查找之间的良好折衷,尽管这侧重于页面的加载时间,而不是后续的 AJAX 请求。
Can I ask why you want to make so many requests? There is good reasons for the browsers limiting the number of requests to the same domain. You will be better off bundling requests if possible.
我能问一下你为什么要提出这么多要求吗?浏览器限制对同一域的请求数量是有充分理由的。如果可能,您最好将请求捆绑在一起。

