Skip to content

Commit a69e203

Browse files
committed
Switch to the Puma web server
WEBrick is not suited to production environments. It is single threaded and single process which means that if two requests come in at the same time, the second must wait for the first to finish. Puma, on the other hand, is multi-threaded and can spawn multiple workers
1 parent d3ec3f6 commit a69e203

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
lines changed

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
source 'https://rubygems.org'
44
ruby '2.3.4'
55

6+
gem 'puma'
67
gem 'rack-rewrite'
78
gem 'rails', '~> 5.0.5'
89

@@ -81,5 +82,6 @@ group :test do
8182
end
8283

8384
group :production do
85+
gem 'rack-timeout'
8486
gem 'rails_12factor', '0.0.3'
8587
end

Gemfile.lock

+5-1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ GEM
263263
pry-rails (0.3.6)
264264
pry (>= 0.10.4)
265265
public_suffix (2.0.5)
266+
puma (3.10.0)
266267
pusher (1.1.0)
267268
httpclient (~> 2.7)
268269
multi_json (~> 1.0)
@@ -274,6 +275,7 @@ GEM
274275
rack-rewrite (1.5.1)
275276
rack-test (0.6.3)
276277
rack (>= 1.0)
278+
rack-timeout (0.4.2)
277279
railroady (1.4.2)
278280
rails (5.0.5)
279281
actioncable (= 5.0.5)
@@ -494,8 +496,10 @@ DEPENDENCIES
494496
omniauth-google-oauth2 (~> 0.5.2)
495497
pg (= 0.18.4)
496498
pry-rails
499+
puma
497500
pusher (= 1.1.0)
498501
rack-rewrite
502+
rack-timeout
499503
railroady (= 1.4.2)
500504
rails (~> 5.0.5)
501505
rails-controller-testing
@@ -523,4 +527,4 @@ RUBY VERSION
523527
ruby 2.3.4p112
524528

525529
BUNDLED WITH
526-
1.15.3
530+
1.15.4

Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# Procfile
2-
web: bundle exec rails s -b 0.0.0.0 -p $PORT
2+
web: bundle exec puma -C config/puma.rb

config/initializers/timeout.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
# There is no request timeout mechanism inside of Puma. The Heroku router will
4+
# timeout all requests that exceed 30 seconds. Although an error will be
5+
# returned back to the client, Puma will continue to work on the request as
6+
# there is no way for the router to notify Puma that the request terminated
7+
# early. To avoid clogging processing ability, Rack::Timeout terminates long
8+
# running requests.
9+
if defined?(Rack::Timeout)
10+
Rack::Timeout.timeout = 20 # seconds
11+
end

config/puma.rb

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
# Specifies the number of `workers` to boot in clustered mode.
4+
# Workers are forked webserver processes. If using threads and workers together
5+
# the concurrency of the application would be max `threads` * `workers`.
6+
# Workers do not work on JRuby or Windows (both of which do not support
7+
# processes).
8+
#
9+
workers ENV.fetch('WEB_CONCURRENCY') { 2 }
10+
111
# Puma can serve each request in a thread from an internal thread pool.
212
# The `threads` method setting takes two numbers a minimum and maximum.
313
# Any libraries that use thread pools should be configured to match
414
# the maximum value specified for Puma. Default is set to 5 threads for minimum
515
# and maximum, this matches the default thread size of Active Record.
616
#
7-
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
17+
threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }
818
threads threads_count, threads_count
919

1020
# Specifies the `port` that Puma will listen on to receive requests, default is 3000.
1121
#
12-
port ENV.fetch("PORT") { 3000 }
22+
port ENV.fetch('PORT') { 3000 }
1323

1424
# Specifies the `environment` that Puma will run in.
1525
#
16-
environment ENV.fetch("RAILS_ENV") { "development" }
17-
18-
# Specifies the number of `workers` to boot in clustered mode.
19-
# Workers are forked webserver processes. If using threads and workers together
20-
# the concurrency of the application would be max `threads` * `workers`.
21-
# Workers do not work on JRuby or Windows (both of which do not support
22-
# processes).
23-
#
24-
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }
26+
environment ENV.fetch('RACK_ENV') { 'development' }
2527

2628
# Use the `preload_app!` method when specifying a `workers` number.
2729
# This directive tells Puma to first boot the application and load code
@@ -30,7 +32,7 @@
3032
# you need to make sure to reconnect any threads in the `on_worker_boot`
3133
# block.
3234
#
33-
# preload_app!
35+
preload_app!
3436

3537
# The code in the `on_worker_boot` will be called if you are using
3638
# clustered mode by specifying a number of `workers`. After each worker
@@ -39,9 +41,11 @@
3941
# or connections that may have been created at application boot, Ruby
4042
# cannot share connections between processes.
4143
#
42-
# on_worker_boot do
43-
# ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
44-
# end
44+
on_worker_boot do
45+
# Worker specific setup for Rails 4.1+
46+
# See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
47+
ActiveRecord::Base.establish_connection
48+
end
4549

4650
# Allow puma to be restarted by `rails restart` command.
47-
plugin :tmp_restart
51+
plugin :tmp_restart

0 commit comments

Comments
 (0)