[英]Redis Subscription + Rails ActionController::Live hangs
在一個簡單的Rails 5應用程序的單個控制器中,我有2個動作( live
和test
),都使用ActionController::Live
。 live
經歷一個循環並將隨機數據寫入response.stream.write
而沒有任何問題。 有用。
然而,在test
中,我正在使用Redis.subscribe。 這是動作代碼:
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
然而, test
方法不能按預期工作。 這是發生的事情:
我啟動應用程序並使用curl
命中test
端點。 Curl停止按預期等待數據。 然后在redis-cli
我向abc
頻道發送一條消息。 我立即在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
但是當來自redis-cli
的所有后續消息都打印在rails側時,直到超時結束( subscribe_with_timeout
為30秒)才會被curl接收。
我正在運行Puma,已經查看並實現了所有Stackoverflow關於concurreny,heartbeat線程以及更多沒有運氣的答案。
經過多次挖掘后,結果發現你需要一個新的行"\\n"
,這些行會在response.stream.write
的任何內容結束時讓它離開緩沖區。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.