Skip to content

Commit 784d482

Browse files
authored
Merge pull request rails#52533 from denys-chaikovskyi/mod/auto-puma-workers
Make automatic detection of processor count in default puma config optional and non-default
2 parents 64bb2dc + c68cea4 commit 784d482

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

guides/source/tuning_performance_for_deployment.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,17 @@ If you use more than one thread per process, then it should be set to how many
121121
or if the server is running multiple applications, to how many cores you want the application to use.
122122
If you only use one thread per worker, then you can increase it to above one per process to account for when workers are
123123
idle waiting for I/O operations.
124-
In the default generated configuration, it is set to use all the available processor cores on the server via the
125-
`Concurrent.available_processor_count` helper. You can also modify it by setting the `WEB_CONCURRENCY` environment variable.
124+
125+
In the default generated configuration, it is set to use 1 worker.
126+
You can also modify it by setting the `WEB_CONCURRENCY` environment variable.
127+
128+
* Setting `WEB_CONCURRENCY` to a specific number will configure Puma to use that many workers.
129+
* Setting `WEB_CONCURRENCY` to "auto" will make Puma use all available processor cores on the server, as determined by
130+
the `Concurrent.available_processor_count` helper.
131+
132+
Please note that using "auto" might result in incorrect configurations on some cloud hosts with shared CPUs or platforms
133+
that inaccurately report CPU counts.
134+
126135

127136
### YJIT
128137

railties/lib/rails/generators/rails/app/templates/config/deploy.yml.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ env:
3030
- RAILS_MASTER_KEY
3131
clear:
3232
# Set this to the number of cores you wish the application to use on each server.
33-
# If not set, app will attempt to use all available cores (but may guess wrong on some cloud hosts!)
33+
# You can use "auto" and the app will attempt to use all available cores (but may guess wrong on some cloud hosts!)
3434
WEB_CONCURRENCY: 1
3535

3636
# Match this to the database server to configure Active Record correctly

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ when "production"
3232
# If you are running more than 1 thread per process, the workers count
3333
# should be equal to the number of processors (CPU cores) in production.
3434
#
35-
# Automatically detect the number of available processors in production.
35+
# Automatically detect the number of available processors in production
36+
# when WEB_CONCURRENCY is set to "auto".
3637
require "concurrent-ruby"
37-
workers_count = Integer(ENV.fetch("WEB_CONCURRENCY") { Concurrent.available_processor_count })
38+
workers_count = if ENV["WEB_CONCURRENCY"] == "auto"
39+
Concurrent.available_processor_count
40+
else
41+
Integer(ENV.fetch("WEB_CONCURRENCY", 1))
42+
end
3843
workers workers_count if workers_count > 1
3944

4045
preload_app!

0 commit comments

Comments
 (0)