简体   繁体   中英

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 . live goes through a loop and write random data to response.stream.write without any issues. It works.

In test however, I am using 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. This is what happens:

I start the app and hit the test endpoint with curl . Curl stops waiting for data as expected. Then in redis-cli I send a message to the abc channel. Immediately I see my message in the rails app window ( puts data ) and I see this in 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 ).

I am running Puma, have looked and implemented at all Stackoverflow answers regarding concurreny, heartbeat threads and more with no luck.

经过多次挖掘后,结果发现你需要一个新的行"\\n" ,这些行会在response.stream.write的任何内容结束时让它离开缓冲区。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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