Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using thin for concurrent requests

I have a Rails 4.1 application with a simple controller which streams a response:

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

When I add puma to my gemfile and make a request against this route using curl I get a streamed response as expected:

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"}

When I switch to thin in my gemfile and make a request the whole thing locks up. I've read in multiple places that thin can handle concurrent requests but I can't seem to make it work.

I'm starting puma simply by running bundle exec rails server. For thin I've tried bundle exec rails server and multiple configurations like bundle exec thin start -a 127.0.0.1 -threaded. Nothing seems to prevent thin from locking up.

How can I get thin to accept concurrent requests?

like image 736
David Tuite Avatar asked Aug 29 '14 04:08

David Tuite


1 Answers

I had the same issue and I had to start the server like this

  bundle exec thin start -a 127.0.0.1 --threaded -e production
like image 182
dc10 Avatar answered Oct 23 '22 19:10

dc10