puma: update config.

Update the config for the Puma webserver (used by `bin/rails server`).

* Update default settings.
* Prefix all Puma environment variables with `PUMA_`.
* Enable the Puma control app (`bin/pumactl`).
This commit is contained in:
evazion
2021-09-11 10:01:25 -05:00
parent 8772bc78ec
commit ee1c3c9418

View File

@@ -1,50 +1,99 @@
# This file contains configuration settings for the Puma web server. These # This file contains configuration settings for the Puma web server. These
# settings apply when running Danbooru with `bin/rails server`. In production, # settings apply when running Danbooru with `bin/rails server`.
# Danbooru currently uses Unicorn instead of Puma. #
# The following environment variables are used:
#
# * RAILS_ENV
# * PUMA_PORT
# * PUMA_BIND
# * PUMA_WORKERS
# * PUMA_MIN_THREADS
# * PUMA_MAX_THREADS
# * PUMA_WORKER_TIMEOUT
# * PUMA_PIDFILE
# * PUMA_CONTROL_URL
#
# Use `bin/pumactl` to control a running Puma instance.
# #
# @see https://puma.io # @see https://puma.io
# @see https://github.com/puma/puma # @see https://github.com/puma/puma
# @see https://github.com/puma/puma/blob/319f84db13ee59f7b24885cec686d5c714998a4c/lib/puma/configuration.rb#L188 (default options)
# @see https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server
# Puma can serve each request in a thread from an internal thread pool. # The server port or listening address. Default is http://0.0.0.0:3000.
# The `threads` method setting takes two numbers: a minimum and maximum. if ENV.has_key?("PUMA_PORT")
# Any libraries that use thread pools should be configured to match port ENV["PUMA_PORT"]
# the maximum value specified for Puma. Default is set to 5 threads for minimum elsif ENV.has_key?("PUMA_BIND")
# and maximum; this matches the default thread size of Active Record. bind ENV["PUMA_BIND"]
# else
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } # low_latency=true means TCP_NODELAY
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count } # backlog=1024 means socket listen backlog
# https://github.com/puma/puma/blob/319f84db13ee59f7b24885cec686d5c714998a4c/lib/puma/dsl.rb#L193
bind "tcp://0.0.0.0:3000?low_latency=true&backlog=1024"
end
# The number of `workers` to boot in clustered mode. Workers are forked web
# server processes. If using threads and workers together, the concurrency of
# the application would be max `threads` * `workers`. Workers do not work on
# JRuby or Windows (both of which do not support processes). The Postgres
# connection limit may need to be raised for high `thread` * `worker` counts.
if ENV.has_key?("PUMA_WORKERS")
workers ENV["PUMA_WORKERS"]
elsif ENV["RAILS_ENV"] == "production"
workers ENV.fetch("PUMA_WORKERS", Etc.nprocessors)
else
# Use single worker mode in development for easier debugging
workers 0
end
# The number of threads per worker to use. The `threads` method
# setting takes two numbers: a minimum and maximum. Any libraries that use
# thread pools should be configured to match the maximum value specified for
# Puma.
max_threads_count = ENV.fetch("PUMA_MAX_THREADS", 5)
min_threads_count = ENV.fetch("PUMA_MIN_THREADS", 1)
threads min_threads_count, max_threads_count threads min_threads_count, max_threads_count
# Specifies the `worker_timeout` threshold that Puma will use to wait before # Verifies that all workers have checked in to the master process within
# terminating a worker in development environments. # the given timeout. If not the worker process will be restarted. This is
# # not a request timeout, it is to protect against a hung or dead process.
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development" # Setting this value will not protect against slow requests.
worker_timeout ENV.fetch("PUMA_WORKER_TIMEOUT", 60)
# Specifies the `port` that Puma will listen on to receive requests; default is 3000. # The number of seconds to wait for another request within a persistent (keep
# # alive) session.
port ENV.fetch("PORT") { 3000 } persistent_timeout 20
# Specifies the `environment` that Puma will run in. # The number of seconds to wait until we get the first data for the request
# first_data_timeout 30
environment ENV.fetch("RAILS_ENV") { "development" }
# Specifies the `pidfile` that Puma will use. # The `environment` that Puma will run in.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } environment ENV.fetch("RAILS_ENV", "development")
# Specifies the number of `workers` to boot in clustered mode. # The `pidfile` that Puma will use.
# Workers are forked web server processes. If using threads and workers together pidfile ENV.fetch("PUMA_PIDFILE", "tmp/pids/server.pid")
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }
# Use the `preload_app!` method when specifying a `workers` number. # Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code # This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write # before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory. # process behavior so workers use less memory.
# if ENV["RAILS_ENV"] == "production"
# preload_app! preload_app!
end
# Allow puma to be restarted by `rails restart` command. # Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart plugin :tmp_restart
# Start the Puma control rack application on +url+. This application can
# be communicated with to control the main server. Additionally, you can
# provide an authentication token, so all requests to the control server
# will need to include that token as a query parameter. This allows for
# simple authentication.
#
# Usage:
#
# * bin/pumactl stats
# * bin/pumactl -C tcp://localhost:9293 stats
#
# https://github.com/puma/puma#controlstatus-server
activate_control_app ENV.fetch("PUMA_CONTROL_URL", "tcp://localhost:9293"), no_token: true