WEBrick is multi-threaded but Rails developers hard-coded a mutex, so it can handle just one request at a time. You can monkey-patch Rails::Server
and you are free to run a multi-threaded WEBrick.
Just note that WEBrick will be multithreaded only when config config.cache_classes = true
and config.eager_load = true
, which is typical to RAILS_ENV=production
. This is because class reloading in development is not thread safe.
To get WEBrick fully multi-threaded in Rails 4.0, just add this to config/initializers/multithreaded_webrick.rb
:
# Remove Rack::Lock so WEBrick can be fully multi-threaded.
require 'rails/commands/server'
class Rails::Server
def middleware
middlewares = []
middlewares << [Rails::Rack::Debugger] if options[:debugger]
middlewares << [::Rack::ContentLength]
Hash.new middlewares
end
end
The offending code in rails/commands/server.rb
that we got rid of is:
# FIXME: add Rack::Lock in the case people are using webrick.
# This is to remain backwards compatible for those who are
# running webrick in production. We should consider removing this
# in development.
if server.name == 'Rack::Handler::WEBrick'
middlewares << [::Rack::Lock]
end
It's not needed on Rails 4.2. It's concurrent out-of-the-box.