Class: Puma::DSL
Overview
The methods that are available for use inside the configuration file. These same methods are used in Puma cli and the rack handler internally.
Used manually (via CLI class):
config = Configuration.new({}) do |user_config|
user_config.port 3001
end
config.load
puts config.[:binds] # => "tcp://127.0.0.1:3001"
Used to load file:
$ cat puma_config.rb
port 3002
Resulting configuration:
config = Configuration.new(config_file: "puma_config.rb")
config.load
puts config.[:binds] # => "tcp://127.0.0.1:3002"
You can also find many examples being used by the test suite in test/config
.
Constant Summary
Constants included from ConfigDefault
ConfigDefault::DefaultRackup, ConfigDefault::DefaultTCPHost, ConfigDefault::DefaultTCPPort, ConfigDefault::DefaultWorkerShutdownTimeout, ConfigDefault::DefaultWorkerTimeout
Class Method Summary collapse
-
.ssl_bind_str(host, port, opts) ⇒ Object
convenience method so logic can be used in CI.
Instance Method Summary collapse
- #_load_from(path) ⇒ Object
- #_offer_plugins ⇒ Object
-
#activate_control_app(url = "auto", opts = {}) ⇒ Object
Start the Puma control rack application on
url
. -
#after_worker_fork(&block) ⇒ Object
(also: #after_worker_boot)
Code to run in the master after a worker has been started.
-
#app(obj = nil, &block) ⇒ Object
Use an object or block as the rack application.
-
#before_fork(&block) ⇒ Object
Code to run immediately before master process forks workers (once on boot).
-
#bind(url) ⇒ Object
Bind the server to
url
. -
#bind_to_activated_sockets(bind = true) ⇒ Object
Bind to (systemd) activated sockets, regardless of configured binds.
-
#clean_thread_locals(which = true) ⇒ Object
Work around leaky apps that leave garbage in Thread locals across requests.
- #clear_binds! ⇒ Object
-
#debug ⇒ Object
Show debugging info.
- #default_host ⇒ Object
-
#directory(dir) ⇒ Object
The directory to operate out of.
-
#drain_on_shutdown(which = true) ⇒ Object
When shutting down, drain the accept socket of pending connections and process them.
- #early_hints(answer = true) ⇒ Object
-
#environment(environment) ⇒ Object
Set the environment in which the rack’s app will run.
-
#extra_runtime_dependencies(answer = []) ⇒ Object
When using prune_bundler, if extra runtime dependencies need to be loaded to initialize your app, then this setting can be used.
-
#first_data_timeout(seconds) ⇒ Object
Define how long the tcp socket stays open, if no data has been received.
-
#force_shutdown_after(val = :forever) ⇒ Object
How long to wait for threads to stop when shutting them down.
-
#fork_worker(after_requests = 1000) ⇒ Object
When enabled, workers will be forked from worker 0 instead of from the master process.
- #get(key, default = nil) ⇒ Object
-
#initialize(options, config) ⇒ DSL
constructor
A new instance of DSL.
- #inject(&blk) ⇒ Object
-
#io_selector_backend(backend) ⇒ Object
Specify the backend for the IO selector.
-
#load(file) ⇒ Object
Load additional configuration from a file Files get loaded later via Configuration#load.
- #log_formatter(&block) ⇒ Object
-
#log_requests(which = true) ⇒ Object
Enable request logging.
-
#lowlevel_error_handler(obj = nil, &block) ⇒ Object
Use
obj
orblock
as the low level error handler. -
#max_fast_inline(num_of_requests) ⇒ Object
The number of requests to attempt inline before sending a client back to the reactor to be subject to normal ordering.
- #mutate_stdout_and_stderr_to_sync_on_write(enabled = true) ⇒ Object
-
#nakayoshi_fork(enabled = true) ⇒ Object
When enabled, Puma will GC 4 times before forking workers.
-
#on_refork(&block) ⇒ Object
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).
-
#on_restart(&block) ⇒ Object
Code to run before doing a restart.
-
#on_worker_boot(&block) ⇒ Object
Code to run in a worker when it boots to setup the process before booting the app.
-
#on_worker_fork(&block) ⇒ Object
Code to run in the master right before a worker is started.
-
#on_worker_shutdown(&block) ⇒ Object
Code to run immediately before a worker shuts down (after it has finished processing HTTP requests).
-
#out_of_band(&block) ⇒ Object
Code to run out-of-band when the worker is idle.
-
#persistent_timeout(seconds) ⇒ Object
Define how long persistent connections can be idle before Puma closes them.
-
#pidfile(path) ⇒ Object
Store the pid of the server in the file at “path”.
-
#plugin(name) ⇒ Object
Load the named plugin for use by this configuration.
-
#port(port, host = nil) ⇒ Object
Define the TCP port to bind to.
-
#preload_app!(answer = true) ⇒ Object
Preload the application before starting the workers; this conflicts with phased restart feature.
-
#prune_bundler(answer = true) ⇒ Object
This option is used to allow your app and its gems to be properly reloaded when not using preload.
-
#queue_requests(answer = true) ⇒ Object
When set to true (the default), workers accept all requests and queue them before passing them to the handlers.
-
#quiet(which = true) ⇒ Object
Disable request logging, if this isn’t used it’ll be enabled by default.
-
#rack_url_scheme(scheme = nil) ⇒ Object
Allows setting ‘env`.
-
#rackup(path) ⇒ Object
Load
path
as a rackup file. -
#raise_exception_on_sigterm(answer = true) ⇒ Object
By default, Puma will raise SignalException when SIGTERM is received.
-
#restart_command(cmd) ⇒ Object
Command to use to restart Puma.
- #set_default_host(host) ⇒ Object
-
#set_remote_address(val = :socket) ⇒ Object
Control how the remote address of the connection is set.
-
#shutdown_debug(val = true) ⇒ Object
When a shutdown is requested, the backtraces of all the threads will be written to $stdout.
-
#silence_single_worker_warning ⇒ Object
Disable warning message when running in cluster mode with a single worker.
-
#ssl_bind(host, port, opts) ⇒ Object
Instead of ‘bind ’ssl://127.0.0.1:9292?key=key_path&cert=cert_path’‘ you can also use the this method.
-
#state_path(path) ⇒ Object
Use
path
as the file to store the server info state. -
#state_permission(permission) ⇒ Object
Use
permission
to restrict permissions for the state file. -
#stdout_redirect(stdout = nil, stderr = nil, append = false) ⇒ Object
Redirect
STDOUT
andSTDERR
to files specified. -
#tag(string) ⇒ Object
Additional text to display in process listing.
-
#threads(min, max) ⇒ Object
Configure
min
to be the minimum number of threads to use to answer requests andmax
the maximum. -
#wait_for_less_busy_worker(val = 0.005) ⇒ Object
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.
-
#worker_boot_timeout(timeout) ⇒ Object
Change the default worker timeout for booting.
-
#worker_shutdown_timeout(timeout) ⇒ Object
Set the timeout for worker shutdown.
-
#worker_timeout(timeout) ⇒ Object
Verifies that all workers have checked in to the master process within the given timeout.
-
#workers(count) ⇒ Object
How many worker processes to run.
Constructor Details
#initialize(options, config) ⇒ DSL
Returns a new instance of DSL.
71 72 73 74 75 76 |
# File 'lib/puma/dsl.rb', line 71 def initialize(, config) @config = config @options = @plugins = [] end |
Class Method Details
.ssl_bind_str(host, port, opts) ⇒ Object
convenience method so logic can be used in CI
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/puma/dsl.rb', line 40 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 v_flags = (ary = opts[:verification_flags]) ? "&verification_flags=#{Array(ary).join ','}" : nil "ssl://#{host}:#{port}?cert=#{opts[:cert]}&key=#{opts[:key]}" \ "#{ssl_cipher_filter}&verify_mode=#{verify}#{tls_str}#{ca_additions}#{v_flags}" end end |
Instance Method Details
#_load_from(path) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/puma/dsl.rb', line 78 def _load_from(path) if path @path = path instance_eval(File.read(path), path, 1) end ensure _offer_plugins end |
#_offer_plugins ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/puma/dsl.rb', line 87 def _offer_plugins @plugins.each do |o| if o.respond_to? :config @options.shift o.config self end end @plugins.clear end |
#activate_control_app(url = "auto", opts = {}) ⇒ Object
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 App::Status to see what the app has available.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/puma/dsl.rb', line 161 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 if opts[:no_token] # 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' else auth_token = opts[:auth_token] auth_token ||= Configuration.random_token end @options[:control_auth_token] = auth_token @options[:control_url_umask] = opts[:umask] if opts[:umask] end |
#after_worker_fork(&block) ⇒ Object Also known as: after_worker_boot
Cluster mode only.
Code to run in the master after a worker has been started. The worker’s index is passed as an argument.
This is called everytime a worker is to be started.
586 587 588 589 |
# File 'lib/puma/dsl.rb', line 586 def after_worker_fork(&block) @options[:after_worker_fork] ||= [] @options[:after_worker_fork] = block end |
#app(obj = nil, &block) ⇒ Object
Use an object or block as the rack application. This allows the configuration file to be the application itself.
139 140 141 142 143 144 145 |
# File 'lib/puma/dsl.rb', line 139 def app(obj=nil, &block) obj ||= block raise "Provide either a #call'able or a block" unless obj @options[:app] = obj end |
#before_fork(&block) ⇒ Object
Cluster mode only.
Code to run immediately before master process forks workers (once on boot). These hooks can block if necessary to wait for background operations unknown to Puma to finish before the process terminates. This can be used to close any connections to remote servers (database, Redis, …) that were opened when preloading the code.
This can be called multiple times to add several hooks.
524 525 526 527 |
# File 'lib/puma/dsl.rb', line 524 def before_fork(&block) @options[:before_fork] ||= [] @options[:before_fork] << block end |
#bind(url) ⇒ Object
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.
The default is “tcp://0.0.0.0:9292”.
You can use query parameters within the url to specify options:
-
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 not optimize for low latency. This is done viaSocket::TCP_NODELAY
. -
Set socket permissions with
umask
.
219 220 221 222 |
# File 'lib/puma/dsl.rb', line 219 def bind(url) @options[:binds] ||= [] @options[:binds] << url end |
#bind_to_activated_sockets(bind = true) ⇒ Object
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.
250 251 252 |
# File 'lib/puma/dsl.rb', line 250 def bind_to_activated_sockets(bind=true) @options[:bind_to_activated_sockets] = bind end |
#clean_thread_locals(which = true) ⇒ Object
Work around leaky apps that leave garbage in Thread locals across requests.
277 278 279 |
# File 'lib/puma/dsl.rb', line 277 def clean_thread_locals(which=true) @options[:clean_thread_locals] = which end |
#clear_binds! ⇒ Object
224 225 226 |
# File 'lib/puma/dsl.rb', line 224 def clear_binds! @options[:binds] = [] end |
#debug ⇒ Object
Show debugging info
370 371 372 |
# File 'lib/puma/dsl.rb', line 370 def debug @options[:debug] = true end |
#default_host ⇒ Object
102 103 104 |
# File 'lib/puma/dsl.rb', line 102 def default_host @options[:default_host] || Configuration::DefaultTCPHost end |
#directory(dir) ⇒ Object
The directory to operate out of.
The default is the current directory.
636 637 638 |
# File 'lib/puma/dsl.rb', line 636 def directory(dir) @options[:directory] = dir.to_s end |
#drain_on_shutdown(which = true) ⇒ Object
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.
286 287 288 |
# File 'lib/puma/dsl.rb', line 286 def drain_on_shutdown(which=true) @options[:drain_on_shutdown] = which end |
#early_hints(answer = true) ⇒ Object
391 392 393 |
# File 'lib/puma/dsl.rb', line 391 def early_hints(answer=true) @options[:early_hints] = answer end |
#environment(environment) ⇒ Object
Set the environment in which the rack’s app will run. The value must be a string.
The default is “development”.
297 298 299 |
# File 'lib/puma/dsl.rb', line 297 def environment(environment) @options[:environment] = environment end |
#extra_runtime_dependencies(answer = []) ⇒ Object
When using prune_bundler, if extra runtime dependencies need to be loaded to initialize your app, then this setting can be used. This includes any Puma plugins.
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.
713 714 715 |
# File 'lib/puma/dsl.rb', line 713 def extra_runtime_dependencies(answer = []) @options[:extra_runtime_dependencies] = Array(answer) end |
#first_data_timeout(seconds) ⇒ Object
Define how long the tcp socket stays open, if no data has been received.
271 272 273 |
# File 'lib/puma/dsl.rb', line 271 def first_data_timeout(seconds) @options[:first_data_timeout] = Integer(seconds) end |
#force_shutdown_after(val = :forever) ⇒ Object
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.
309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/puma/dsl.rb', line 309 def force_shutdown_after(val=:forever) i = case val when :forever -1 when :immediately 0 else Float(val) end @options[:force_shutdown_after] = i end |
#fork_worker(after_requests = 1000) ⇒ Object
Cluster mode only.
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.
870 871 872 |
# File 'lib/puma/dsl.rb', line 870 def fork_worker(after_requests=1000) @options[:fork_worker] = Integer(after_requests) end |
#get(key, default = nil) ⇒ Object
110 111 112 |
# File 'lib/puma/dsl.rb', line 110 def get(key,default=nil) @options[key.to_sym] || default end |
#inject(&blk) ⇒ Object
106 107 108 |
# File 'lib/puma/dsl.rb', line 106 def inject(&blk) instance_eval(&blk) end |
#io_selector_backend(backend) ⇒ Object
Specify the backend for the IO selector.
Provided values will be passed directly to NIO::Selector.new
, with the exception of :auto
which will let nio4r choose the backend.
Check the documentation of NIO::Selector.backends
for the list of valid options. Note that the available options on your system will depend on the operating system. If you want to use the pure Ruby backend (not recommended due to its comparatively low performance), set environment variable NIO4R_PURE
to true
.
The default is :auto
.
913 914 915 |
# File 'lib/puma/dsl.rb', line 913 def io_selector_backend(backend) @options[:io_selector_backend] = backend.to_sym end |
#load(file) ⇒ Object
Load additional configuration from a file Files get loaded later via Configuration#load
188 189 190 191 |
# File 'lib/puma/dsl.rb', line 188 def load(file) @options[:config_files] ||= [] @options[:config_files] << file end |
#log_formatter(&block) ⇒ Object
408 409 410 |
# File 'lib/puma/dsl.rb', line 408 def log_formatter(&block) @options[:log_formatter] = block end |
#log_requests(which = true) ⇒ Object
Enable request logging
364 365 366 |
# File 'lib/puma/dsl.rb', line 364 def log_requests(which=true) @options[:log_requests] = which end |
#lowlevel_error_handler(obj = nil, &block) ⇒ Object
Use obj
or block
as the low level error handler. This allows the configuration file to change the default error on the server.
657 658 659 660 661 |
# File 'lib/puma/dsl.rb', line 657 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 |
#max_fast_inline(num_of_requests) ⇒ Object
The number of requests to attempt inline before sending a client back to the reactor to be subject to normal ordering.
894 895 896 |
# File 'lib/puma/dsl.rb', line 894 def max_fast_inline(num_of_requests) @options[:max_fast_inline] = Float(num_of_requests) end |
#mutate_stdout_and_stderr_to_sync_on_write(enabled = true) ⇒ Object
917 918 919 |
# File 'lib/puma/dsl.rb', line 917 def mutate_stdout_and_stderr_to_sync_on_write(enabled=true) @options[:mutate_stdout_and_stderr_to_sync_on_write] = enabled end |
#nakayoshi_fork(enabled = true) ⇒ Object
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.
887 888 889 |
# File 'lib/puma/dsl.rb', line 887 def nakayoshi_fork(enabled=true) @options[:nakayoshi_fork] = enabled end |
#on_refork(&block) ⇒ Object
Cluster mode with ‘fork_worker` enabled only.
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.
611 612 613 614 |
# File 'lib/puma/dsl.rb', line 611 def on_refork(&block) @options[:before_refork] ||= [] @options[:before_refork] << block end |
#on_restart(&block) ⇒ Object
Code to run before doing a restart. This code should close log files, database connections, etc.
This can be called multiple times to add code each time.
331 332 333 334 |
# File 'lib/puma/dsl.rb', line 331 def on_restart(&block) @options[:on_restart] ||= [] @options[:on_restart] << block end |
#on_worker_boot(&block) ⇒ Object
Cluster mode only.
Code to run in a worker when it boots to setup the process before booting the app.
This can be called multiple times to add several hooks.
539 540 541 542 |
# File 'lib/puma/dsl.rb', line 539 def on_worker_boot(&block) @options[:before_worker_boot] ||= [] @options[:before_worker_boot] << block end |
#on_worker_fork(&block) ⇒ Object
Cluster mode only.
Code to run in the master right before a worker is started. The worker’s index is passed as an argument.
This can be called multiple times to add several hooks.
571 572 573 574 |
# File 'lib/puma/dsl.rb', line 571 def on_worker_fork(&block) @options[:before_worker_fork] ||= [] @options[:before_worker_fork] << block end |
#on_worker_shutdown(&block) ⇒ Object
Cluster mode only.
Code to run immediately before a worker shuts down (after it has finished processing HTTP requests). These hooks can block if necessary to wait for background operations unknown to Puma to finish before the process terminates.
This can be called multiple times to add several hooks.
556 557 558 559 |
# File 'lib/puma/dsl.rb', line 556 def on_worker_shutdown(&block) @options[:before_worker_shutdown] ||= [] @options[:before_worker_shutdown] << block end |
#out_of_band(&block) ⇒ Object
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.
This can be called multiple times to add several hooks.
625 626 627 628 |
# File 'lib/puma/dsl.rb', line 625 def out_of_band(&block) @options[:out_of_band] ||= [] @options[:out_of_band] << block end |
#persistent_timeout(seconds) ⇒ Object
Define how long persistent connections can be idle before Puma closes them.
265 266 267 |
# File 'lib/puma/dsl.rb', line 265 def persistent_timeout(seconds) @options[:persistent_timeout] = Integer(seconds) end |
#pidfile(path) ⇒ Object
Store the pid of the server in the file at “path”.
350 351 352 |
# File 'lib/puma/dsl.rb', line 350 def pidfile(path) @options[:pidfile] = path.to_s end |
#plugin(name) ⇒ Object
Load the named plugin for use by this configuration
116 117 118 |
# File 'lib/puma/dsl.rb', line 116 def plugin(name) @plugins << @config.load_plugin(name) end |
#port(port, host = nil) ⇒ Object
Define the TCP port to bind to. Use bind
for more advanced options.
258 259 260 261 |
# File 'lib/puma/dsl.rb', line 258 def port(port, host=nil) host ||= default_host bind URI::Generic.build(scheme: 'tcp', host: host, port: Integer(port)).to_s end |
#preload_app!(answer = true) ⇒ Object
Cluster mode only.
Preload the application before starting the workers; this conflicts with phased restart feature. On by default if your app uses more than 1 worker.
646 647 648 |
# File 'lib/puma/dsl.rb', line 646 def preload_app!(answer=true) @options[:preload_app] = answer end |
#prune_bundler(answer = true) ⇒ Object
This is incompatible with preload_app!
.
This is only supported for RubyGems 2.2+
This option is used to allow your app and its gems to be properly reloaded when not using preload.
When set, if Puma detects that it’s been invoked in the 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.
This means that Puma is now decoupled from your Bundler context and when each worker loads, it will be loading a new Bundler context and thus can float around as the release dictates.
680 681 682 |
# File 'lib/puma/dsl.rb', line 680 def prune_bundler(answer=true) @options[:prune_bundler] = answer end |
#queue_requests(answer = true) ⇒ Object
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 slow clients and queue requests before they reach Puma.
790 791 792 |
# File 'lib/puma/dsl.rb', line 790 def queue_requests(answer=true) @options[:queue_requests] = answer end |
#quiet(which = true) ⇒ Object
Disable request logging, if this isn’t used it’ll be enabled by default.
358 359 360 |
# File 'lib/puma/dsl.rb', line 358 def quiet(which=true) @options[:log_requests] = !which end |
#rack_url_scheme(scheme = nil) ⇒ Object
Allows setting ‘env`. Only necessary if X-Forwarded-Proto is not being set by your proxy Normal values are ’http’ or ‘https’.
387 388 389 |
# File 'lib/puma/dsl.rb', line 387 def rack_url_scheme(scheme=nil) @options[:rack_url_scheme] = scheme end |
#rackup(path) ⇒ Object
Load path
as a rackup file.
The default is “config.ru”.
380 381 382 |
# File 'lib/puma/dsl.rb', line 380 def rackup(path) @options[:rackup] ||= path.to_s end |
#raise_exception_on_sigterm(answer = true) ⇒ Object
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.
696 697 698 |
# File 'lib/puma/dsl.rb', line 696 def raise_exception_on_sigterm(answer=true) @options[:raise_exception_on_sigterm] = answer end |
#restart_command(cmd) ⇒ Object
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.
342 343 344 |
# File 'lib/puma/dsl.rb', line 342 def restart_command(cmd) @options[:restart_cmd] = cmd.to_s end |
#set_default_host(host) ⇒ Object
98 99 100 |
# File 'lib/puma/dsl.rb', line 98 def set_default_host(host) @options[:default_host] = host end |
#set_remote_address(val = :socket) ⇒ Object
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:
-
:socket (the default) - read the peername from the socket using the syscall. This is the normal behavior.
-
:localhost - set the remote address to “127.0.0.1”
-
**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.
-
**<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.
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 |
# File 'lib/puma/dsl.rb', line 835 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 @options[:remote_address_header] = "HTTP_" + hdr.upcase.tr("-", "_") else raise "Invalid value for set_remote_address - #{val.inspect}" end else raise "Invalid value for set_remote_address - #{val}" end end |
#shutdown_debug(val = true) ⇒ Object
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.
798 799 800 |
# File 'lib/puma/dsl.rb', line 798 def shutdown_debug(val=true) @options[:shutdown_debug] = val end |
#silence_single_worker_warning ⇒ Object
Cluster mode only.
Disable warning message when running in cluster mode with a single worker.
Cluster mode has some overhead of running an additional ‘control’ process in order to manage the cluster. If only running a single worker it is likely not worth paying that overhead vs running in single mode with additional threads instead.
There are some scenarios where running cluster mode with a single worker may still be warranted and valid under certain deployment scenarios, see github.com/puma/puma/issues/2534
Moving from workers = 1 to workers = 0 will save 10-30% of memory use.
506 507 508 |
# File 'lib/puma/dsl.rb', line 506 def silence_single_worker_warning @options[:silence_single_worker_warning] = true end |
#ssl_bind(host, port, opts) ⇒ Object
Instead of ‘bind ’ssl://127.0.0.1:9292?key=key_path&cert=cert_path’‘ you can also use the this method.
457 458 459 |
# File 'lib/puma/dsl.rb', line 457 def ssl_bind(host, port, opts) bind self.class.ssl_bind_str(host, port, opts) end |
#state_path(path) ⇒ Object
Use path
as the file to store the server info state. This is used by pumactl
to query and control the server.
466 467 468 |
# File 'lib/puma/dsl.rb', line 466 def state_path(path) @options[:state] = path.to_s end |
#state_permission(permission) ⇒ Object
Use permission
to restrict permissions for the state file.
476 477 478 |
# File 'lib/puma/dsl.rb', line 476 def () @options[:state_permission] = end |
#stdout_redirect(stdout = nil, stderr = nil, append = false) ⇒ Object
Redirect STDOUT
and STDERR
to files specified. The append
parameter specifies whether the output is appended, the default is false
.
402 403 404 405 406 |
# File 'lib/puma/dsl.rb', line 402 def stdout_redirect(stdout=nil, stderr=nil, append=false) @options[:redirect_stdout] = stdout @options[:redirect_stderr] = stderr @options[:redirect_append] = append end |
#tag(string) ⇒ Object
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.
726 727 728 |
# File 'lib/puma/dsl.rb', line 726 def tag(string) @options[:tag] = string.to_s end |
#threads(min, max) ⇒ Object
Configure min
to be the minimum number of threads to use to answer requests and max
the maximum.
The default is the environment variables PUMA_MIN_THREADS
/ PUMA_MAX_THREADS
(or MIN_THREADS
/ MAX_THREADS
if the PUMA_
variables aren’t set).
If these environment variables aren’t set, the default is “0, 5” in MRI or “0, 16” for other interpreters.
424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/puma/dsl.rb', line 424 def threads(min, max) min = Integer(min) max = Integer(max) if min > max raise "The minimum (#{min}) number of threads must be less than or equal to the max (#{max})" end if max < 1 raise "The maximum number of threads (#{max}) must be greater than 0" end @options[:min_threads] = min @options[:max_threads] = max end |
#wait_for_less_busy_worker(val = 0.005) ⇒ Object
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.
Only works on MRI. For all other interpreters, this setting does nothing.
812 813 814 |
# File 'lib/puma/dsl.rb', line 812 def wait_for_less_busy_worker(val=0.005) @options[:wait_for_less_busy_worker] = val.to_f end |
#worker_boot_timeout(timeout) ⇒ Object
Cluster mode only.
Change the default worker timeout for booting.
If unspecified, this defaults to the value of worker_timeout.
763 764 765 |
# File 'lib/puma/dsl.rb', line 763 def worker_boot_timeout(timeout) @options[:worker_boot_timeout] = Integer(timeout) end |
#worker_shutdown_timeout(timeout) ⇒ Object
Cluster mode only.
Set the timeout for worker shutdown.
772 773 774 |
# File 'lib/puma/dsl.rb', line 772 def worker_shutdown_timeout(timeout) @options[:worker_shutdown_timeout] = Integer(timeout) end |
#worker_timeout(timeout) ⇒ Object
Cluster mode only.
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.
742 743 744 745 746 747 748 749 750 751 |
# File 'lib/puma/dsl.rb', line 742 def worker_timeout(timeout) timeout = Integer(timeout) min = Const::WORKER_CHECK_INTERVAL if timeout <= min raise "The minimum worker_timeout must be greater than the worker reporting interval (#{min})" end @options[:worker_timeout] = timeout end |
#workers(count) ⇒ Object
Cluster mode only.
How many worker processes to run. Typically this is set to the number of available cores.
The default is the value of the environment variable WEB_CONCURRENCY
if set, otherwise 0.
488 489 490 |
# File 'lib/puma/dsl.rb', line 488 def workers(count) @options[:workers] = count.to_i end |