Class: Puma::DSL

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/puma/dsl.rb


The methods that are available for use inside the config 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

puts config.options[:binds]

Used to load file:

$ cat puma_config.rb
  port 3002

config = Configuration.new(config_file: "puma_config.rb")

puts config.options[:binds]
# => "tcp://"

Detailed docs can be found in `examples/config.rb`

Constant Summary

ConfigDefault - Included

DefaultRackup, DefaultTCPHost, DefaultTCPPort, DefaultWorkerShutdownTimeout, DefaultWorkerTimeout

Class Method Summary

Instance Method Summary

Constructor Details

.new(options, config) ⇒ DSL

[ GitHub ]

# File 'lib/puma/dsl.rb', line 33

def initialize(options, config)
  @config  = config
  @options = options

  @plugins = []

Instance Method Details


[ GitHub ]

# File 'lib/puma/dsl.rb', line 40

def _load_from(path)
  if path
    @path = path
    instance_eval(File.read(path), path, 1)


[ GitHub ]

# File 'lib/puma/dsl.rb', line 49

def _offer_plugins
  @plugins.each do |o|
    if o.respond_to? :config
      o.config self


#activate_control_app(url = "auto", opts = {})

Start the ::Puma control rack app on url. This app can be communicated with to control the main server.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 96

def activate_control_app(url="auto", opts={})
  if url == "auto"
    path = Configuration.temp_path
    @options[:control_url] = "unix://#{path}"
    @options[:control_url_temp] = path
    @options[:control_url] = url

  if opts[:no_token]
    auth_token = :none
    auth_token = opts[:auth_token]
    auth_token ||= Configuration.random_token

  @options[:control_auth_token] = auth_token
  @options[:control_url_umask] = opts[:umask] if opts[:umask]


Alias for #after_worker_fork.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 376

alias_method :after_worker_boot, :after_worker_fork

#after_worker_fork(&block) Also known as: #after_worker_boot

*Cluster mode only* Code to run in the master after it starts a worker.

This can be called multiple times to add hooks.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 371

def after_worker_fork(&block)
  @options[:after_worker_fork] ||= []
  @options[:after_worker_fork] = block

#app(obj = nil, &block)

Use obj or block as the Rack app. This allows a config file to be the app itself.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 85

def app(obj=nil, &block)
  obj ||= block

  raise "Provide either a #call'able or a block" unless obj

  @options[:app] = obj


*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 hooks.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 329

def before_fork(&block)
  @options[:before_fork] ||= []
  @options[:before_fork] << block



multiple urls can be bound to, calling `bind` does not overwrite previous bindings.

Adds a binding for the server to url. tcp://, unix://, and ssl:// are the only accepted protocols. Use query parameters within the url to specify options.


Explicitly the socket backlog depth (default is 1024)


Set up ssl cert


Prefer low-latency over higher throughput (via `Socket::TCP_NODELAY`)


Set socket permissions

[ GitHub ]

# File 'lib/puma/dsl.rb', line 139

def bind(url)
  @options[:binds] ||= []
  @options[:binds] << url

#clean_thread_locals(which = true)

Work around leaky apps that leave garbage in Thread locals across requests

[ GitHub ]

# File 'lib/puma/dsl.rb', line 171

def clean_thread_locals(which=true)
  @options[:clean_thread_locals] = which


[ GitHub ]

# File 'lib/puma/dsl.rb', line 144

def clear_binds!
  @options[:binds] = []

#daemonize(which = true)

Daemonize the server into the background. Highly suggest that this be combined with #pidfile and #stdout_redirect.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 177

def daemonize(which=true)
  @options[:daemon] = which


Show debugging info

[ GitHub ]

# File 'lib/puma/dsl.rb', line 251

def debug
  @options[:debug] = true


[ GitHub ]

# File 'lib/puma/dsl.rb', line 64

def default_host
  @options[:default_host] || Configuration::DefaultTCPHost


The directory to operate out of.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 379

def directory(dir)
  @options[:directory] = dir.to_s

#drain_on_shutdown(which = true)

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.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 185

def drain_on_shutdown(which=true)
  @options[:drain_on_shutdown] = which

#early_hints(answer = true)

[ GitHub ]

# File 'lib/puma/dsl.rb', line 267

def early_hints(answer=true)
  @options[:early_hints] = answer


Set the environment in which the Rack's app will run.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 190

def environment(environment)
  @options[:environment] = environment


Define how long the tcp socket stays open, if no data has been received

[ GitHub ]

# File 'lib/puma/dsl.rb', line 164

def first_data_timeout(seconds)
  @options[:first_data_timeout] = Integer(seconds)

#force_shutdown_after(val = :forever)

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.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 201

def force_shutdown_after(val=:forever)
  i = case val
      when :forever
      when :immediately

  @options[:force_shutdown_after] = i

#get(key, default = nil)

[ GitHub ]

# File 'lib/puma/dsl.rb', line 72

def get(key,default=nil)
  @options[key.to_sym] || default


[ GitHub ]

# File 'lib/puma/dsl.rb', line 68

def inject(&blk)


Load additional configuration from a file Files get loaded later via Configuration#load

[ GitHub ]

# File 'lib/puma/dsl.rb', line 118

def load(file)
  @options[:config_files] ||= []
  @options[:config_files] << file

#log_requests(which = true)

Enable request logging

[ GitHub ]

# File 'lib/puma/dsl.rb', line 245

def log_requests(which=true)
  @options[:log_requests] = which

#lowlevel_error_handler(obj = nil, &block)

Use obj or block as the low level error handler. This allows a config file to change the default error on the server.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 405

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


Code to run before doing a restart. This code should close logfiles, database connections, etc.

This can be called multiple times to add code each time.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 219

def on_restart(&block)
  @options[:on_restart] ||= []
  @options[:on_restart] << block


*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 hooks.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 339

def on_worker_boot(&block)
  @options[:before_worker_boot] ||= []
  @options[:before_worker_boot] << block


*Cluster mode only* Code to run in the master when it is about to create the worker by forking itself.

This can be called multiple times to add hooks.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 361

def on_worker_fork(&block)
  @options[:before_worker_fork] ||= []
  @options[:before_worker_fork] << block


*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 hooks.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 351

def on_worker_shutdown(&block)
  @options[:before_worker_shutdown] ||= []
  @options[:before_worker_shutdown] << block


Define how long persistent connections can be idle before puma closes them

[ GitHub ]

# File 'lib/puma/dsl.rb', line 158

def persistent_timeout(seconds)
  @options[:persistent_timeout] = Integer(seconds)


Store the pid of the server in the file at path.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 233

def pidfile(path)
  @options[:pidfile] = path.to_s


Load the named plugin for use by this configuration

[ GitHub ]

# File 'lib/puma/dsl.rb', line 78

def plugin(name)
  @plugins << @config.load_plugin(name)

#port(port, host = nil)

Define the TCP port to bind to. Use #bind for more advanced options.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 150

def port(port, host=nil)
  host ||= default_host
  bind "tcp://#{host}:#{port}"

#preload_app!(answer = true)

*Cluster mode only* Preload the application before starting the workers and setting up the listen ports. This conflicts with using the phased restart feature, you can't use both.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 398

def preload_app!(answer=true)
  @options[:preload_app] = answer

#prune_bundler(answer = true)

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.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 423

def prune_bundler(answer=true)
  @options[:prune_bundler] = answer

#queue_requests(answer = true)

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.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 463

def queue_requests(answer=true)
  @options[:queue_requests] = answer

#quiet(which = true)

Disable request logging.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 239

def quiet(which=true)
  @options[:log_requests] = !which


Load path as a rackup file.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 257

def rackup(path)
  @options[:rackup] = path.to_s


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.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 228

def restart_command(cmd)
  @options[:restart_cmd] = cmd.to_s


[ GitHub ]

# File 'lib/puma/dsl.rb', line 60

def set_default_host(host)
  @options[:default_host] = host

#set_remote_address(val = :socket)

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 “”

  • 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.
[ GitHub ]

# File 'lib/puma/dsl.rb', line 493

def set_remote_address(val=:socket)
  case val
  when :socket
    @options[:remote_address] = val
  when :localhost
    @options[:remote_address] = :value
    @options[:remote_address_value] = "".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("-", "_")
      raise "Invalid value for set_remote_address - #{val.inspect}"
    raise "Invalid value for set_remote_address - #{val}"

#shutdown_debug(val = true)

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.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 470

def shutdown_debug(val=true)
  @options[:shutdown_debug] = val

#ssl_bind(host, port, opts)

[ GitHub ]

# File 'lib/puma/dsl.rb', line 296

def ssl_bind(host, port, opts)
  verify = opts.fetch(:verify_mode, 'none')

  if defined?(JRUBY_VERSION)
    keystore_additions = "keystore=#{opts[:keystore]}&keystore-pass=#{opts[:keystore_pass]}"
    bind "ssl://#{host}:#{port}?cert=#{opts[:cert]}&key=#{opts[:key]}&#{keystore_additions}&verify_mode=#{verify}"
    bind "ssl://#{host}:#{port}?cert=#{opts[:cert]}&key=#{opts[:key]}&verify_mode=#{verify}"


Use path as the file to store the server info state. This is used by pumactl to query and control the server.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 310

def state_path(path)
  @options[:state] = path.to_s

#stdout_redirect(stdout = nil, stderr = nil, append = false)

Redirect STDOUT and STDERR to files specified.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 272

def stdout_redirect(stdout=nil, stderr=nil, append=false)
  @options[:redirect_stdout] = stdout
  @options[:redirect_stderr] = stderr
  @options[:redirect_append] = append


Additional text to display in process listing

[ GitHub ]

# File 'lib/puma/dsl.rb', line 428

def tag(string)
  @options[:tag] = string.to_s


Run the app as a raw TCP app instead of an HTTP rack app

[ GitHub ]

# File 'lib/puma/dsl.rb', line 390

def tcp_mode
  @options[:mode] = :tcp


Run Puma in TCP mode

[ GitHub ]

# File 'lib/puma/dsl.rb', line 263

def tcp_mode!
  @options[:mode] = :tcp

#threads(min, max)

Configure min to be the minimum number of threads to use to answer requests and max the maximum.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 281

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})"

  if max < 1
    raise "The maximum number of threads (#{max}) must be greater than 0"

  @options[:min_threads] = min
  @options[:max_threads] = max


*Cluster mode only* Set the timeout for workers to boot

[ GitHub ]

# File 'lib/puma/dsl.rb', line 441

def worker_boot_timeout(timeout)
  @options[:worker_boot_timeout] = Integer(timeout)


DEPRECATED: The directory to operate out of.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 384

def worker_directory(dir)
  $stderr.puts "worker_directory is deprecated. Please use `directory`"
  directory dir


*Cluster mode only* Set the timeout for worker shutdown

[ GitHub ]

# File 'lib/puma/dsl.rb', line 446

def worker_shutdown_timeout(timeout)
  @options[:worker_shutdown_timeout] = Integer(timeout)


*Cluster mode only* Set the timeout for workers in seconds When set the master process will terminate any workers that have not checked in within the given timeout. This mitigates hung processes. Default value is 60 seconds.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 436

def worker_timeout(timeout)
  @options[:worker_timeout] = Integer(timeout)


*Cluster mode only* How many worker processes to run.

[ GitHub ]

# File 'lib/puma/dsl.rb', line 316

def workers(count)
  @options[:workers] = count.to_i