长轮询使用Ajax和PHP

长轮询使用Ajax和PHP

本文介绍了长轮询使用Ajax和PHP - 阿帕奇冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们尝试在我们公司的ERP系统来实现长轮询基于通知服务。类似于Facebook的通知。

We try to implement long-polling based notification service in our company's ERP. Similar to Facebook notifications.

用到的技术:

  • 暂停在每次循环设置为60秒到1秒睡眠 PHP。
  • jQuery的AJAX的处理。
  • 在Apache作为Web服务器。
  • PHP with timeout set to 60 seconds and 1 second sleep in each iteration of loop.
  • jQuery for AJAX handling.
  • Apache as web server.

近一个月的编码后,我们去生产。部署几分钟后,我们不得不回滚一切。原来,我们的服务器(8个核心)无法从20名员工处理长的请求,使用〜5每一个浏览器标签。例如:用户打开3个标签与我们的ERP,与每个选项卡上的一个长轮询AJAX。打开第4个选项卡是不可能的 - 它挂起,直到previous 3被杀害(因此AJAX停止)一个

After nearly month of coding, we went to production. Few minutes after deployment we had to rollback everything. It turned out that our server (8 cores) couldn't handle long requests from 20 employees, using ~5 browser tabs each.For example: User opened 3 tabs with our ERP, with one long-polling AJAX on each tab. Opening 4th tab is impossible - it hangs until one of previous 3 is killed (and therefore AJAX is stopped).

阿帕奇限制',我们的思想。所以我们去谷歌上搜索。我发现关于Apache的MPM模块的configs一些信息,所以我给它一个尝试。我们的服务器使用 prefork MPM,如 apachectl中-l 告诉我们。所以,我在配置改变几行看起来是这样的:

'Apache limitations', we thought. So we went googling. I found some info about Apache's MPM modules and configs, so I gave it a try. Our server use prefork MPM, as apachectl -l shown us. So I changed few lines in config to look something like this:

<IfModule mpm_prefork_module>
    StartServers          1
    MinSpareServers       16
    MaxSpareServers      32
    ServerLimit          50%
    MaxClients          150
    MaxClients           50%
    MaxRequestsPerChild   0
</IfModule>

有趣的是,它可以在我的本地机器具有类似的配置。在服务器上,它看起来像阿帕奇忽略的配置,因为与比MinSpareServers 设置为16,则重新启动后lauches 8。 WHE不知道该怎么做。

Funny thing is, it works on my local machine with similar config. On server, it looks like Apache ignores config, because with MinSpareServers set to 16, it lauches 8 after restart. Whe have no idea what to do.

推荐答案

路人在previous后的第一个评论给了我很好的方向来看看,如果我们打一个服务器最大的浏览器的连接。

Passerby in first comment of previous post gave me good direction to check out if we hit max browser connections to one server.

事实证明,每个浏览器都有这些限制,你不能改变(据我所知)。我们做了一个解决办法,使其工作。

As it turns out, each browser has those limit and you can't change them (as far as I know).We made a workaround to make it work.

让我们假设我是从

http://domain.com/ajax

要避免碰到最大的浏览器连接,每个长轮询 AJAX 连接到随机的子域名,如:

To avoid hitting max browser connections, each long-polling AJAX connects to random subdomain, like:

http://31289.domain.com/ajax
http://43289.domain.com/ajax

等。有一个通配符上的DNS服务器指向从 *。domain.com domain.com ,和子是唯一随机数,由JS每个选项卡上生成。

and so on. There's a wildcard on a DNS server pointing from *.domain.com to domain.com, and subdomain is unique random number, generated by JS on each tab.

有关更多信息,请访问this螺纹。

For more information, check out this thread.

还有的是还有些问题与 AJAX同源安全,但我们设法去解决它,使用适当的头两个 JS PHP 两侧。

There's been also some problems with AJAX Same Origin Security, but we managed to work it out, using appropriate headers on both JS and PHP sides.

如果您想了解更多关于头,看看here在计算器和这里Mozilla开发的页面。谢谢!

If you want to know more about headers, check it out here on StackOverflow, and here on Mozilla Developer's page. Thanks!

这篇关于长轮询使用Ajax和PHP - 阿帕奇冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:12