对并发请求使用瘦

Dav*_*ite 5 ruby multithreading ruby-on-rails thin ruby-on-rails-4

我有一个带有简单控制器的Rails 4.1应用程序,该控制器可以流式传输响应:

class ServerSentEventsController < ApplicationController
  include ActionController::Live

  def index
    response.headers['Content-Type'] = 'text/event-stream'
    sse = ServerSentEvent.new(response.stream)

    begin
      loop do
        sse.write(time: Time.now)
        sleep 1
      end
    rescue IOError
      # When the client disconnects, we'll get an IOError on write
    ensure
      sse.close
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

当我将puma添加到我的gemfile并使用此路由针对此路由发出请求时,curl得到的响应是预期的:

curl -i 'http://localhost:3000/sse'
<!-- truncated headers output -->    
data: {"time":"2014-08-29 05:16:00 +0100"}

data: {"time":"2014-08-29 05:16:01 +0100"}

data: {"time":"2014-08-29 05:16:02 +0100"}
Run Code Online (Sandbox Code Playgroud)

当我切换到在我的Gemfile,并要求整个事情锁定。我在多个地方都读过,thin可以处理并发请求,但是我似乎无法使其正常工作。

我只是通过跑步来开始美洲豹bundle exec rails server。对于瘦身我已经尝试了bundle exec rails server多种配置,如bundle exec thin start -a 127.0.0.1 -threaded。似乎没有什么可以阻止thin锁定。

如何瘦身接受并发请求?

dc1*_*c10 1

我遇到了同样的问题,我必须像这样启动服务器

  bundle exec thin start -a 127.0.0.1 --threaded -e production
Run Code Online (Sandbox Code Playgroud)