简体   繁体   English

为什么我的Rack应用程序挂起?

[英]Why is my Rack app hanging?

I've written an open source Rack app and core lirary to stream stock quotes from Tradeking.com. 我编写了一个开源的Rack应用程序和核心程序库,以传输Tradeking.com的股票报价。 Here's the rack app: https://github.com/chaddjohnson/trading_websocket_service . 这是机架应用程序: https : //github.com/chaddjohnson/trading_websocket_service And here's the core library: https://github.com/chaddjohnson/trading_core . 这是核心库: https : //github.com/chaddjohnson/trading_core

I modified trading_core/lib/trading_core/quote_streamer/tradeking.rb to output streamed data to the console, as so: 我修改了trading_core / lib / trading_core / quote_streamer / tradeking.rb以将流数据输出到控制台,如下所示:

@http.stream do |data|
  puts data

After a few hours of it streaming and outputting to the console, it simply freezes. 流式传输几个小时并输出到控制台后,它只是冻结了。 I have absolutely no idea why. 我完全不知道为什么。 I've done my best to ensure that any loops are not infinite. 我已尽力确保任何循环都不是无限的。 I've been debugging this for over a week now. 我已经调试了一个多星期了。

Any ideas why this is happening? 任何想法为什么会这样?

Here are the main files where I think this could be happening: 以下是我认为可能会发生的主要文件:

On the first look at your code I could imagine two reasons for your issues: 第一次查看您的代码时,我可以想象出出现问题的两个原因:

  1. You are hitting rate limits with Tradeking (maybe using more than 256 symbols or multiple instances of your script (they don't like that) or updating your symbols too fast (changing them) 您正在通过Tradeking达到速率限制(可能使用了256个以上的符号或脚本的多个实例(他们不喜欢这样)或更新符号的速度太快(更改它们)
  2. As you are parsing every incoming message you might "choke" your stream. 解析每个传入消息时,您可能会“阻塞”流。 I see that a lot with people using Twitter streams, parsing the results and writing them to a DB. 我看到很多人使用Twitter流,解析结果并将其写入数据库。 It's just too slow and then starts blocking. 它太慢了,然后开始阻塞。

Glad I could push you in the right direction! 很高兴我可以将您推向正确的方向!

My first hunches were about open connections: 我最初的预感是关于开放连接的:

You might need to make sure you close the connection: @http.close in an ensure block at the end of the method you referenced 您可能需要确保关闭连接:@ http.close在您引用的方法末尾的suresure块中

Then on to some forensics: 然后进行一些取证:

Sounds like you'll have to dig into the ruby process you're running to figure out what's choking it up. 听起来,您必须深入研究正在运行的红宝石流程,以找出造成阻塞的原因。 Depending on what OS you're on of course, on mac and linux you can use tools like gdb, strace, dtrace, etc. For example: ruby.dzone.com/articles/debugging-stuck-ruby-processes 当然,取决于您所使用的操作系统,在Mac和Linux上,您可以使用gdb,strace,dtrace等工具。例如:ruby.dzone.com/articles/debugging-stuck-ruby-processes

We found that select was being called a lot which led us to keep our suspicion on the connections. 我们发现select被称为很多东西,这使我们对连接保持怀疑。 Turns out the problem was in reusing a dead connection via @api ||= ...init code... 原来问题出在通过@api ||= ...init code...重用@api ||= ...init code...连接@api ||= ...init code...

Glad you were able to find what it was! 很高兴您能够找到它是什么! And thanks for posting your solution! 感谢您发布解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM