Rails 应用程序的快速 AJAX 响应
我需要向用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 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.
您可以考虑的一种方法是让(部分或全部)您的请求在 中创建延迟监控作业外部队列 反过来会定期通知监控应用程序。
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.
您需要的是 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.