Skip to content

Commit

Permalink
Merge branch 'master' into log_formatter_config
Browse files Browse the repository at this point in the history
# Conflicts:
#       History.md
  • Loading branch information
ylecuyer committed Jul 28, 2019
2 parents 82274d6 + 9f4edf4 commit 267d6c7
Show file tree
Hide file tree
Showing 35 changed files with 408 additions and 285 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -9,6 +9,7 @@ gem "nio4r", "~> 2.0"
gem "rack", "< 3.0"
gem "minitest", "~> 5.11"
gem "minitest-retry"
gem "minitest-proveit"

gem "jruby-openssl", :platform => "jruby"

Expand Down
23 changes: 18 additions & 5 deletions History.md
@@ -1,12 +1,16 @@
## Master

x features
* Features
* Add log_formatter configuration (#1816)

* Bugfixes
* Your bugfix goes here (#Github Number)

## 4.0.1 / 2019-07-11

* 2 bugfixes
* Socket removed after reload (#1829)
* Add extconf tests for DTLS_method & TLS_server_method, use in minissl.rb. (#1832)
* 1 feature
* Add log_formatter configuration
* Fix socket removed after reload - should fix problems with systemd socket activation. (#1829)
* Add extconf tests for DTLS_method & TLS_server_method, use in minissl.rb. Should fix "undefined symbol: DTLS_method" when compiling against old OpenSSL versions. (#1832)


## 4.0.0 / 2019-06-25
Expand Down Expand Up @@ -1439,3 +1443,12 @@ be added back in a future date when a java Puma::MiniSSL is added.
## 1.0.0 / 2012-03-29

* Released!

## Ignore - this is for maintainers to copy-paste during release
## Master

* Features
* Your feature goes here (#Github Number)

* Bugfixes
* Your bugfix goes here (#Github Number)
70 changes: 45 additions & 25 deletions README.md
Expand Up @@ -10,36 +10,38 @@
[![Code Climate](https://codeclimate.com/github/puma/puma.svg)](https://codeclimate.com/github/puma/puma)
[![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=puma&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=puma&package-manager=bundler&version-scheme=semver)

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

## Built For Speed &amp; 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 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 using a thread pool. Each request is served in a separate thread, so truly concurrent Ruby implementations (JRuby, Rubinius) will use all available CPU cores.

Puma was designed to be the go-to server for [Rubinius](https://rubini.us), 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.
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 IO waiting to be done in parallel.

## Quick Start

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

Without arguments, puma will look for a rackup (.ru) file in the current working directory called `config.ru`.

## Frameworks

### Rails

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

Then start your server with the `rails` command:
Start your server with the `rails` command:

```
$ rails s
$ rails server
```

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

```
$ bundle exec puma
Expand All @@ -53,7 +55,7 @@ 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:
Or you can configure your Sinatra application to always use Puma:

```ruby
require 'sinatra'
Expand All @@ -72,9 +74,9 @@ Puma uses a thread pool. You can set the minimum and maximum number of threads t
$ 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).
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 cause contention for the Global VM Lock, when using MRI).

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

Expand All @@ -84,9 +86,9 @@ Puma also offers "clustered mode". Clustered mode `fork`s workers from a master
$ 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.
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, with 16 in each worker process.

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](https://en.wikipedia.org/wiki/Copy-on-write) (Ruby 2.0+ only). Use the `--preload` flag from the command line:
In clustered mode, Puma can "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](https://en.wikipedia.org/wiki/Copy-on-write) (Ruby 2.0+ only). Use the `--preload` flag from the command line:

```
$ puma -w 3 --preload
Expand All @@ -111,8 +113,7 @@ 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.
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:

Expand All @@ -125,7 +126,7 @@ on_worker_boot do
end
```

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

```ruby
# config/puma.rb
Expand All @@ -138,13 +139,13 @@ Preloading can’t be used with phased restart, since phased restart kills and r

### 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:
Bind Puma to a socket 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)?
To use a UNIX Socket instead of TCP:

```
$ puma -b unix:///var/run/puma.sock
Expand All @@ -157,13 +158,14 @@ $ 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.
To use or avoid specific SSL cipher suites, use `ssl_cipher_filter` or `ssl_cipher_list` options.

##### Ruby:

Expand All @@ -179,21 +181,21 @@ $ puma -b 'ssl://127.0.0.1:9292?keystore=path_to_keystore&keystore-pass=keystore

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 ?
Disable TLS v1 with the `no_tlsv1` option:

```
$ 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 has a built-in status and control app that can be used to query and control Puma.

```
$ 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](https://github.com/puma/puma/blob/master/lib/puma/app/status.rb) to see what the app has available.
Puma will start the control server on localhost port 9293. All requests to the control server will need to include control token (in this case, `token=foo`) as a query parameter. This allows for simple authentication. Check out [status.rb](https://github.com/puma/puma/blob/master/lib/puma/app/status.rb) to see what the status app has available.

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

Expand All @@ -205,13 +207,13 @@ 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:
You can also provide a configuration file 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 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, Puma looks for configuration at `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:

Expand All @@ -236,7 +238,7 @@ Puma responds to several signals. A detailed guide to using UNIX signals with Pu
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).
* **Windows**: Cluster mode is not supported due to a lack of fork(2).

## Known Bugs

Expand Down Expand Up @@ -278,6 +280,24 @@ $ bundle install
$ bundle exec rake
```

To run a single test file, use the `TEST` environment variable:

```bash
$ TEST=test/test_binder.rb bundle exec rake test
```

Or use [`m`](https://github.com/qrush/m):

```
$ bundle exec m test/test_binder.rb
```

Which can also be used to run a single test case:

```
$ bundle exec m test/test_binder.rb:37
```

## License

Puma is copyright Evan Phoenix and contributors, licensed under the BSD 3-Clause license. See the included LICENSE file for details.
15 changes: 7 additions & 8 deletions Rakefile
Expand Up @@ -7,14 +7,12 @@ require_relative 'lib/puma/detect'
require 'rubygems/package_task'
require 'bundler/gem_tasks'

gemspec = Gem::Specification.load(Dir['*.gemspec'].first)
gemspec = Gem::Specification.load("puma.gemspec")
Gem::PackageTask.new(gemspec).define

# Add rubocop task
RuboCop::RakeTask.new

spec = Gem::Specification.load("puma.gemspec")

# generate extension code using Ragel (C and Java)
desc "Generate extension code (C and Java) using Ragel"
task :ragel
Expand All @@ -40,16 +38,16 @@ task :ragel => ['ext/puma_http11/org/jruby/puma/Http11Parser.java']
if !Puma.jruby?
# compile extensions using rake-compiler
# C (MRI, Rubinius)
Rake::ExtensionTask.new("puma_http11", spec) do |ext|
Rake::ExtensionTask.new("puma_http11", gemspec) do |ext|
# place extension inside namespace
ext.lib_dir = "lib/puma"

CLEAN.include "lib/puma/{1.8,1.9}"
CLEAN.include "lib/puma/puma_http11.rb"
end
CLEAN.include "lib/puma/{1.8,1.9}"
CLEAN.include "lib/puma/puma_http11.rb"
end
else
# Java (JRuby)
Rake::JavaExtensionTask.new("puma_http11", spec) do |ext|
Rake::JavaExtensionTask.new("puma_http11", gemspec) do |ext|
ext.lib_dir = "lib/puma"
end
end
Expand All @@ -75,6 +73,7 @@ end

namespace :test do
desc "Run the integration tests"

task :integration do
sh "ruby test/shell/run.rb"
end
Expand Down
2 changes: 2 additions & 0 deletions lib/puma.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Standard libraries
require 'socket'
require 'tempfile'
Expand Down
2 changes: 2 additions & 0 deletions lib/puma/accept_nonblock.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'openssl'

module OpenSSL
Expand Down
2 changes: 2 additions & 0 deletions lib/puma/app/status.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'json'

module Puma
Expand Down
4 changes: 2 additions & 2 deletions lib/puma/configuration.rb
Expand Up @@ -20,7 +20,7 @@ module ConfigDefault
# In this class any "user" specified options take precedence over any
# "file" specified options, take precedence over any "default" options.
#
# User input is prefered over "defaults":
# User input is preferred over "defaults":
# user_options = { foo: "bar" }
# default_options = { foo: "zoo" }
# options = UserFileDefaultOptions.new(user_options, default_options)
Expand All @@ -32,7 +32,7 @@ module ConfigDefault
# puts options.all_of(:foo)
# # => ["bar", "zoo"]
#
# A "file" option can be set. This config will be prefered over "default" options
# A "file" option can be set. This config will be preferred over "default" options
# but will defer to any available "user" specified options.
#
# user_options = { foo: "bar" }
Expand Down
5 changes: 4 additions & 1 deletion lib/puma/const.rb
Expand Up @@ -100,7 +100,7 @@ class UnsupportedOption < RuntimeError
# too taxing on performance.
module Const

PUMA_VERSION = VERSION = "4.0.0".freeze
PUMA_VERSION = VERSION = "4.0.1".freeze
CODE_NAME = "4 Fast 4 Furious".freeze
PUMA_SERVER_STRING = ['puma', PUMA_VERSION, CODE_NAME].join(' ').freeze

Expand Down Expand Up @@ -160,6 +160,9 @@ module Const
LINE_END = "\r\n".freeze
REMOTE_ADDR = "REMOTE_ADDR".freeze
HTTP_X_FORWARDED_FOR = "HTTP_X_FORWARDED_FOR".freeze
HTTP_X_FORWARDED_SSL = "HTTP_X_FORWARDED_SSL".freeze
HTTP_X_FORWARDED_SCHEME = "HTTP_X_FORWARDED_SCHEME".freeze
HTTP_X_FORWARDED_PROTO = "HTTP_X_FORWARDED_PROTO".freeze

SERVER_NAME = "SERVER_NAME".freeze
SERVER_PORT = "SERVER_PORT".freeze
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/dsl.rb
Expand Up @@ -480,7 +480,7 @@ def worker_timeout(timeout)
raise "The minimum worker_timeout must be greater than the worker reporting interval (#{min})"
end

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

# *Cluster mode only* Set the timeout for workers to boot
Expand Down
9 changes: 6 additions & 3 deletions lib/puma/events.rb
Expand Up @@ -39,8 +39,8 @@ def call(str)
#
def initialize(stdout, stderr)
@formatter = DefaultFormatter.new
@stdout = stdout
@stderr = stderr
@stdout = stdout.dup
@stderr = stderr.dup

@stdout.sync = true
@stderr.sync = true
Expand Down Expand Up @@ -103,7 +103,10 @@ def format(str)
# parsing exception.
#
def parse_error(server, env, error)
@stderr.puts "#{Time.now}: HTTP parse error, malformed request (#{env[HTTP_X_FORWARDED_FOR] || env[REMOTE_ADDR]}): #{error.inspect}\n---\n"
@stderr.puts "#{Time.now}: HTTP parse error, malformed request " \
"(#{env[HTTP_X_FORWARDED_FOR] || env[REMOTE_ADDR]}#{env[REQUEST_PATH]}): " \
"#{error.inspect}" \
"\n---\n"
end

# An SSL error has occurred.
Expand Down
2 changes: 2 additions & 0 deletions lib/puma/plugin/tmp_restart.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'puma/plugin'

Puma::Plugin.create do
Expand Down
2 changes: 2 additions & 0 deletions lib/puma/rack/builder.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Puma
end

Expand Down
2 changes: 2 additions & 0 deletions lib/puma/rack/urlmap.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Puma::Rack
# Rack::URLMap takes a hash mapping urls or paths to apps, and
# dispatches accordingly. Support for HTTP/1.1 host names exists if
Expand Down

0 comments on commit 267d6c7

Please sign in to comment.