Skip to content

Commit eac48e9

Browse files
authored
Merge pull request rails#50669 from byroot/update-puma-defaults
Update the default Puma configuration
2 parents 61b48fe + 06d614a commit eac48e9

File tree

1 file changed

+45
-26
lines changed
  • railties/lib/rails/generators/rails/app/templates/config

1 file changed

+45
-26
lines changed

railties/lib/rails/generators/rails/app/templates/config/puma.rb.tt

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,57 @@
22
# are invoked here are part of Puma's configuration DSL. For more information
33
# about methods provided by the DSL, see https://puma.io/puma/Puma/DSL.html.
44

5-
# Puma can serve each request in a thread from an internal thread pool.
6-
# The `threads` method setting takes two numbers: a minimum and maximum.
7-
# Any libraries that use thread pools should be configured to match
8-
# the maximum value specified for Puma. Default is set to 5 threads for minimum
9-
# and maximum; this matches the default thread size of Active Record.
10-
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
11-
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
12-
threads min_threads_count, max_threads_count
13-
14-
# Specifies that the worker count should equal the number of processors in production.
15-
if ENV["RAILS_ENV"] == "production"
16-
require "concurrent-ruby"
17-
worker_count = Integer(ENV.fetch("WEB_CONCURRENCY") { Concurrent.physical_processor_count })
18-
workers worker_count if worker_count > 1
19-
end
5+
rails_env = ENV.fetch("RAILS_ENV", "development")
6+
7+
# Puma starts a configurable number of processes (workers) and each process
8+
# serves each request in a thread from an internal thread pool.
9+
#
10+
# The ideal number of threads per worker depends both on how much time the
11+
# application spends waiting for IO operations and on how much you wish to
12+
# to prioritize throughput over latency.
13+
#
14+
# As a rule of thumb, increasing the number of threads will increase how much
15+
# traffic a given process can handle (throughput), but due to CRuby's
16+
# Global VM Lock (GVL) it has diminishing returns and will degrade the
17+
# response time (latency) of the application.
18+
#
19+
# The default is set to 3 threads as it's deemed a decent compromise between
20+
# throughput and latency for the average Rails application.
21+
#
22+
# Any libraries that use a connection pool or another resource pool should
23+
# be configured to provide at least as many connections as the number of
24+
# threads. This includes Active Record's `pool` parameter in `database.yml`.
25+
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 3 }
26+
threads threads_count, threads_count
2027

21-
# Specifies the `worker_timeout` threshold that Puma will use to wait before
22-
# terminating a worker in development environments.
23-
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"
28+
if rails_env == "production"
29+
# If you are running more than 1 thread per process, the workers count
30+
# should be equal to the number of processors (CPU cores) in production.
31+
#
32+
# It defaults to 1 because it's impossible to reliably detect how many
33+
# CPU cores are available. Make sure to set the `WEB_CONCURRENCY` environment
34+
# variable to match the number of processors.
35+
processors_count = Integer(ENV.fetch("WEB_CONCURRENCY") { 1 })
36+
if processors_count > 1
37+
workers worker_count
38+
else
39+
preload_app!
40+
end
41+
end
2442

2543
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
2644
port ENV.fetch("PORT") { 3000 }
2745

2846
# Specifies the `environment` that Puma will run in.
29-
environment ENV.fetch("RAILS_ENV") { "development" }
30-
31-
# Specifies the `pidfile` that Puma will use.
32-
if ENV["PIDFILE"]
33-
pidfile ENV["PIDFILE"]
34-
else
35-
pidfile "tmp/pids/server.pid" if ENV.fetch("RAILS_ENV", "development") == "development"
36-
end
47+
environment rails_env
3748

3849
# Allow puma to be restarted by `bin/rails restart` command.
3950
plugin :tmp_restart
51+
52+
pidfile ENV["PIDFILE"] if ENV["PIDFILE"]
53+
54+
if rails_env == "development"
55+
# Specifies a very generous `worker_timeout` so that the worker
56+
# isn't killed by Puma when suspended by a debugger.
57+
worker_timeout 3600
58+
end

0 commit comments

Comments
 (0)