Rails 应用程序的快速 AJAX 响应

发布于 2024-09-08 05:13:11 字数 518 浏览 9 评论 0原文

我需要向用 RoR 编写的基于 Web 的监控系统发送警报。强力解决方案是使用 JavaScript 频繁轮询轻量级控制器。当然,缺点是为了获得对警报的严格响应时间,我必须非常频繁地进行轮询(每 5 秒一次)。

我的一个想法是让源自 AJAX 的轮询线程在服务器端休眠,直到服务器收到警报。然后,服务器将唤醒休眠线程并将响应返回给 Web 客户端,该响应将立即显示。这将使我能够将轮询间隔缩短至每 30 秒或每分钟一次,同时缩短提醒用户所需的时间。

我没有想到的一件事是 mongrel/rails 并没有像我预期的那样为每个网络请求启动一个线程。这意味着其他传入的 Web 请求将被阻塞,直到第一个线程的睡眠超时。

我尝试过调用“config.threadsafe!”在我的配置中,但这似乎并没有改变每个请求线程模型的行为。另外,看来是用 config.threadsafe 运行的!这是一个有风险的提议,可能需要对我现有的应用程序进行大量的测试和返工。

对我采取的方法有什么想法,或者有更好的方法来获得我正在寻找的响应时间,而不需要向服务器发送大量请求?

I have a need to send alerts to a web-based monitoring system written in RoR. The brute force solution is to frequently poll a lightweight controller with javascript. Naturally, the downside is that in order to get a tight response time on the alerts, I'd have to poll very frequently (every 5 seconds).

One idea I had was to have the AJAX-originated polling thread sleep on the server side until an alert arrived on the server. The server would then wake up the sleeping thread and get a response back to the web client that would be shown immediately. This would have allowed me to cut the polling interval down to once every 30 seconds or every minute while improving the time it took to alert the user.

One thing I didn't count on was that mongrel/rails doesn't launch a thread per web request as I had expected it to. That means that other incoming web requests block until the first thread's sleep times out.

I've tried tinkering around with calling "config.threadsafe!" in my configuration, but that doesn't seem to change the behavior to a thread per request model. Plus, it appears that running with config.threadsafe! is a risky proposition that could require a great deal more testing and rework on my existing application.

Any thoughts on the approach I took or better ways to go about getting the response times I'm looking for without the need to deluge the server with requests?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

念﹏祤嫣 2024-09-15 05:13:11

您可以使用 Rails Metal 来提高控制器性能,甚至可以将其完全分离为 < a href="http://www.sinatrarb.com/" rel="nofollow noreferrer">Sinatra 应用程序(Sinatra 可以处理一些严重的请求吞吐量)。

另一个想法是使用 Juggernaut 或类似的解决方案来研究推送解决方案。

You could use Rails Metal to improve the controller performance or maybe even separate it out entirely into a Sinatra application (Sinatra can handle some serious request throughput).

Another idea is to look into a push solution using Juggernaut or similar.

英雄似剑 2024-09-15 05:13:11

您可以考虑的一种方法是让(部分或全部)您的请求在 中创建延迟监控作业外部队列 反过来会定期通知监控应用程序。

One approach you could consider is to have (some or all of) your requests create deferred monitoring jobs in an external queue which would in turn periodically notify the monitoring application.

南风起 2024-09-15 05:13:11

您需要的是 Juggernaut 这是一个 Rails 插件,允许您的应用程序启动连接并将数据推送给客户端。换句话说,您的应用程序可以实时连接到服务器,并具有即时更新的优势。

What you need is Juggernaut which is a Rails plugin that allows your app to initiate a connection and push data to the client. In other words your app can have a real time connection to the server with the advantage of instant updates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文