How to configure Thin web server from Sinatra?

Viewed 1208

This is my web app:

class Front < Sinatra::Base
  configure do
    set :server, :thin
  end
  get '/' do
    'Hello, world!'
  end 
end

I start it like this:

Front.start!

Works great, but I want to configure Thin to be "threaded". I know it's possible, according to their documentation. But I can't figure out how to pass that threaded: true parameter to Thin. I tried this and it doesn't work:

configure do
  set :server_settings, threaded: true
end
1 Answers

The thin web server is threaded by default when started in the way you describe.

# thin_test.rb
require 'sinatra/base'

class Front < Sinatra::Base
  configure do
    set :server, :thin
  end

  get '/' do
    'Hello, world!'
  end 

  get '/foo' do
    sleep 30
    'bar'
  end
end

Front.start!

Start with:

ruby thin_test.rb

Confirm with:

# will hang for 30 seconds while sleeping
curl localhost:4567/foo

# will complete successfully while the other request is pending
curl localhost:4567
Hello, world!

There is additional detail in this answer about how Sinatra uses other web servers.

If this doesn't work for some reason, it may be possible to hack together something with the server_settings option, which is generally only useful for WEBrick unless you use some undocumented ways to force it:

require 'sinatra/base'
require 'thin'

class ThreadedThinBackend < ::Thin::Backends::TcpServer
  def initialize(host, port, options)
    super(host, port)
    @threaded = true
  end
end

class Front < Sinatra::Base
  configure do
    set :server, :thin

    class << settings
      def server_settings
        { :backend => ThreadedThinBackend }
      end
    end
  end

  get '/' do
    'Hello, world!'
  end 

  get '/foo' do
    sleep 30
    'foobar'
  end
end

Front.start!

It's hard for me to tell if this example is the cause of it being threaded though, because it starts in threaded mode by default. That said, it doesn't raise an exception and does run in threaded mode:

# will hang for 30 seconds while sleeping
curl localhost:4567/foo

# will complete successfully while the other request is pending
curl localhost:4567
Hello, world!

More information about server_settings can be found here, here, and here.

Related