class Puma::Server

The HTTP Server itself. Serves out a single Rack app.

This class is used by the `Puma::Single` and `Puma::Cluster` classes to generate one or more `Puma::Server` instances capable of handling requests. Each Puma process will contain one `Puma::Server` instance.

The `Puma::Server` instance pulls requests from the socket, adds them to a `Puma::Reactor` where they get eventually passed to a `Puma::ThreadPool`.

Each `Puma::Server` will have one reactor and one thread pool.

Constants

THREAD_LOCAL_KEY

Attributes

app[RW]
auto_trim_time[R]

@todo the following may be deprecated in the future

binder[RW]
early_hints[R]

@todo the following may be deprecated in the future

events[R]
first_data_timeout[R]

@todo the following may be deprecated in the future

leak_stack_on_error[R]

@todo the following may be deprecated in the future

log_writer[R]
max_threads[R]
min_threads[R]
persistent_timeout[R]

@todo the following may be deprecated in the future

reaping_time[R]

@todo the following may be deprecated in the future

requests_count[R]
thread[R]

Public Class Methods

current() click to toggle source

@!attribute [r] current

# File lib/puma/server.rb, line 132
def current
  Thread.current[THREAD_LOCAL_KEY]
end
new(app, events = nil, options = {}) click to toggle source

Create a server for the rack app app.

log_writer is a Puma::LogWriter object used to log info and error messages.

events is a Puma::Events object used to notify application status events.

Server#run returns a thread that you can join on to wait for the server to do its work.

@note Several instance variables exist so they are available for testing,

and have default values set via +fetch+.  Normally the values are set via
`::Puma::Configuration.puma_default_options`.

@note The `events` parameter is set to nil, and set to `Events.new` in code.

Often `options` needs to be passed, but `events` does not.  Using nil allows
calling code to not require events.rb.
# File lib/puma/server.rb, line 68
def initialize(app, events = nil, options = {})
  @app = app
  @events = events || Events.new

  @check, @notify = nil
  @status = :stop

  @thread = nil
  @thread_pool = nil

  @options = if options.is_a?(UserFileDefaultOptions)
    options
  else
    UserFileDefaultOptions.new(options, Configuration::DEFAULTS)
  end

  @clustered                 = (@options.fetch :workers, 0) > 0
  @worker_write              = @options[:worker_write]
  @log_writer                = @options.fetch :log_writer, LogWriter.stdio
  @early_hints               = @options[:early_hints]
  @first_data_timeout        = @options[:first_data_timeout]
  @persistent_timeout        = @options[:persistent_timeout]
  @idle_timeout              = @options[:idle_timeout]
  @min_threads               = @options[:min_threads]
  @max_threads               = @options[:max_threads]
  @queue_requests            = @options[:queue_requests]
  @max_fast_inline           = @options[:max_fast_inline]
  @io_selector_backend       = @options[:io_selector_backend]
  @http_content_length_limit = @options[:http_content_length_limit]

  # make this a hash, since we prefer `key?` over `include?`
  @supported_http_methods =
    if @options[:supported_http_methods] == :any
      :any
    else
      if (ary = @options[:supported_http_methods])
        ary
      else
        SUPPORTED_HTTP_METHODS
      end.sort.product([nil]).to_h.freeze
    end

  temp = !!(@options[:environment] =~ /\A(development|test)\z/)
  @leak_stack_on_error = @options[:environment] ? temp : true

  @binder = Binder.new(log_writer)

  ENV['RACK_ENV'] ||= "development"

  @mode = :http

  @precheck_closing = true

  @requests_count = 0

  @idle_timeout_reached = false
end

Public Instance Methods

inherit_binder(bind) click to toggle source
# File lib/puma/server.rb, line 126
def inherit_binder(bind)
  @binder = bind
end