1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
A Ruby/Rack web server built for parallelism
Find a file
schneems 84ce04dc95 Make Launcher#close_binder_listeners public
To avoid a `send` call in `cluster.rb` and to indicate to the maintainers that other classes use this method, we will make it public.
2019-06-07 14:10:20 -05:00
.github Add puma version to issue template 2017-08-16 08:24:42 -06:00
bin Fix puma-wild handling a restart properly. Fixes #550 2014-10-29 17:07:25 -07:00
docs Merge pull request #1777 from tagliala/patch-1 2019-04-21 22:20:37 -07:00
examples Merge branch 'master' into control-exception-on-sigterm 2019-02-21 19:30:00 +01:00
ext/puma_http11 Fixes #1772. Explicit cast to Buffer to make Java 8 still find proper signature (when compiled with 9+ and specify Java 8 compat level) 2019-04-13 20:58:49 -05:00
lib Make Launcher#close_binder_listeners public 2019-06-07 14:10:20 -05:00
test [close #1802] Close listeners on SIGTERM 2019-05-30 15:16:51 -05:00
tools Fix checking pid existence condition branches 2018-09-18 15:18:57 +02:00
win_gem_test Appveyor - Create pre-compiled Windows gems and test 2018-09-11 16:31:54 -05:00
.gitattributes Stop the CRLF warnings 2019-01-24 21:51:55 -08:00
.gitignore Appveyor - Create pre-compiled Windows gems and test 2018-09-11 16:31:54 -05:00
.rubocop.yml Update dev files - .travis.yml, .rubocop.yml, Gemfile 2018-09-11 16:31:54 -05:00
.rubocop_todo.yml Use Rubocop 0.50; fix SpaceBeforeBlockBraces layout (#1472) 2017-11-30 08:52:40 -07:00
.travis.yml travis.yml - xcode10.2 & osx matrix update (#1783) 2019-04-24 17:17:58 +09:00
appveyor.yml Appveyor - Create pre-compiled Windows gems and test 2018-09-11 16:31:54 -05:00
Gemfile Switch IO reactor to nio4r 2019-02-19 16:38:21 -08:00
History.md Add history for commits since v3.12.1 2019-03-19 17:17:55 -05:00
LICENSE Update license 2011-09-23 20:27:54 -07:00
puma.gemspec Switch IO reactor to nio4r 2019-02-19 16:38:21 -08:00
Rakefile Friendlier dev setup 2019-01-06 15:53:20 +01:00
README.md README.md - remove missed http, Appveyor update [skip ci] (#1781) 2019-04-24 17:15:45 +09:00
Release.md v 3.11.4 2018-04-12 14:38:52 -05:00

Puma: A Ruby Web Server Built For Concurrency

Gitter Travis Build Status Appveyor Build Status Code Climate SemVer

Puma is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications in development and production.

Built For Speed & Concurrency

Under the hood, Puma processes requests using a C-optimized Ragel extension (inherited from Mongrel) that provides fast, accurate HTTP 1.1 protocol parsing in a portable way. Puma then serves the request in a thread from an internal thread pool. Since each request is served in a separate thread, truly concurrent Ruby implementations (JRuby, Rubinius) will use all available CPU cores.

Puma was designed to be the go-to server for Rubinius, but also works well with JRuby and MRI.

On MRI, there is a Global VM Lock (GVL) that ensures only one thread can run Ruby code at a time. But if you're doing a lot of blocking IO (such as HTTP calls to external APIs like Twitter), Puma still improves MRI's throughput by allowing blocking IO to be run concurrently.

Quick Start

$ gem install puma
$ puma <any rackup (*.ru) file>

Frameworks

Rails

Puma is the default server for Rails, and should already be included in your Gemfile.

Then start your server with the rails command:

$ rails s

Many configuration options are not available when using rails s. It is recommended that you use Puma's executable instead:

$ bundle exec puma

Sinatra

You can run your Sinatra application with Puma from the command line like this:

$ ruby app.rb -s Puma

Or you can configure your application to always use Puma:

require 'sinatra'
configure { set :server, :puma }

Configuration

Puma provides numerous options. Consult puma -h (or puma --help) for a full list of CLI options, or see dsl.rb.

Thread Pool

Puma uses a thread pool. You can set the minimum and maximum number of threads that are available in the pool with the -t (or --threads) flag:

$ puma -t 8:32

Puma will automatically scale the number of threads, from the minimum until it caps out at the maximum, based on how much traffic is present. The current default is 0:16. Feel free to experiment, but be careful not to set the number of maximum threads to a large number, as you may exhaust resources on the system (or hit resource limits).

Be aware that additionally Puma creates threads on its own for internal purposes (e.g. handling slow clients). So even if you specify -t 1:1, expect around 7 threads created in your application.

Clustered mode

Puma also offers "clustered mode". Clustered mode forks workers from a master process. Each child process still has its own thread pool. You can tune the number of workers with the -w (or --workers) flag:

$ puma -t 8:32 -w 3

Note that threads are still used in clustered mode, and the -t thread flag setting is per worker, so -w 2 -t 16:16 will spawn 32 threads in total.

In clustered mode, Puma may "preload" your application. This loads all the application code prior to forking. Preloading reduces total memory usage of your application via an operating system feature called copy-on-write (Ruby 2.0+ only). Use the --preload flag from the command line:

$ puma -w 3 --preload

If you're using a configuration file, use the preload_app! method:

# config/puma.rb
workers 3
preload_app!

Additionally, you can specify a block in your configuration file that will be run on boot of each worker:

# config/puma.rb
on_worker_boot do
  # configuration here
end

This code can be used to setup the process before booting the application, allowing you to do some Puma-specific things that you don't want to embed in your application. For instance, you could fire a log notification that a worker booted or send something to statsd. This can be called multiple times.

If you're preloading your application and using ActiveRecord, it's recommended that you setup your connection pool here:

# config/puma.rb
on_worker_boot do
  ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
  end
end

On top of that, you can specify a block in your configuration file that will be run before workers are forked:

# config/puma.rb
before_fork do
  # configuration here
end

Preloading cant be used with phased restart, since phased restart kills and restarts workers one-by-one, and preload_app copies the code of master into the workers.

Binding TCP / Sockets

In contrast to many other server configs which require multiple flags, Puma simply uses one URI parameter with the -b (or --bind) flag:

$ puma -b tcp://127.0.0.1:9292

Want to use UNIX Sockets instead of TCP (which can provide a 5-10% performance boost)?

$ puma -b unix:///var/run/puma.sock

If you need to change the permissions of the UNIX socket, just add a umask parameter:

$ puma -b 'unix:///var/run/puma.sock?umask=0111'

Need a bit of security? Use SSL sockets:

$ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'

Controlling SSL Cipher Suites

Need to use or avoid specific SSL cipher suites? Use ssl_cipher_filter or ssl_cipher_list options.

Ruby:
$ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&ssl_cipher_filter=!aNULL:AES+SHA'
JRuby:
$ puma -b 'ssl://127.0.0.1:9292?keystore=path_to_keystore&keystore-pass=keystore_password&ssl_cipher_list=TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA'

See https://www.openssl.org/docs/man1.0.2/apps/ciphers.html for cipher filter format and full list of cipher suites.

Don't want to use insecure TLSv1.0 ?

$ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&no_tlsv1=true'

Control/Status Server

Puma has a built-in status/control app that can be used to query and control Puma itself.

$ puma --control-url tcp://127.0.0.1:9293 --control-token foo

Puma will start the control server on localhost port 9293. All requests to the control server will need to include token=foo as a query parameter. This allows for simple authentication. Check out status.rb to see what the app has available.

You can also interact with the control server via pumactl. This command will restart Puma:

$ pumactl --control-url 'tcp://127.0.0.1:9293' --control-token foo restart

To see a list of pumactl options, use pumactl --help.

Configuration File

You can also provide a configuration file which Puma will use with the -C (or --config) flag:

$ puma -C /path/to/config

If no configuration file is specified, Puma will look for a configuration file at config/puma.rb. If an environment is specified, either via the -e and --environment flags, or through the RACK_ENV environment variable, the default file location will be config/puma/environment_name.rb.

If you want to prevent Puma from looking for a configuration file in those locations, provide a dash as the argument to the -C (or --config) flag:

$ puma -C "-"

Take the following sample configuration as inspiration or check out dsl.rb to see all available options.

Restart

Puma includes the ability to restart itself. When available (MRI, Rubinius, JRuby), Puma performs a "hot restart". This is the same functionality available in Unicorn and NGINX which keep the server sockets open between restarts. This makes sure that no pending requests are dropped while the restart is taking place.

For more, see the restart documentation.

Signals

Puma responds to several signals. A detailed guide to using UNIX signals with Puma can be found in the signals documentation.

Platform Constraints

Some platforms do not support all Puma features.

  • JRuby, Windows: server sockets are not seamless on restart, they must be closed and reopened. These platforms have no way to pass descriptors into a new process that is exposed to Ruby. Also, cluster mode is not supported due to a lack of fork(2).
  • Windows: daemon mode is not supported due to a lack of fork(2).

Known Bugs

For MRI versions 2.2.7, 2.2.8, 2.2.9, 2.2.10 2.3.4 and 2.4.1, you may see stream closed in another thread (IOError). It may be caused by a Ruby bug. It can be fixed with the gem https://rubygems.org/gems/stopgap_13632:

if %w(2.2.7 2.2.8 2.2.9 2.2.10 2.3.4 2.4.1).include? RUBY_VERSION
  begin
    require 'stopgap_13632'
  rescue LoadError
  end
end

Deployment

Puma has support for Capistrano with an external gem.

It is common to use process monitors with Puma. Modern process monitors like systemd or upstart provide continuous monitoring and restarts for increased reliability in production environments:

Community Plugins

Contributing

To run the test suite:

$ bundle install
$ bundle exec rake

License

Puma is copyright Evan Phoenix and contributors, licensed under the BSD 3-Clause license. See the included LICENSE file for details.