[英]Redis Subscription + Rails ActionController::Live hangs
In a single controller of a simple Rails 5 app, I have 2 actions ( live
and test
), both using ActionController::Live
. 在一个简单的Rails 5应用程序的单个控制器中,我有2个动作( live
和test
),都使用ActionController::Live
。 live
goes through a loop and write random data to response.stream.write
without any issues. live
经历一个循环并将随机数据写入response.stream.write
而没有任何问题。 It works. 有用。
In test
however, I am using Redis.subscribe. 然而,在test
中,我正在使用Redis.subscribe。 Here is the action code: 这是动作代码:
def test
response.headers['Content-Type'] = 'text/event-stream'
redis = Redis.new
redis.subscribe_with_timeout(30, "abc") do |on|
on.message do |event, data|
puts data
response.stream.write data
end
end
rescue IOError
# ignore
ensure
redis.quit
response.stream.close
end
The test
method however doesn't work as expected. 然而, test
方法不能按预期工作。 This is what happens: 这是发生的事情:
I start the app and hit the test
endpoint with curl
. 我启动应用程序并使用curl
命中test
端点。 Curl stops waiting for data as expected. Curl停止按预期等待数据。 Then in redis-cli
I send a message to the abc
channel. 然后在redis-cli
我向abc
频道发送一条消息。 Immediately I see my message in the rails app window ( puts data
) and I see this in curl: 我立即在rails应用程序窗口中看到了我的消息( puts data
),我在curl中看到了这个消息:
HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
X-Request-Id: 25df052d-e3f2-4327-9a97-088e702460fa
X-Runtime: 2.709001
Transfer-Encoding: chunked
But while the all the consequent messages from redis-cli
are printed on the rails side, they are not received by curl until the timeout is over (30 seconds in subscribe_with_timeout
). 但是当来自redis-cli
的所有后续消息都打印在rails侧时,直到超时结束( subscribe_with_timeout
为30秒)才会被curl接收。
I am running Puma, have looked and implemented at all Stackoverflow answers regarding concurreny, heartbeat threads and more with no luck. 我正在运行Puma,已经查看并实现了所有Stackoverflow关于concurreny,heartbeat线程以及更多没有运气的答案。
经过多次挖掘后,结果发现你需要一个新的行"\\n"
,这些行会在response.stream.write
的任何内容结束时让它离开缓冲区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.