2018-09-17 12:41:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-20 00:06:25 -04:00
|
|
|
require 'puma/const'
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
module Puma
|
2019-08-05 20:30:43 -04:00
|
|
|
# The methods that are available for use inside the configuration file.
|
2017-03-06 13:32:12 -05:00
|
|
|
# These same methods are used in Puma cli and the rack handler
|
|
|
|
# internally.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2017-03-06 13:32:12 -05:00
|
|
|
# Used manually (via CLI class):
|
|
|
|
#
|
|
|
|
# config = Configuration.new({}) do |user_config|
|
|
|
|
# user_config.port 3001
|
|
|
|
# end
|
|
|
|
# config.load
|
|
|
|
#
|
2020-09-17 11:15:19 -04:00
|
|
|
# puts config.options[:binds] # => "tcp://127.0.0.1:3001"
|
2017-03-06 13:32:12 -05:00
|
|
|
#
|
|
|
|
# Used to load file:
|
2020-09-17 11:40:45 -04:00
|
|
|
#
|
2017-03-06 13:32:12 -05:00
|
|
|
# $ cat puma_config.rb
|
2020-09-17 11:15:19 -04:00
|
|
|
# port 3002
|
|
|
|
#
|
|
|
|
# Resulting configuration:
|
2017-03-06 13:32:12 -05:00
|
|
|
#
|
|
|
|
# config = Configuration.new(config_file: "puma_config.rb")
|
|
|
|
# config.load
|
|
|
|
#
|
2020-09-17 11:15:19 -04:00
|
|
|
# puts config.options[:binds] # => "tcp://127.0.0.1:3002"
|
2017-03-06 13:32:12 -05:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# You can also find many examples being used by the test suite in
|
|
|
|
# +test/config+.
|
2020-09-17 11:15:19 -04:00
|
|
|
#
|
2015-03-13 20:10:39 -04:00
|
|
|
class DSL
|
2015-04-11 16:12:27 -04:00
|
|
|
include ConfigDefault
|
|
|
|
|
2020-11-27 10:40:26 -05:00
|
|
|
# convenience method so logic can be used in CI
|
|
|
|
# @see ssl_bind
|
|
|
|
#
|
|
|
|
def self.ssl_bind_str(host, port, opts)
|
|
|
|
verify = opts.fetch(:verify_mode, 'none').to_s
|
|
|
|
|
|
|
|
tls_str =
|
|
|
|
if opts[:no_tlsv1_1] then '&no_tlsv1_1=true'
|
|
|
|
elsif opts[:no_tlsv1] then '&no_tlsv1=true'
|
|
|
|
else ''
|
|
|
|
end
|
|
|
|
|
|
|
|
ca_additions = "&ca=#{opts[:ca]}" if ['peer', 'force_peer'].include?(verify)
|
|
|
|
|
|
|
|
if defined?(JRUBY_VERSION)
|
|
|
|
ssl_cipher_list = opts[:ssl_cipher_list] ?
|
|
|
|
"&ssl_cipher_list=#{opts[:ssl_cipher_list]}" : nil
|
|
|
|
keystore_additions = "keystore=#{opts[:keystore]}&keystore-pass=#{opts[:keystore_pass]}"
|
|
|
|
"ssl://#{host}:#{port}?#{keystore_additions}#{ssl_cipher_list}" \
|
|
|
|
"&verify_mode=#{verify}#{tls_str}#{ca_additions}"
|
|
|
|
else
|
|
|
|
ssl_cipher_filter = opts[:ssl_cipher_filter] ?
|
|
|
|
"&ssl_cipher_filter=#{opts[:ssl_cipher_filter]}" : nil
|
|
|
|
"ssl://#{host}:#{port}?cert=#{opts[:cert]}&key=#{opts[:key]}" \
|
|
|
|
"#{ssl_cipher_filter}&verify_mode=#{verify}#{tls_str}#{ca_additions}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-07 02:10:48 -05:00
|
|
|
def initialize(options, config)
|
2017-03-03 16:11:59 -05:00
|
|
|
@config = config
|
2015-03-13 20:10:39 -04:00
|
|
|
@options = options
|
2016-02-07 17:51:54 -05:00
|
|
|
|
|
|
|
@plugins = []
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def _load_from(path)
|
2016-03-05 19:07:12 -05:00
|
|
|
if path
|
|
|
|
@path = path
|
|
|
|
instance_eval(File.read(path), path, 1)
|
|
|
|
end
|
2016-02-07 17:51:54 -05:00
|
|
|
ensure
|
|
|
|
_offer_plugins
|
|
|
|
end
|
|
|
|
|
|
|
|
def _offer_plugins
|
|
|
|
@plugins.each do |o|
|
|
|
|
if o.respond_to? :config
|
|
|
|
@options.shift
|
|
|
|
o.config self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@plugins.clear
|
|
|
|
end
|
|
|
|
|
Rack handler should use provided default host
This issue is somewhat tricky. When Rails is booted via `rails server` there are two types of configuration options passed, ones specified directly by a user like `rails s -p 3001` will always "win".
For any other config that is not explicitly passed in, puma will consider it a "default". For example when you run `rails s` (without -p) then the default port will be 3000.
There is one other way to configure puma though, and that is via a config file:
```
# config/puma.rb
port 3002
```
This is the order of precedence for configuration
1) Anything the user explicitly passes to `rails s`
2) Config specified in `config/puma.rb` file
3) Default values passed in via `rails s`
4) Defaults values stored in puma
This fallback mechanism works well except in the case of calling `port` in a `config/puma.rb` file. To understand look at the [old method definition](https://github.com/puma/puma/blob/2668597ec1dd9546d83db9f2ec5ad092add483e6/lib/puma/dsl.rb#L140-L145):
```
def port(port, host=nil)
host ||= Configuration::DefaultTCPHost
bind "tcp://#{host}:#{port}"
end
```
When the `port` method gets called, even if the user did not specify a `host` the `Configuration::DefaultTCPHost` will be used, which is a problem for local development because it defaults to `0.0.0.0`. [SO about 0.0.0.0 versus localhost](https://stackoverflow.com/questions/20778771/what-is-the-difference-between-0-0-0-0-127-0-0-1-and-localhost).
In this case, while a user did directly specify a port, they did not specify a host, so you would expect the `rails s` defaults passed in to take affect.
To make Puma respect that the host coming from `rails s` has more precedence than it's own default host, we must introduce the ability to set and retrieve a default_host value.
This is then used in the rack handler so when `rails s` passes in `:Host => "localhost"` then it is used instead of reverting to `0.0.0.0`.
The issue with #1699 is the test was wrong, it would have failed if a config file was present with a `port` invocation.
2019-01-04 14:15:31 -05:00
|
|
|
def set_default_host(host)
|
|
|
|
@options[:default_host] = host
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_host
|
|
|
|
@options[:default_host] || Configuration::DefaultTCPHost
|
|
|
|
end
|
|
|
|
|
2016-02-07 17:51:54 -05:00
|
|
|
def inject(&blk)
|
|
|
|
instance_eval(&blk)
|
|
|
|
end
|
|
|
|
|
2016-02-19 17:11:42 -05:00
|
|
|
def get(key,default=nil)
|
|
|
|
@options[key.to_sym] || default
|
|
|
|
end
|
|
|
|
|
2016-02-07 17:51:54 -05:00
|
|
|
# Load the named plugin for use by this configuration
|
|
|
|
#
|
|
|
|
def plugin(name)
|
|
|
|
@plugins << @config.load_plugin(name)
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Use an object or block as the rack application. This allows the
|
|
|
|
# configuration file to be the application itself.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @example
|
|
|
|
# app do |env|
|
|
|
|
# body = 'Hello, World!'
|
|
|
|
#
|
|
|
|
# [
|
|
|
|
# 200,
|
|
|
|
# {
|
|
|
|
# 'Content-Type' => 'text/plain',
|
|
|
|
# 'Content-Length' => body.length.to_s
|
|
|
|
# },
|
|
|
|
# [body]
|
|
|
|
# ]
|
|
|
|
# end
|
2020-09-17 11:15:19 -04:00
|
|
|
#
|
|
|
|
# @see Puma::Configuration#app
|
|
|
|
#
|
2015-03-13 20:10:39 -04:00
|
|
|
def app(obj=nil, &block)
|
|
|
|
obj ||= block
|
|
|
|
|
|
|
|
raise "Provide either a #call'able or a block" unless obj
|
|
|
|
|
|
|
|
@options[:app] = obj
|
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# Check out {Puma::App::Status} to see what the app has available.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @example
|
|
|
|
# activate_control_app 'unix:///var/run/pumactl.sock'
|
|
|
|
# @example
|
|
|
|
# activate_control_app 'unix:///var/run/pumactl.sock', { auth_token: '12345' }
|
|
|
|
# @example
|
|
|
|
# activate_control_app 'unix:///var/run/pumactl.sock', { no_token: true }
|
2016-02-06 22:00:29 -05:00
|
|
|
def activate_control_app(url="auto", opts={})
|
|
|
|
if url == "auto"
|
|
|
|
path = Configuration.temp_path
|
|
|
|
@options[:control_url] = "unix://#{path}"
|
|
|
|
@options[:control_url_temp] = path
|
|
|
|
else
|
|
|
|
@options[:control_url] = url
|
|
|
|
end
|
2015-03-13 20:10:39 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
if opts[:no_token]
|
2019-05-28 09:37:34 -04:00
|
|
|
# We need to use 'none' rather than :none because this value will be
|
|
|
|
# passed on to an instance of OptionParser, which doesn't support
|
|
|
|
# symbols as option values.
|
|
|
|
#
|
|
|
|
# See: https://github.com/puma/puma/issues/1193#issuecomment-305995488
|
|
|
|
auth_token = 'none'
|
2016-02-06 22:00:29 -05:00
|
|
|
else
|
2015-03-13 20:18:15 -04:00
|
|
|
auth_token = opts[:auth_token]
|
2016-02-06 22:00:29 -05:00
|
|
|
auth_token ||= Configuration.random_token
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
2016-02-06 22:00:29 -05:00
|
|
|
|
|
|
|
@options[:control_auth_token] = auth_token
|
|
|
|
@options[:control_url_umask] = opts[:umask] if opts[:umask]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Load additional configuration from a file
|
2017-03-06 13:28:44 -05:00
|
|
|
# Files get loaded later via Configuration#load
|
2016-02-06 22:00:29 -05:00
|
|
|
def load(file)
|
2017-03-03 15:30:28 -05:00
|
|
|
@options[:config_files] ||= []
|
|
|
|
@options[:config_files] << file
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Bind the server to +url+. "tcp://", "unix://" and "ssl://" are the only
|
|
|
|
# accepted protocols. Multiple urls can be bound to, calling `bind` does
|
|
|
|
# not overwrite previous bindings.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# The default is "tcp://0.0.0.0:9292".
|
2017-11-28 12:11:59 -05:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# You can use query parameters within the url to specify options:
|
2017-11-28 12:11:59 -05:00
|
|
|
#
|
2020-09-17 11:15:19 -04:00
|
|
|
# * Set the socket backlog depth with +backlog+, default is 1024.
|
|
|
|
# * Set up an SSL certificate with +key+ & +cert+.
|
|
|
|
# * Set whether to optimize for low latency instead of throughput with
|
|
|
|
# +low_latency+, default is to optimize for low latency. This is done
|
|
|
|
# via +Socket::TCP_NODELAY+.
|
|
|
|
# * Set socket permissions with +umask+.
|
2017-11-28 12:11:59 -05:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @example Backlog depth
|
|
|
|
# bind 'unix:///var/run/puma.sock?backlog=512'
|
|
|
|
# @example SSL cert
|
|
|
|
# bind 'ssl://127.0.0.1:9292?key=key.key&cert=cert.pem'
|
|
|
|
# @example Disable optimization for low latency
|
|
|
|
# bind 'tcp://0.0.0.0:9292?low_latency=false'
|
|
|
|
# @example Socket permissions
|
|
|
|
# bind 'unix:///var/run/puma.sock?umask=0111'
|
2020-09-17 11:15:19 -04:00
|
|
|
# @see Puma::Runner#load_and_bind
|
|
|
|
# @see Puma::Cluster#run
|
|
|
|
#
|
2015-03-13 20:10:39 -04:00
|
|
|
def bind(url)
|
2017-03-03 15:30:28 -05:00
|
|
|
@options[:binds] ||= []
|
|
|
|
@options[:binds] << url
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2017-08-02 15:28:39 -04:00
|
|
|
def clear_binds!
|
|
|
|
@options[:binds] = []
|
|
|
|
end
|
|
|
|
|
2020-11-10 12:24:39 -05:00
|
|
|
# Bind to (systemd) activated sockets, regardless of configured binds.
|
|
|
|
#
|
|
|
|
# Systemd can present sockets as file descriptors that are already opened.
|
|
|
|
# By default Puma will use these but only if it was explicitly told to bind
|
|
|
|
# to the socket. If not, it will close the activated sockets. This means
|
|
|
|
# all configuration is duplicated.
|
|
|
|
#
|
|
|
|
# Binds can contain additional configuration, but only SSL config is really
|
|
|
|
# relevant since the unix and TCP socket options are ignored.
|
|
|
|
#
|
|
|
|
# This means there is a lot of duplicated configuration for no additional
|
|
|
|
# value in most setups. This method tells the launcher to bind to all
|
|
|
|
# activated sockets, regardless of existing bind.
|
|
|
|
#
|
|
|
|
# To clear configured binds, the value only can be passed. This will clear
|
|
|
|
# out any binds that may have been configured.
|
|
|
|
#
|
|
|
|
# @example Use any systemd activated sockets as well as configured binds
|
|
|
|
# bind_to_activated_sockets
|
|
|
|
#
|
|
|
|
# @example Only bind to systemd activated sockets, ignoring other binds
|
|
|
|
# bind_to_activated_sockets 'only'
|
|
|
|
def bind_to_activated_sockets(bind=true)
|
|
|
|
@options[:bind_to_activated_sockets] = bind
|
|
|
|
end
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
# Define the TCP port to bind to. Use +bind+ for more advanced options.
|
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @example
|
|
|
|
# port 9292
|
2016-02-06 22:00:29 -05:00
|
|
|
def port(port, host=nil)
|
Rack handler should use provided default host
This issue is somewhat tricky. When Rails is booted via `rails server` there are two types of configuration options passed, ones specified directly by a user like `rails s -p 3001` will always "win".
For any other config that is not explicitly passed in, puma will consider it a "default". For example when you run `rails s` (without -p) then the default port will be 3000.
There is one other way to configure puma though, and that is via a config file:
```
# config/puma.rb
port 3002
```
This is the order of precedence for configuration
1) Anything the user explicitly passes to `rails s`
2) Config specified in `config/puma.rb` file
3) Default values passed in via `rails s`
4) Defaults values stored in puma
This fallback mechanism works well except in the case of calling `port` in a `config/puma.rb` file. To understand look at the [old method definition](https://github.com/puma/puma/blob/2668597ec1dd9546d83db9f2ec5ad092add483e6/lib/puma/dsl.rb#L140-L145):
```
def port(port, host=nil)
host ||= Configuration::DefaultTCPHost
bind "tcp://#{host}:#{port}"
end
```
When the `port` method gets called, even if the user did not specify a `host` the `Configuration::DefaultTCPHost` will be used, which is a problem for local development because it defaults to `0.0.0.0`. [SO about 0.0.0.0 versus localhost](https://stackoverflow.com/questions/20778771/what-is-the-difference-between-0-0-0-0-127-0-0-1-and-localhost).
In this case, while a user did directly specify a port, they did not specify a host, so you would expect the `rails s` defaults passed in to take affect.
To make Puma respect that the host coming from `rails s` has more precedence than it's own default host, we must introduce the ability to set and retrieve a default_host value.
This is then used in the rack handler so when `rails s` passes in `:Host => "localhost"` then it is used instead of reverting to `0.0.0.0`.
The issue with #1699 is the test was wrong, it would have failed if a config file was present with a `port` invocation.
2019-01-04 14:15:31 -05:00
|
|
|
host ||= default_host
|
2016-02-06 22:00:29 -05:00
|
|
|
bind "tcp://#{host}:#{port}"
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2020-09-17 11:15:19 -04:00
|
|
|
# Define how long persistent connections can be idle before Puma closes them.
|
|
|
|
# @see Puma::Server.new
|
2016-07-15 13:03:44 -04:00
|
|
|
def persistent_timeout(seconds)
|
2018-03-19 17:05:15 -04:00
|
|
|
@options[:persistent_timeout] = Integer(seconds)
|
2016-07-15 13:03:44 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Define how long the tcp socket stays open, if no data has been received.
|
2020-09-17 11:15:19 -04:00
|
|
|
# @see Puma::Server.new
|
2017-06-04 09:18:54 -04:00
|
|
|
def first_data_timeout(seconds)
|
2018-03-19 17:05:15 -04:00
|
|
|
@options[:first_data_timeout] = Integer(seconds)
|
2017-06-04 09:18:54 -04:00
|
|
|
end
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
# Work around leaky apps that leave garbage in Thread locals
|
2019-08-05 20:30:43 -04:00
|
|
|
# across requests.
|
2015-03-13 20:10:39 -04:00
|
|
|
def clean_thread_locals(which=true)
|
|
|
|
@options[:clean_thread_locals] = which
|
|
|
|
end
|
|
|
|
|
2020-09-17 11:15:19 -04:00
|
|
|
# When shutting down, drain the accept socket of pending connections and
|
|
|
|
# process them. This loops over the accept socket until there are no more
|
|
|
|
# read events and then stops looking and waits for the requests to finish.
|
|
|
|
# @see Puma::Server#graceful_shutdown
|
|
|
|
#
|
2015-03-13 20:10:39 -04:00
|
|
|
def drain_on_shutdown(which=true)
|
|
|
|
@options[:drain_on_shutdown] = which
|
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Set the environment in which the rack's app will run. The value must be
|
|
|
|
# a string.
|
|
|
|
#
|
|
|
|
# The default is "development".
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# environment 'production'
|
2015-03-13 20:10:39 -04:00
|
|
|
def environment(environment)
|
|
|
|
@options[:environment] = environment
|
|
|
|
end
|
|
|
|
|
2016-04-07 13:25:10 -04:00
|
|
|
# How long to wait for threads to stop when shutting them
|
|
|
|
# down. Defaults to :forever. Specifying :immediately will cause
|
|
|
|
# Puma to kill the threads immediately. Otherwise the value
|
|
|
|
# is the number of seconds to wait.
|
|
|
|
#
|
|
|
|
# Puma always waits a few seconds after killing a thread for it to try
|
|
|
|
# to finish up it's work, even in :immediately mode.
|
2020-09-17 11:15:19 -04:00
|
|
|
# @see Puma::Server#graceful_shutdown
|
2016-04-07 13:25:10 -04:00
|
|
|
def force_shutdown_after(val=:forever)
|
|
|
|
i = case val
|
|
|
|
when :forever
|
|
|
|
-1
|
|
|
|
when :immediately
|
|
|
|
0
|
|
|
|
else
|
2020-04-16 21:38:30 -04:00
|
|
|
Float(val)
|
2016-04-07 13:25:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@options[:force_shutdown_after] = i
|
|
|
|
end
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
# Code to run before doing a restart. This code should
|
2019-08-05 20:30:43 -04:00
|
|
|
# close log files, database connections, etc.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
|
|
|
# This can be called multiple times to add code each time.
|
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @example
|
|
|
|
# on_restart do
|
|
|
|
# puts 'On restart...'
|
|
|
|
# end
|
2015-03-13 20:10:39 -04:00
|
|
|
def on_restart(&block)
|
2017-03-03 15:30:28 -05:00
|
|
|
@options[:on_restart] ||= []
|
|
|
|
@options[:on_restart] << block
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Command to use to restart Puma. This should be just how to
|
|
|
|
# load Puma itself (ie. 'ruby -Ilib bin/puma'), not the arguments
|
|
|
|
# to Puma, as those are the same as the original process.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @example
|
|
|
|
# restart_command '/u/app/lolcat/bin/restart_puma'
|
2015-03-13 20:10:39 -04:00
|
|
|
def restart_command(cmd)
|
2016-02-06 22:00:29 -05:00
|
|
|
@options[:restart_cmd] = cmd.to_s
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Store the pid of the server in the file at "path".
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# pidfile '/u/apps/lolcat/tmp/pids/puma.pid'
|
2015-03-13 20:10:39 -04:00
|
|
|
def pidfile(path)
|
2016-02-06 22:00:29 -05:00
|
|
|
@options[:pidfile] = path.to_s
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Disable request logging, if this isn't used it'll be enabled by default.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @example
|
|
|
|
# quiet
|
2016-02-25 16:09:02 -05:00
|
|
|
def quiet(which=true)
|
|
|
|
@options[:log_requests] = !which
|
|
|
|
end
|
|
|
|
|
|
|
|
# Enable request logging
|
|
|
|
#
|
|
|
|
def log_requests(which=true)
|
|
|
|
@options[:log_requests] = which
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
# Show debugging info
|
|
|
|
#
|
|
|
|
def debug
|
|
|
|
@options[:debug] = true
|
|
|
|
end
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
# Load +path+ as a rackup file.
|
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# The default is "config.ru".
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# rackup '/u/apps/lolcat/config.ru'
|
2015-03-13 20:10:39 -04:00
|
|
|
def rackup(path)
|
2020-04-27 19:15:33 -04:00
|
|
|
@options[:rackup] ||= path.to_s
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 08:30:16 -04:00
|
|
|
def early_hints(answer=true)
|
|
|
|
@options[:early_hints] = answer
|
|
|
|
end
|
|
|
|
|
2020-09-17 11:15:19 -04:00
|
|
|
# Redirect +STDOUT+ and +STDERR+ to files specified. The +append+ parameter
|
2019-08-05 20:30:43 -04:00
|
|
|
# specifies whether the output is appended, the default is +false+.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# stdout_redirect '/app/lolcat/log/stdout', '/app/lolcat/log/stderr'
|
|
|
|
# @example
|
|
|
|
# stdout_redirect '/app/lolcat/log/stdout', '/app/lolcat/log/stderr', true
|
2015-03-13 20:10:39 -04:00
|
|
|
def stdout_redirect(stdout=nil, stderr=nil, append=false)
|
|
|
|
@options[:redirect_stdout] = stdout
|
|
|
|
@options[:redirect_stderr] = stderr
|
|
|
|
@options[:redirect_append] = append
|
|
|
|
end
|
|
|
|
|
2019-08-01 15:21:23 -04:00
|
|
|
def log_formatter(&block)
|
|
|
|
@options[:log_formatter] = block
|
|
|
|
end
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
# Configure +min+ to be the minimum number of threads to use to answer
|
|
|
|
# requests and +max+ the maximum.
|
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# The default is "0, 16".
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# threads 0, 16
|
|
|
|
# @example
|
|
|
|
# threads 5, 5
|
2015-03-13 20:10:39 -04:00
|
|
|
def threads(min, max)
|
|
|
|
min = Integer(min)
|
|
|
|
max = Integer(max)
|
|
|
|
if min > max
|
2015-10-13 11:30:42 -04:00
|
|
|
raise "The minimum (#{min}) number of threads must be less than or equal to the max (#{max})"
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2016-01-28 20:00:17 -05:00
|
|
|
if max < 1
|
|
|
|
raise "The maximum number of threads (#{max}) must be greater than 0"
|
|
|
|
end
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
@options[:min_threads] = min
|
|
|
|
@options[:max_threads] = max
|
|
|
|
end
|
|
|
|
|
2020-09-17 11:15:19 -04:00
|
|
|
# Instead of `bind 'ssl://127.0.0.1:9292?key=key_path&cert=cert_path'` you
|
|
|
|
# can also use the this method.
|
2019-08-05 20:30:43 -04:00
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# ssl_bind '127.0.0.1', '9292', {
|
|
|
|
# cert: path_to_cert,
|
|
|
|
# key: path_to_key,
|
|
|
|
# ssl_cipher_filter: cipher_filter, # optional
|
|
|
|
# verify_mode: verify_mode, # default 'none'
|
|
|
|
# }
|
2020-11-27 10:40:26 -05:00
|
|
|
# @example For JRuby, two keys are required: keystore & keystore_pass.
|
2019-08-05 20:30:43 -04:00
|
|
|
# ssl_bind '127.0.0.1', '9292', {
|
|
|
|
# keystore: path_to_keystore,
|
2020-11-27 10:40:26 -05:00
|
|
|
# keystore_pass: password,
|
|
|
|
# ssl_cipher_list: cipher_list, # optional
|
|
|
|
# verify_mode: verify_mode # default 'none'
|
2019-08-05 20:30:43 -04:00
|
|
|
# }
|
2015-03-13 20:10:39 -04:00
|
|
|
def ssl_bind(host, port, opts)
|
2020-11-27 10:40:26 -05:00
|
|
|
bind self.class.ssl_bind_str(host, port, opts)
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Use +path+ as the file to store the server info state. This is
|
2019-08-05 20:30:43 -04:00
|
|
|
# used by +pumactl+ to query and control the server.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @example
|
|
|
|
# state_path '/u/apps/lolcat/tmp/pids/puma.state'
|
2015-03-13 20:10:39 -04:00
|
|
|
def state_path(path)
|
|
|
|
@options[:state] = path.to_s
|
|
|
|
end
|
|
|
|
|
2020-05-06 23:13:35 -04:00
|
|
|
# Use +permission+ to restrict permissions for the state file.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# state_permission 0600
|
2020-09-17 11:15:19 -04:00
|
|
|
# @version 5.0.0
|
|
|
|
#
|
2020-05-06 23:13:35 -04:00
|
|
|
def state_permission(permission)
|
|
|
|
@options[:state_permission] = permission
|
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# How many worker processes to run. Typically this is set to
|
2019-12-16 20:50:37 -05:00
|
|
|
# the number of available cores.
|
2019-08-05 20:30:43 -04:00
|
|
|
#
|
|
|
|
# The default is 0.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @note Cluster mode only.
|
2020-09-17 11:15:19 -04:00
|
|
|
# @see Puma::Cluster
|
2015-03-13 20:10:39 -04:00
|
|
|
def workers(count)
|
|
|
|
@options[:workers] = count.to_i
|
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Code to run immediately before master process
|
2015-08-05 20:03:36 -04:00
|
|
|
# forks workers (once on boot). These hooks can block if necessary
|
2019-08-05 20:30:43 -04:00
|
|
|
# to wait for background operations unknown to Puma to finish before
|
2015-08-05 20:03:36 -04:00
|
|
|
# the process terminates.
|
2019-08-05 20:30:43 -04:00
|
|
|
# This can be used to close any connections to remote servers (database,
|
|
|
|
# Redis, ...) that were opened when preloading the code.
|
2015-08-05 20:03:36 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# This can be called multiple times to add several hooks.
|
2015-08-05 20:03:36 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @note Cluster mode only.
|
|
|
|
# @example
|
|
|
|
# before_fork do
|
|
|
|
# puts "Starting workers..."
|
|
|
|
# end
|
2015-08-05 20:03:36 -04:00
|
|
|
def before_fork(&block)
|
2017-03-03 15:30:28 -05:00
|
|
|
@options[:before_fork] ||= []
|
|
|
|
@options[:before_fork] << block
|
2015-08-05 20:03:36 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Code to run in a worker when it boots to setup
|
2016-04-07 16:50:08 -04:00
|
|
|
# the process before booting the app.
|
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# This can be called multiple times to add several hooks.
|
2016-04-07 16:50:08 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @note Cluster mode only.
|
|
|
|
# @example
|
2020-05-15 14:03:31 -04:00
|
|
|
# on_worker_boot do
|
|
|
|
# puts 'Before worker boot...'
|
2019-08-05 20:30:43 -04:00
|
|
|
# end
|
2016-04-07 16:50:08 -04:00
|
|
|
def on_worker_boot(&block)
|
2017-03-03 15:30:28 -05:00
|
|
|
@options[:before_worker_boot] ||= []
|
|
|
|
@options[:before_worker_boot] << block
|
2016-04-07 16:50:08 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Code to run immediately before a worker shuts
|
2015-03-13 20:10:39 -04:00
|
|
|
# down (after it has finished processing HTTP requests). These hooks
|
|
|
|
# can block if necessary to wait for background operations unknown
|
2019-08-05 20:30:43 -04:00
|
|
|
# to Puma to finish before the process terminates.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# This can be called multiple times to add several hooks.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @note Cluster mode only.
|
|
|
|
# @example
|
|
|
|
# on_worker_shutdown do
|
|
|
|
# puts 'On worker shutdown...'
|
|
|
|
# end
|
2015-03-13 20:10:39 -04:00
|
|
|
def on_worker_shutdown(&block)
|
2017-03-03 15:30:28 -05:00
|
|
|
@options[:before_worker_shutdown] ||= []
|
|
|
|
@options[:before_worker_shutdown] << block
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Code to run in the master right before a worker is started. The worker's
|
|
|
|
# index is passed as an argument.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# This can be called multiple times to add several hooks.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @note Cluster mode only.
|
|
|
|
# @example
|
|
|
|
# on_worker_fork do
|
|
|
|
# puts 'Before worker fork...'
|
|
|
|
# end
|
2015-03-13 20:10:39 -04:00
|
|
|
def on_worker_fork(&block)
|
2017-03-03 15:30:28 -05:00
|
|
|
@options[:before_worker_fork] ||= []
|
|
|
|
@options[:before_worker_fork] << block
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Code to run in the master after a worker has been started. The worker's
|
|
|
|
# index is passed as an argument.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# This is called everytime a worker is to be started.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @note Cluster mode only.
|
|
|
|
# @example
|
|
|
|
# after_worker_fork do
|
|
|
|
# puts 'After worker fork...'
|
|
|
|
# end
|
2016-04-07 16:50:08 -04:00
|
|
|
def after_worker_fork(&block)
|
2017-03-03 15:30:28 -05:00
|
|
|
@options[:after_worker_fork] ||= []
|
|
|
|
@options[:after_worker_fork] = block
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2016-04-07 16:50:08 -04:00
|
|
|
alias_method :after_worker_boot, :after_worker_fork
|
|
|
|
|
2020-05-04 16:52:20 -04:00
|
|
|
# When `fork_worker` is enabled, code to run in Worker 0
|
|
|
|
# before all other workers are re-forked from this process,
|
|
|
|
# after the server has temporarily stopped serving requests
|
|
|
|
# (once per complete refork cycle).
|
|
|
|
#
|
|
|
|
# This can be used to trigger extra garbage-collection to maximize
|
|
|
|
# copy-on-write efficiency, or close any connections to remote servers
|
|
|
|
# (database, Redis, ...) that were opened while the server was running.
|
|
|
|
#
|
|
|
|
# This can be called multiple times to add several hooks.
|
|
|
|
#
|
|
|
|
# @note Cluster mode with `fork_worker` enabled only.
|
|
|
|
# @example
|
|
|
|
# on_refork do
|
|
|
|
# 3.times {GC.start}
|
|
|
|
# end
|
2020-09-17 11:15:19 -04:00
|
|
|
# @version 5.0.0
|
|
|
|
#
|
2020-05-04 16:52:20 -04:00
|
|
|
def on_refork(&block)
|
|
|
|
@options[:before_refork] ||= []
|
|
|
|
@options[:before_refork] << block
|
|
|
|
end
|
|
|
|
|
2018-09-13 14:49:09 -04:00
|
|
|
# Code to run out-of-band when the worker is idle.
|
|
|
|
# These hooks run immediately after a request has finished
|
|
|
|
# processing and there are no busy threads on the worker.
|
|
|
|
# The worker doesn't accept new requests until this code finishes.
|
|
|
|
#
|
|
|
|
# This hook is useful for running out-of-band garbage collection
|
|
|
|
# or scheduling asynchronous tasks to execute after a response.
|
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# This can be called multiple times to add several hooks.
|
2018-09-13 14:49:09 -04:00
|
|
|
def out_of_band(&block)
|
|
|
|
@options[:out_of_band] ||= []
|
|
|
|
@options[:out_of_band] << block
|
|
|
|
end
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
# The directory to operate out of.
|
2019-08-05 20:30:43 -04:00
|
|
|
#
|
|
|
|
# The default is the current directory.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# directory '/u/apps/lolcat'
|
2015-03-13 20:10:39 -04:00
|
|
|
def directory(dir)
|
|
|
|
@options[:directory] = dir.to_s
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Preload the application before starting the workers; this conflicts with
|
2020-11-09 12:18:25 -05:00
|
|
|
# phased restart feature. On by default if your app uses more than 1 worker.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @note Cluster mode only.
|
|
|
|
# @example
|
|
|
|
# preload_app!
|
2015-03-13 20:10:39 -04:00
|
|
|
def preload_app!(answer=true)
|
|
|
|
@options[:preload_app] = answer
|
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Use +obj+ or +block+ as the low level error handler. This allows the
|
|
|
|
# configuration file to change the default error on the server.
|
2015-03-13 20:10:39 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @example
|
|
|
|
# lowlevel_error_handler do |err|
|
|
|
|
# [200, {}, ["error page"]]
|
|
|
|
# end
|
2015-03-13 20:10:39 -04:00
|
|
|
def lowlevel_error_handler(obj=nil, &block)
|
|
|
|
obj ||= block
|
|
|
|
raise "Provide either a #call'able or a block" unless obj
|
|
|
|
@options[:lowlevel_error_handler] = obj
|
|
|
|
end
|
|
|
|
|
|
|
|
# This option is used to allow your app and its gems to be
|
|
|
|
# properly reloaded when not using preload.
|
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# When set, if Puma detects that it's been invoked in the
|
2015-03-13 20:10:39 -04:00
|
|
|
# context of Bundler, it will cleanup the environment and
|
|
|
|
# re-run itself outside the Bundler environment, but directly
|
|
|
|
# using the files that Bundler has setup.
|
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# This means that Puma is now decoupled from your Bundler
|
2015-03-13 20:10:39 -04:00
|
|
|
# context and when each worker loads, it will be loading a
|
|
|
|
# new Bundler context and thus can float around as the release
|
|
|
|
# dictates.
|
2019-08-05 20:30:43 -04:00
|
|
|
#
|
2020-09-17 11:15:19 -04:00
|
|
|
# @see extra_runtime_dependencies
|
2019-10-23 03:36:41 -04:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @note This is incompatible with +preload_app!+.
|
2019-09-02 12:10:33 -04:00
|
|
|
# @note This is only supported for RubyGems 2.2+
|
2015-03-13 20:10:39 -04:00
|
|
|
def prune_bundler(answer=true)
|
|
|
|
@options[:prune_bundler] = answer
|
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# By default, Puma will raise SignalException when SIGTERM is received. In
|
|
|
|
# environments where SIGTERM is something expected, you can suppress these
|
|
|
|
# with this option.
|
|
|
|
#
|
|
|
|
# This can be useful for example in Kubernetes, where rolling restart is
|
|
|
|
# guaranteed usually on infrastructure level.
|
2018-12-19 05:11:29 -05:00
|
|
|
#
|
2019-08-05 20:30:43 -04:00
|
|
|
# @example
|
|
|
|
# raise_exception_on_sigterm false
|
2020-09-17 11:15:19 -04:00
|
|
|
# @see Puma::Launcher#setup_signals
|
|
|
|
# @see Puma::Cluster#setup_signals
|
|
|
|
#
|
2018-12-19 05:11:29 -05:00
|
|
|
def raise_exception_on_sigterm(answer=true)
|
|
|
|
@options[:raise_exception_on_sigterm] = answer
|
|
|
|
end
|
|
|
|
|
2019-09-02 12:10:33 -04:00
|
|
|
# When using prune_bundler, if extra runtime dependencies need to be loaded to
|
2019-10-23 03:36:41 -04:00
|
|
|
# initialize your app, then this setting can be used. This includes any Puma plugins.
|
2019-09-02 12:10:33 -04:00
|
|
|
#
|
|
|
|
# Before bundler is pruned, the gem names supplied will be looked up in the bundler
|
|
|
|
# context and then loaded again after bundler is pruned.
|
|
|
|
# Only applies if prune_bundler is used.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# extra_runtime_dependencies ['gem_name_1', 'gem_name_2']
|
|
|
|
# @example
|
2019-10-23 03:36:41 -04:00
|
|
|
# extra_runtime_dependencies ['puma_worker_killer', 'puma-heroku']
|
2020-09-17 11:15:19 -04:00
|
|
|
# @see Puma::Launcher#extra_runtime_deps_directories
|
|
|
|
#
|
2019-09-02 12:10:33 -04:00
|
|
|
def extra_runtime_dependencies(answer = [])
|
|
|
|
@options[:extra_runtime_dependencies] = Array(answer)
|
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Additional text to display in process listing.
|
|
|
|
#
|
|
|
|
# If you do not specify a tag, Puma will infer it. If you do not want Puma
|
|
|
|
# to add a tag, use an empty string.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# tag 'app name'
|
|
|
|
# @example
|
|
|
|
# tag ''
|
2015-03-13 20:10:39 -04:00
|
|
|
def tag(string)
|
2016-02-06 22:00:29 -05:00
|
|
|
@options[:tag] = string.to_s
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Verifies that all workers have checked in to the master process within
|
|
|
|
# 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.
|
|
|
|
# Setting this value will not protect against slow requests.
|
|
|
|
#
|
|
|
|
# The minimum value is 6 seconds, the default value is 60 seconds.
|
|
|
|
#
|
|
|
|
# @note Cluster mode only.
|
|
|
|
# @example
|
|
|
|
# worker_timeout 60
|
2020-09-17 11:15:19 -04:00
|
|
|
# @see Puma::Cluster::Worker#ping_timeout
|
|
|
|
#
|
2015-03-13 20:10:39 -04:00
|
|
|
def worker_timeout(timeout)
|
2019-01-24 18:55:01 -05:00
|
|
|
timeout = Integer(timeout)
|
2019-03-20 00:06:25 -04:00
|
|
|
min = Const::WORKER_CHECK_INTERVAL
|
2019-01-24 18:55:01 -05:00
|
|
|
|
|
|
|
if timeout <= min
|
|
|
|
raise "The minimum worker_timeout must be greater than the worker reporting interval (#{min})"
|
|
|
|
end
|
|
|
|
|
2019-07-14 07:17:39 -04:00
|
|
|
@options[:worker_timeout] = timeout
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2019-08-05 20:30:43 -04:00
|
|
|
# Change the default worker timeout for booting.
|
|
|
|
#
|
|
|
|
# If unspecified, this defaults to the value of worker_timeout.
|
|
|
|
#
|
|
|
|
# @note Cluster mode only.
|
2020-09-17 11:15:19 -04:00
|
|
|
#
|
|
|
|
# @example
|
2019-08-05 20:30:43 -04:00
|
|
|
# worker_boot_timeout 60
|
2020-09-17 11:15:19 -04:00
|
|
|
# @see Puma::Cluster::Worker#ping_timeout
|
|
|
|
#
|
2015-11-06 11:46:48 -05:00
|
|
|
def worker_boot_timeout(timeout)
|
2018-03-19 17:05:15 -04:00
|
|
|
@options[:worker_boot_timeout] = Integer(timeout)
|
2015-11-06 11:46:48 -05:00
|
|
|
end
|
|
|
|
|
2020-09-17 11:15:19 -04:00
|
|
|
# Set the timeout for worker shutdown.
|
2019-08-05 20:30:43 -04:00
|
|
|
#
|
|
|
|
# @note Cluster mode only.
|
2020-09-17 11:15:19 -04:00
|
|
|
# @see Puma::Cluster::Worker#term
|
|
|
|
#
|
2015-03-13 20:10:39 -04:00
|
|
|
def worker_shutdown_timeout(timeout)
|
2018-03-19 17:05:15 -04:00
|
|
|
@options[:worker_shutdown_timeout] = Integer(timeout)
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# When set to true (the default), workers accept all requests
|
|
|
|
# and queue them before passing them to the handlers.
|
|
|
|
# When set to false, each worker process accepts exactly as
|
|
|
|
# many requests as it is configured to simultaneously handle.
|
|
|
|
#
|
|
|
|
# Queueing requests generally improves performance. In some
|
|
|
|
# cases, such as a single threaded application, it may be
|
|
|
|
# better to ensure requests get balanced across workers.
|
|
|
|
#
|
|
|
|
# Note that setting this to false disables HTTP keepalive and
|
|
|
|
# slow clients will occupy a handler thread while the request
|
|
|
|
# is being sent. A reverse proxy, such as nginx, can handle
|
2019-08-05 20:30:43 -04:00
|
|
|
# slow clients and queue requests before they reach Puma.
|
2020-09-17 11:15:19 -04:00
|
|
|
# @see Puma::Server
|
2015-03-13 20:10:39 -04:00
|
|
|
def queue_requests(answer=true)
|
|
|
|
@options[:queue_requests] = answer
|
|
|
|
end
|
2015-04-11 01:52:38 -04:00
|
|
|
|
|
|
|
# When a shutdown is requested, the backtraces of all the
|
|
|
|
# threads will be written to $stdout. This can help figure
|
|
|
|
# out why shutdown is hanging.
|
2020-09-17 11:15:19 -04:00
|
|
|
#
|
2015-04-11 01:52:38 -04:00
|
|
|
def shutdown_debug(val=true)
|
|
|
|
@options[:shutdown_debug] = val
|
|
|
|
end
|
2016-01-06 13:12:09 -05:00
|
|
|
|
2020-05-10 21:43:24 -04:00
|
|
|
|
|
|
|
# Attempts to route traffic to less-busy workers by causing them to delay
|
|
|
|
# listening on the socket, allowing workers which are not processing any
|
|
|
|
# requests to pick up new requests first.
|
2020-05-10 21:49:51 -04:00
|
|
|
#
|
2020-05-10 21:43:24 -04:00
|
|
|
# Only works on MRI. For all other interpreters, this setting does nothing.
|
2020-09-17 11:15:19 -04:00
|
|
|
# @see Puma::Server#handle_servers
|
|
|
|
# @see Puma::ThreadPool#wait_for_less_busy_worker
|
|
|
|
# @version 5.0.0
|
|
|
|
#
|
2020-05-11 00:49:00 -04:00
|
|
|
def wait_for_less_busy_worker(val=0.005)
|
2019-11-20 09:39:37 -05:00
|
|
|
@options[:wait_for_less_busy_worker] = val.to_f
|
|
|
|
end
|
|
|
|
|
2016-01-06 13:12:09 -05:00
|
|
|
# Control how the remote address of the connection is set. This
|
|
|
|
# is configurable because to calculate the true socket peer address
|
|
|
|
# a kernel syscall is required which for very fast rack handlers
|
|
|
|
# slows down the handling significantly.
|
|
|
|
#
|
|
|
|
# There are 4 possible values:
|
|
|
|
#
|
2020-09-17 11:15:19 -04:00
|
|
|
# 1. **:socket** (the default) - read the peername from the socket using the
|
|
|
|
# syscall. This is the normal behavior.
|
|
|
|
# 2. **:localhost** - set the remote address to "127.0.0.1"
|
|
|
|
# 3. **header: <http_header>**- set the remote address to the value of the
|
|
|
|
# provided http header. For instance:
|
|
|
|
# `set_remote_address header: "X-Real-IP"`.
|
|
|
|
# Only the first word (as separated by spaces or comma) is used, allowing
|
|
|
|
# headers such as X-Forwarded-For to be used as well.
|
|
|
|
# 4. **\<Any string\>** - this allows you to hardcode remote address to any value
|
|
|
|
# you wish. Because Puma never uses this field anyway, it's format is
|
|
|
|
# entirely in your hands.
|
|
|
|
#
|
2016-01-06 13:12:09 -05:00
|
|
|
def set_remote_address(val=:socket)
|
|
|
|
case val
|
|
|
|
when :socket
|
|
|
|
@options[:remote_address] = val
|
|
|
|
when :localhost
|
|
|
|
@options[:remote_address] = :value
|
|
|
|
@options[:remote_address_value] = "127.0.0.1".freeze
|
|
|
|
when String
|
|
|
|
@options[:remote_address] = :value
|
|
|
|
@options[:remote_address_value] = val
|
|
|
|
when Hash
|
|
|
|
if hdr = val[:header]
|
|
|
|
@options[:remote_address] = :header
|
2018-07-30 22:30:32 -04:00
|
|
|
@options[:remote_address_header] = "HTTP_" + hdr.upcase.tr("-", "_")
|
2016-01-06 13:12:09 -05:00
|
|
|
else
|
|
|
|
raise "Invalid value for set_remote_address - #{val.inspect}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise "Invalid value for set_remote_address - #{val}"
|
|
|
|
end
|
|
|
|
end
|
2016-02-07 01:28:02 -05:00
|
|
|
|
2020-05-01 18:44:58 -04:00
|
|
|
# When enabled, workers will be forked from worker 0 instead of from the master process.
|
|
|
|
# This option is similar to `preload_app` because the app is preloaded before forking,
|
|
|
|
# but it is compatible with phased restart.
|
|
|
|
#
|
|
|
|
# This option also enables the `refork` command (SIGURG), which optimizes copy-on-write performance
|
|
|
|
# in a running app.
|
|
|
|
#
|
|
|
|
# A refork will automatically trigger once after the specified number of requests
|
|
|
|
# (default 1000), or pass 0 to disable auto refork.
|
|
|
|
#
|
|
|
|
# @note Cluster mode only.
|
2020-09-17 11:15:19 -04:00
|
|
|
# @version 5.0.0
|
|
|
|
#
|
2020-05-01 18:44:58 -04:00
|
|
|
def fork_worker(after_requests=1000)
|
|
|
|
@options[:fork_worker] = Integer(after_requests)
|
|
|
|
end
|
2020-05-10 23:50:36 -04:00
|
|
|
|
|
|
|
# When enabled, Puma will GC 4 times before forking workers.
|
|
|
|
# If available (Ruby 2.7+), we will also call GC.compact.
|
|
|
|
# Not recommended for non-MRI Rubies.
|
|
|
|
#
|
|
|
|
# Based on the work of Koichi Sasada and Aaron Patterson, this option may
|
|
|
|
# decrease memory utilization of preload-enabled cluster-mode Pumas. It will
|
|
|
|
# also increase time to boot and fork. See your logs for details on how much
|
|
|
|
# time this adds to your boot process. For most apps, it will be less than one
|
|
|
|
# second.
|
2020-09-17 11:15:19 -04:00
|
|
|
#
|
|
|
|
# @see Puma::Cluster#nakayoshi_gc
|
|
|
|
# @version 5.0.0
|
|
|
|
#
|
2020-05-18 17:27:19 -04:00
|
|
|
def nakayoshi_fork(enabled=true)
|
2020-05-10 23:50:36 -04:00
|
|
|
@options[:nakayoshi_fork] = enabled
|
|
|
|
end
|
2020-10-26 17:59:30 -04:00
|
|
|
|
|
|
|
# The number of requests to attempt inline before sending a client back to
|
|
|
|
# the reactor to be subject to normal ordering.
|
|
|
|
#
|
|
|
|
def max_fast_inline(num_of_requests)
|
|
|
|
@options[:max_fast_inline] = Float(num_of_requests)
|
|
|
|
end
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
end
|