Rubinius + Puma doesn't process requests concurrently

541 views Asked by At

I have simple Sinatra based application which freezes each request for 30 seconds:

get '/test' do
   puts "#{@@counter}, #{Thread.current.object_id}"
   1.upto(30) {|i| sleep 1;puts "#{Thread.current.object_id}, #{i}"}
   [200, "#{Thread.current.object_id}, #{@@counter += 1}"]
end 

I set up puma (2.5.1) web server on rubinius 2.0.0-rc1 with following puma config:

pidfile "#{app_path}/pid_files/puma.pid"
state_path "#{app_path}/pid_files/puma.state"
environment 'production'
threads 3, 3
bind "tcp://x.x.x.x:9292"
daemonize true

I was expecting that this config will be able to process 3 requests concurrently while 4th and subsequent request will be waiting for unused thread. But it appeared that requests are run consequentially. Processing of the second request is started only after first request is finished. Why?

2

There are 2 answers

1
matt On BEST ANSWER

How are you testing this? In my testing firing up multiple browser windows all pointed at the same url results in the requests being handled in sequence, but this seems to be because the browser is only sending one request at a time (I’m using Chrome, I didn’t check any others).

Having multiple browser windows opening different urls on the server results in the requests being handled concurrently as expected (you can use the Sinatra splat param to test this easily, with a route like get '/*/ do ...).

Using curl to send multiple requests at once, even all to the same url, also works as expected.

1
digitalextremist On

Very likely, Sinatra needs to be run with ruby app.rb rather than rackup or a server-specific command to start the application, per Sinatra's automatic configuration approach. Or you need to get deeper into Sinatra configuration, not just Puma

See the very last lines of Konstantin's answer: Is Sinatra multi threaded?

... Otherwise this is a problem with Puma which I've seen the likes of, which caused me to move to Reel and then away from Rack and Sinatra entirely.