SSE(EventSource的):为什么不超过6连接的更多? [英] SSE(EventSource): why no more than 6 connections?

查看:925
本文介绍了SSE(EventSource的):为什么不超过6连接的更多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看有多少同时SSE(又名EventSource的)我可以建立连接,超载我的机器前。但与Firefox(火狐18或Firefox 20)测试它停在6个连接:附加的连接不提供任何错误,但不发送任何数据。 (在Firebug中,我可以有看到他们,等待连接。)
铬25还停在6个连接,所以没有歌剧12.15。
但它似乎并没有被服务器端的限制(我使用Apache + PHP),因为我可以运行在同一时间(即18连接)所有三种浏览器,以及所有来自同一IP地址的。
(服务器和客户端在同一台计算机上,但使用的是172.16.x.x地址,而不是127.0.0.1。)

所以,我设置的测试与CORS,并试图连接到另一台服务器,其中有一个全球IP。这一次,我得到的Firefox 12的连接。这表明它毕竟是Apache配置?不,歌剧还是只得到6连接。 (为Chrome没有号码,CORS不出现工作。)我也可以运行连接到两台服务器,共计18个连接(但从来没有任何更多)在Firefox和Opera中一共有12个。

作为第3的测试,我搬到这两个后端和HTML到远程服务器,并加载页面的方式。这一次,我打了10火狐连接的限制!?!歌剧仍然有6和铬限制(工作,因为没有涉及到这个时候CORS)具有6的限制。

我倒是AP preciate任何洞察到哪里这个数字6从何而来,如果它仅仅是巧合,这三个浏览器是相同的。特别是任何了解为什么火狐有时是6,有时10,有时12(参数上证所规范似乎离开未定义的最大连接数。)


Apache的配置是使用prefork,这意味着这些设置:

  StartServers的5
比MinSpareServers 5
MaxSpareServers的10
MaxClients的150
MaxRequestsPerChild 0

(包括本地(Ubuntu的10.04)和全球(Ubuntu的11.10)服务器在这里有相同的Apache设置。)
我认为关键数字还有就是MaxClients的是150我做了一个快速变化的实验来StartServers的50,而不是5,但得到了相同的结果。


下面是客户端HTML / JavaScript的(1或2行取消注释和修改,如果你想尝试连接到不同的服务器,因为这里给出它希望找到sse.php在同一目录下HTML):

 <!DOCTYPE HTML>
< HTML和GT;
< HEAD>
<间的charset =UTF-8/>
<标题>上证Stresstest< /标题>
< /头>
<身体GT;
< p n =犯错>< / P>
&下,P ID =×>&下; / P>
<脚本>
函数的start(){函数的onMessage(五){
的document.getElementById('X')的innerHTML + = e.origin +:+ this.dummy_n +:+ e.data +&所述峰; br />中;
};功能的onError(五){
的document.getElementById('犯错')的innerHTML + =错误:+ this.dummy_n +:+ JSON.stringify(E)+< BR />中。
};对于(VAR N = 1; N< = 32; ++ N){
    // NB。 T主要是为了避免缓存
    VAR URL ='sse.php dummy_n ='+ N +'& T公司='+(新的Date()的getTime());
    //如果(N%2 == 0)
    // URL ='?HTTP://example.com/sse.php dummy_n ='+ N +'& T公司='+(新的Date()的getTime());
    变种ES =新的EventSource(URL);
    es.dummy_n = N; //所以我们可以找出每一个
    es.addEventListener('错误',onError的,虚假的);
    es.addEventListener('消息',的onMessage,FALSE);
    }
}的setTimeout(开始(),1000); //只有Safari浏览器需要1000毫秒的延迟。
< / SCRIPT>
< /身体GT;
< / HTML>

和后端sse.php脚本是这样的:

 < PHP
$ IP = array_key_exists('SERVER_ADDR',$ _ SERVER)$ _ SERVER ['SERVER_ADDR']:'CLI';?
标题(内容类型:text /事件流');
标题(访问控制允许来源:*'); // CORS:允许从任何地点访问
@ob_flush(); @齐平();
//现在主循环
而(真){
    回声数据:gmdate(Y-M-D H:我:秒)$ IP\\ n \\ n;。
    @ob_flush(); @齐平();
    睡眠(1);
    }
?>


解决方案

原因可能是每一个EventSource的目标发起新的HTTP会话,实际上开辟了新的TCP / IP套接字。因为你从服务器连续无限循环推数据,插座保持开放不断。
所有Web浏览器对的同时的活跃到同一服务器连接的上限。通常在4〜6依照RFC 2616范围内您的浏览器只是preventing新的连接是开放的作为要超出此限制。

您可以在这里学到一些更多的信息,以及:结果
http://www.stevesouders.com/blog/ 2008/03/20 /综合报道上并行的连接/

I wanted to see how many simultaneous SSE (aka EventSource) connections I could setup, before overloading my machine. But testing with Firefox (Firefox 18 or Firefox 20) it stopped at 6 connections: the additional connections give no error, but do not send any data. (In Firebug I can see them there, waiting to connect.) Chromium 25 also stopped at 6 connections, and so did Opera 12.15. But it does not seem to be a server-side limit (I'm using Apache + PHP), as I could run all three browsers at the same time (i.e. 18 connections), and all are coming from the same IP address. (Server and client are on the same machine, but using a 172.16.x.x address, not 127.0.0.1.)

So, I set the test up with CORS, and tried connecting to another server, which has a global IP. This time I get 12 connections for Firefox. Suggesting it is Apache configuration after all? No, Opera still only gets 6 connections. (No number for Chrome, as CORS does not appear to work.) I could also run connecting to both servers, for a total of 18 connections (but never any more) in Firefox, and a total of 12 in Opera.

As a 3rd test I moved both back-end and html to the remote server, and loaded the page that way. This time I hit a limit of 10 connections for Firefox!?! Opera still has a limit of 6. And Chromium (which works as there is no CORS involved this time) has a limit of 6.

I'd appreciate any insight into where this number 6 comes from, and if it is just coincidence that all three browsers are the same. And especially any insight into why Firefox is sometimes 6, sometimes 10, sometimes 12. (The SSE specification seems to leave the maximum number of connections undefined.)


Apache configuration is to use prefork, which means these settings:

StartServers          5
MinSpareServers       5
MaxSpareServers      10
MaxClients          150
MaxRequestsPerChild   0

(Both local (Ubuntu 10.04) and global (Ubuntu 11.10) servers have identical Apache settings here.) I believe the key number there is that MaxClients is 150. I did a quick experiment changing StartServers to 50, instead of 5, but got identical results.


Here is the client-side HTML/javascript (1 or 2 lines to uncomment, and modify, if you want to experiment connecting to a different server; as given here it expects to find sse.php in the same directory as the HTML):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>SSE Stresstest</title>
</head>
<body>
<p id="err"></p>
<p id="x"></p>
<script>
function start(){

function onMessage(e){
document.getElementById('x').innerHTML+=e.origin+":"+this.dummy_n+":"+e.data+"<br/>";
};

function onError(e){
document.getElementById('err').innerHTML+="ERR:"+this.dummy_n+":"+JSON.stringify(e)+"<br/>";
};

for(var n=1;n<=32;++n){
    //NB. 't' primarily to avoid caching
    var url='sse.php?dummy_n='+n+'&t='+(new Date().getTime());
    //if(n%2==0)
    //    url='http://example.com/sse.php?dummy_n='+n+'&t='+(new Date().getTime());
    var es=new EventSource(url);
    es.dummy_n=n;   //So we can identify each one
    es.addEventListener('error',onError,false);
    es.addEventListener('message',onMessage,false);
    }
}

setTimeout("start()",1000);   //Only Safari needs the 1000ms delay.
</script>
</body>
</html>

And the backend sse.php script is like this:

<?php
$ip=array_key_exists('SERVER_ADDR',$_SERVER)?$_SERVER['SERVER_ADDR']:'cli';
header('Content-Type: text/event-stream');
header('Access-Control-Allow-Origin: *');   //CORS: allow access from anywhere
@ob_flush();@flush();
//Now the main loop
while(true){
    echo "data:".gmdate("Y-m-d H:i:s,").$ip."\n\n";
    @ob_flush();@flush();
    sleep(1);
    }
?>

解决方案

The reason could be every EventSource object initiates the new HTTP session and in fact opens new tcp/ip socket. Because of you're pushing data from server continuously in infinite loop, the socket keeps open continuously. All web browsers has an upper limit on simultaneous active connections to the same server. normally in range of 4 to 6 as per RFC 2616. Your browser just preventing new connection to be open as this limit to be exceeded.

You may learn here some more info as well:
http://www.stevesouders.com/blog/2008/03/20/roundup-on-parallel-connections/

这篇关于SSE(EventSource的):为什么不超过6连接的更多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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