puma 2.16.0 → 3.11.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. checksums.yaml +5 -5
  2. data/{History.txt → History.md} +489 -70
  3. data/README.md +143 -174
  4. data/docs/architecture.md +36 -0
  5. data/{DEPLOYMENT.md → docs/deployment.md} +1 -1
  6. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  7. data/docs/images/puma-connection-flow.png +0 -0
  8. data/docs/images/puma-general-arch.png +0 -0
  9. data/docs/nginx.md +2 -2
  10. data/docs/plugins.md +28 -0
  11. data/docs/restart.md +39 -0
  12. data/docs/signals.md +56 -3
  13. data/docs/systemd.md +272 -0
  14. data/ext/puma_http11/extconf.rb +2 -0
  15. data/ext/puma_http11/http11_parser.c +291 -447
  16. data/ext/puma_http11/http11_parser.h +1 -0
  17. data/ext/puma_http11/http11_parser.java.rl +5 -5
  18. data/ext/puma_http11/http11_parser.rl +10 -9
  19. data/ext/puma_http11/http11_parser_common.rl +1 -1
  20. data/ext/puma_http11/io_buffer.c +8 -8
  21. data/ext/puma_http11/mini_ssl.c +64 -6
  22. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +113 -131
  23. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +9 -2
  24. data/ext/puma_http11/puma_http11.c +1 -0
  25. data/lib/puma/app/status.rb +9 -1
  26. data/lib/puma/binder.rb +90 -38
  27. data/lib/puma/cli.rb +134 -491
  28. data/lib/puma/client.rb +142 -4
  29. data/lib/puma/cluster.rb +132 -76
  30. data/lib/puma/commonlogger.rb +19 -20
  31. data/lib/puma/compat.rb +3 -7
  32. data/lib/puma/configuration.rb +206 -67
  33. data/lib/puma/const.rb +21 -31
  34. data/lib/puma/control_cli.rb +92 -103
  35. data/lib/puma/convenient.rb +23 -0
  36. data/lib/puma/daemon_ext.rb +6 -0
  37. data/lib/puma/detect.rb +10 -1
  38. data/lib/puma/dsl.rb +203 -45
  39. data/lib/puma/events.rb +22 -13
  40. data/lib/puma/io_buffer.rb +1 -1
  41. data/lib/puma/jruby_restart.rb +1 -2
  42. data/lib/puma/launcher.rb +431 -0
  43. data/lib/puma/minissl.rb +83 -4
  44. data/lib/puma/null_io.rb +19 -11
  45. data/lib/puma/plugin/tmp_restart.rb +34 -0
  46. data/lib/puma/plugin.rb +115 -0
  47. data/lib/puma/rack/backports/uri/common_193.rb +17 -13
  48. data/lib/puma/rack/builder.rb +3 -0
  49. data/lib/puma/rack/urlmap.rb +9 -8
  50. data/lib/puma/reactor.rb +18 -0
  51. data/lib/puma/runner.rb +43 -15
  52. data/lib/puma/server.rb +141 -35
  53. data/lib/puma/single.rb +16 -6
  54. data/lib/puma/state_file.rb +29 -0
  55. data/lib/puma/tcp_logger.rb +8 -1
  56. data/lib/puma/thread_pool.rb +60 -10
  57. data/lib/puma/util.rb +1 -5
  58. data/lib/puma.rb +13 -4
  59. data/lib/rack/handler/puma.rb +76 -29
  60. data/tools/jungle/README.md +12 -2
  61. data/tools/jungle/init.d/README.md +9 -2
  62. data/tools/jungle/init.d/puma +86 -59
  63. data/tools/jungle/init.d/run-puma +16 -1
  64. data/tools/jungle/rc.d/README.md +74 -0
  65. data/tools/jungle/rc.d/puma +61 -0
  66. data/tools/jungle/rc.d/puma.conf +10 -0
  67. data/tools/jungle/upstart/puma.conf +1 -1
  68. data/tools/trickletest.rb +1 -1
  69. metadata +28 -95
  70. data/COPYING +0 -55
  71. data/Gemfile +0 -13
  72. data/Manifest.txt +0 -74
  73. data/Rakefile +0 -158
  74. data/docs/config.md +0 -0
  75. data/lib/puma/capistrano.rb +0 -94
  76. data/lib/puma/rack/backports/uri/common_18.rb +0 -56
  77. data/lib/puma/rack/backports/uri/common_192.rb +0 -52
  78. data/puma.gemspec +0 -52
data/README.md CHANGED
@@ -1,274 +1,243 @@
1
- # Puma: A Ruby Web Server Built For Concurrency
1
+ <p align="center">
2
+ <img src="http://puma.io/images/logos/puma-logo-large.png">
3
+ </p>
2
4
 
3
- [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/puma/puma?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) [![Build Status](https://secure.travis-ci.org/puma/puma.png)](http://travis-ci.org/puma/puma) [![Dependency Status](https://gemnasium.com/puma/puma.png)](https://gemnasium.com/puma/puma) <a href="https://codeclimate.com/github/puma/puma"><img src="https://codeclimate.com/github/puma/puma.png" /></a>
5
+ # Puma: A Ruby Web Server Built For Concurrency
4
6
 
5
- ## Description
7
+ [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/puma/puma?utm\_source=badge&utm\_medium=badge&utm\_campaign=pr-badge)
8
+ [![Build Status](https://secure.travis-ci.org/puma/puma.svg)](http://travis-ci.org/puma/puma)
9
+ [![AppVeyor](https://img.shields.io/appveyor/ci/nateberkopec/puma.svg)](https://ci.appveyor.com/project/nateberkopec/puma)
10
+ [![Dependency Status](https://gemnasium.com/puma/puma.svg)](https://gemnasium.com/puma/puma)
11
+ [![Code Climate](https://codeclimate.com/github/puma/puma.svg)](https://codeclimate.com/github/puma/puma)
6
12
 
7
- Puma is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications. Puma is intended for use in both development and production environments. In order to get the best throughput, it is highly recommended that you use a Ruby implementation with real threads like Rubinius or JRuby.
13
+ Puma is a **simple, fast, threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications** in development and production.
8
14
 
9
15
  ## Built For Speed &amp; Concurrency
10
16
 
11
- Puma is a simple, fast, and highly concurrent HTTP 1.1 server for Ruby web applications. It can be used with any application that supports Rack, and is considered the replacement for Webrick and Mongrel. It was designed to be the go-to server for [Rubinius](http://rubini.us), but also works well with JRuby and MRI. Puma is intended for use in both development and production environments.
17
+ 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.
12
18
 
13
- 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 (which you can control). This allows Puma to provide real concurrency for your web application!
19
+ Puma was designed to be the go-to server for [Rubinius](http://rubini.us), but also works well with JRuby and MRI.
14
20
 
15
- With Rubinius 2.0, Puma will utilize all cores on your CPU with real threads, meaning you won't have to spawn multiple processes to increase throughput. You can expect to see a similar benefit from JRuby.
16
-
17
- On MRI, there is a Global Interpreter Lock (GIL) that ensures only one thread can be run 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 (EventMachine-based servers such as Thin turn off this ability, requiring you to use special libraries). Your mileage may vary. In order to get the best throughput, it is highly recommended that you use a Ruby implementation with real threads like [Rubinius](http://rubini.us) or [JRuby](http://jruby.org).
21
+ 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.
18
22
 
19
23
  ## Quick Start
20
24
 
21
- The easiest way to get started with Puma is to install it via RubyGems. You can do this easily:
22
-
23
- $ gem install puma
24
-
25
- Now you should have the `puma` command available in your PATH, so just do the following in the root folder of your Rack application:
26
-
27
- $ puma app.ru
28
-
29
- ## Advanced Setup
30
-
31
- ### Sinatra
32
-
33
- You can run your Sinatra application with Puma from the command line like this:
34
-
35
- $ ruby app.rb -s Puma
36
-
37
- Or you can configure your application to always use Puma:
38
-
39
- require 'sinatra'
40
- configure { set :server, :puma }
25
+ ```
26
+ $ gem install puma
27
+ $ puma <any rackup (*.ru) file>
28
+ ```
41
29
 
42
- If you use Bundler, make sure you add Puma to your Gemfile (see below).
30
+ ## Frameworks
43
31
 
44
32
  ### Rails
45
33
 
46
- First, make sure Puma is in your Gemfile:
47
-
48
- gem 'puma'
34
+ Puma is the default server for Rails, and should already be included in your Gemfile.
49
35
 
50
36
  Then start your server with the `rails` command:
51
37
 
52
- $ rails s Puma
38
+ ```
39
+ $ rails s
40
+ ```
41
+
42
+ Many configuration options are not available when using `rails s`. It is recommended that you use Puma's executable instead:
43
+
44
+ ```
45
+ $ bundle exec puma
46
+ ```
53
47
 
54
- ### Rackup
48
+ ### Sinatra
55
49
 
56
- You can pass it as an option to `rackup`:
50
+ You can run your Sinatra application with Puma from the command line like this:
57
51
 
58
- $ rackup -s Puma
52
+ ```
53
+ $ ruby app.rb -s Puma
54
+ ```
59
55
 
60
- Alternatively, you can modify your `config.ru` to choose Puma by default, by adding the following as the first line:
56
+ Or you can configure your application to always use Puma:
61
57
 
62
- #\ -s puma
58
+ ```ruby
59
+ require 'sinatra'
60
+ configure { set :server, :puma }
61
+ ```
63
62
 
64
63
  ## Configuration
65
64
 
66
- Puma provides numerous options for controlling the operation of the server. Consult `puma -h` (or `puma --help`) for a full list.
65
+ Puma provides numerous options. Consult `puma -h` (or `puma --help`) for a full list of CLI options, or see [dsl.rb](https://github.com/puma/puma/blob/master/lib/puma/dsl.rb).
67
66
 
68
67
  ### Thread Pool
69
68
 
70
- Puma utilizes a dynamic thread pool which you can modify. You can set the minimum and maximum number of threads that are available in the pool with the `-t` (or `--threads`) flag:
69
+ 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:
71
70
 
72
- $ puma -t 8:32
71
+ ```
72
+ $ puma -t 8:32
73
+ ```
73
74
 
74
- Puma will automatically scale the number of threads 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 very large number, as you may exhaust resources on the system (or hit resource limits).
75
+ 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).
75
76
 
76
- ### Clustered mode
77
+ 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.
77
78
 
78
- Puma 2 offers clustered mode, allowing you to use forked processes to handle multiple incoming requests concurrently, in addition to threads already provided. You can tune the number of workers with the `-w` (or `--workers`) flag:
79
+ ### Clustered mode
79
80
 
80
- $ puma -t 8:32 -w 3
81
+ Puma also offers "clustered mode". Clustered mode `fork`s 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:
81
82
 
82
- On a ruby implementation that offers native threads, you should tune this number to match the number of cores available.
83
- 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 be 32 threads.
83
+ ```
84
+ $ puma -t 8:32 -w 3
85
+ ```
84
86
 
85
- If you're running in Clustered Mode you can optionally choose to preload your application before starting up the workers. This is necessary in order to take advantage of the [Copy on Write](http://en.wikipedia.org/wiki/Copy-on-write) feature introduced in [MRI Ruby 2.0](https://blog.heroku.com/archives/2013/3/6/matz_highlights_ruby_2_0_at_waza). To do this simply specify the `--preload` flag in invocation:
87
+ 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.
86
88
 
87
- # CLI invocation
88
- $ puma -t 8:32 -w 3 --preload
89
+ 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:
89
90
 
90
- If you're using a configuration file, use the `preload_app!` method, and be sure to specify your config file's location with the `-C` flag:
91
+ ```
92
+ $ puma -w 3 --preload
93
+ ```
91
94
 
92
- $ puma -C config/puma.rb
95
+ If you're using a configuration file, use the `preload_app!` method:
93
96
 
94
- # config/puma.rb
95
- threads 8,32
96
- workers 3
97
- preload_app!
97
+ ```ruby
98
+ # config/puma.rb
99
+ workers 3
100
+ preload_app!
101
+ ```
98
102
 
99
103
  Additionally, you can specify a block in your configuration file that will be run on boot of each worker:
100
104
 
101
- # config/puma.rb
102
- on_worker_boot do
103
- # configuration here
104
- end
105
+ ```ruby
106
+ # config/puma.rb
107
+ on_worker_boot do
108
+ # configuration here
109
+ end
110
+ ```
105
111
 
106
112
  This code can be used to setup the process before booting the application, allowing
107
113
  you to do some Puma-specific things that you don't want to embed in your application.
108
114
  For instance, you could fire a log notification that a worker booted or send something to statsd.
109
- This can be called multiple times to add hooks.
110
-
111
- If you're preloading your application and using ActiveRecord, it's recommend you setup your connection pool here:
112
-
113
- # config/puma.rb
114
- on_worker_boot do
115
- ActiveSupport.on_load(:active_record) do
116
- ActiveRecord::Base.establish_connection
117
- end
118
- end
119
-
120
- On top of that, you can specify a block in your configuration file that will be run before workers are forked
121
-
122
- # config/puma.rb
123
- before_fork do
124
- # configuration here
125
- end
126
-
127
- This code can be used to clean up before forking to clients, allowing
128
- you to do some Puma-specific things that you don't want to embed in your application.
129
-
130
- If you're preloading your application and using ActiveRecord, it's recommend you close any connections to the database here to prevent connection leakage:
115
+ This can be called multiple times.
131
116
 
132
- # config/puma.rb
133
- before_fork do
134
- ActiveRecord::Base.connection_pool.disconnect!
135
- end
117
+ If you're preloading your application and using ActiveRecord, it's recommended that you setup your connection pool here:
136
118
 
137
- This rule applies to any connections to external services (Redis, databases, memcache, ...) that might be started automatically by the framework.
138
-
139
- When you use preload_app, your new code goes all in the master process, and is then copied in the workers (meaning it’s only compatible with cluster mode). General rule is to use preload_app when your workers die often and need fast starts. If you don’t have many workers, you probably should not use preload_app.
140
-
141
- Note that preload_app can’t be used with phased restart, since phased restart kills and restarts workers one-by-one, and preload_app is all about copying the code of master into the workers.
142
-
143
- ### Error handler for low-level errors
119
+ ```ruby
120
+ # config/puma.rb
121
+ on_worker_boot do
122
+ ActiveSupport.on_load(:active_record) do
123
+ ActiveRecord::Base.establish_connection
124
+ end
125
+ end
126
+ ```
144
127
 
145
- If puma encounters an error outside of the context of your application, it will respond with a 500 and a simple
146
- textual error message (see `lowlevel_error` in [this file](https://github.com/puma/puma/blob/master/lib/puma/server.rb)).
147
- You can specify custom behavior for this scenario. For example, you can report the error to your third-party
148
- error-tracking service (in this example, [rollbar](http://rollbar.com)):
128
+ On top of that, you can specify a block in your configuration file that will be run before workers are forked:
149
129
 
150
130
  ```ruby
151
- lowlevel_error_handler do |e|
152
- Rollbar.critical(e)
153
- [500, {}, ["An error has occurred, and engineers have been informed. Please reload the page. If you continue to have problems, contact support@example.com\n"]]
131
+ # config/puma.rb
132
+ before_fork do
133
+ # configuration here
154
134
  end
155
135
  ```
156
136
 
137
+ Preloading can’t 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.
138
+
157
139
  ### Binding TCP / Sockets
158
140
 
159
141
  In contrast to many other server configs which require multiple flags, Puma simply uses one URI parameter with the `-b` (or `--bind`) flag:
160
142
 
161
- $ puma -b tcp://127.0.0.1:9292
143
+ ```
144
+ $ puma -b tcp://127.0.0.1:9292
145
+ ```
162
146
 
163
- Want to use UNIX Sockets instead of TCP (which can provide a 5-10% performance boost)? No problem!
147
+ Want to use UNIX Sockets instead of TCP (which can provide a 5-10% performance boost)?
164
148
 
165
- $ puma -b unix:///var/run/puma.sock
149
+ ```
150
+ $ puma -b unix:///var/run/puma.sock
151
+ ```
166
152
 
167
153
  If you need to change the permissions of the UNIX socket, just add a umask parameter:
168
154
 
169
- $ puma -b 'unix:///var/run/puma.sock?umask=0111'
155
+ ```
156
+ $ puma -b 'unix:///var/run/puma.sock?umask=0111'
157
+ ```
170
158
 
171
- Need a bit of security? Use SSL sockets!
159
+ Need a bit of security? Use SSL sockets:
172
160
 
173
- $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'
161
+ ```
162
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'
163
+ ```
174
164
 
175
165
  ### Control/Status Server
176
166
 
177
- Puma comes with a builtin status/control app that can be used query and control Puma itself. Here is an example of starting Puma with the control server:
178
-
179
- $ puma --control tcp://127.0.0.1:9293 --control-token foo
180
-
181
- This directs Puma to start the control server on localhost port 9293. Additionally, 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.
182
-
183
- ### Configuration file
184
-
185
- You can also provide a configuration file which Puma will use with the `-C` (or `--config`) flag:
186
-
187
- $ puma -C /path/to/config
188
-
189
- By default, 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.
190
-
191
- 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:
192
-
193
- $ puma -C "-"
167
+ Puma has a built-in status/control app that can be used to query and control Puma itself.
194
168
 
195
- Take the following [sample configuration](https://github.com/puma/puma/blob/master/examples/config.rb) as inspiration or check out [configuration.rb](https://github.com/puma/puma/blob/master/lib/puma/configuration.rb) to see all available options.
196
-
197
- ## Restart
198
-
199
- Puma includes the ability to restart itself allowing easy upgrades to new versions. 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.
200
-
201
- To perform a restart, there are 2 builtin mechanisms:
202
-
203
- * Send the `puma` process the `SIGUSR2` signal
204
- * Use the status server and issue `/restart`
169
+ ```
170
+ $ puma --control tcp://127.0.0.1:9293 --control-token foo
171
+ ```
205
172
 
206
- No code is shared between the current and restarted process, so it should be safe to issue a restart any place where you would manually stop Puma and start it again.
173
+ 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.
207
174
 
208
- If the new process is unable to load, it will simply exit. You should therefore run Puma under a supervisor when using it in production.
175
+ You can also interact with the control server via `pumactl`. This command will restart Puma:
209
176
 
210
- ### Normal vs Hot vs Phased Restart
177
+ ```
178
+ $ pumactl -C 'tcp://127.0.0.1:9293' --control-token foo restart
179
+ ```
211
180
 
212
- A hot restart means that no requests while deploying your new code will be lost, since the server socket is kept open between restarts.
181
+ To see a list of `pumactl` options, use `pumactl --help`.
213
182
 
214
- But beware, hot restart does not mean that the incoming requests won’t hang for multiple seconds while your new code has not fully deployed. If you need a zero downtime and zero hanging requests deploy, you must use phased restart.
183
+ ### Configuration File
215
184
 
216
- When you run pumactl phased-restart, Puma kills workers one-by-one, meaning that at least another worker is still available to serve requests, which lead in zero hanging request (yay!).
185
+ You can also provide a configuration file which Puma will use with the `-C` (or `--config`) flag:
217
186
 
218
- But again beware, upgrading an application sometimes involves upgrading the database schema. With phased restart, there may be a moment during the deployment where processes belonging to the previous version and processes belonging to the new version both exist at the same time. Any database schema upgrades you perform must therefore be backwards-compatible with the old application version.
187
+ ```
188
+ $ puma -C /path/to/config
189
+ ```
219
190
 
220
- if you perform a lot of database migrations, you probably should not use phased restart and use a normal/hot restart instead (pumactl restart). That way, no code is shared while deploying (in that case, preload_app might help for quicker deployment, see below).
191
+ 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`.
221
192
 
193
+ 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:
222
194
 
223
- ### Cleanup Code
195
+ ```
196
+ $ puma -C "-"
197
+ ```
224
198
 
225
- Puma isn't able to understand all the resources that your app may use, so it provides a hook in the configuration file you pass to `-C` called `on_restart`. The block passed to `on_restart` will be called, unsurprisingly, just before Puma restarts itself.
199
+ Take the following [sample configuration](https://github.com/puma/puma/blob/master/examples/config.rb) as inspiration or check out [dsl.rb](https://github.com/puma/puma/blob/master/lib/puma/dsl.rb) to see all available options.
226
200
 
227
- You should place code to close global log files, redis connections, etc in this block so that their file descriptors don't leak into the restarted process. Failure to do so will result in slowly running out of descriptors and eventually obscure crashes as the server is restart many times.
201
+ ## Restart
228
202
 
229
- ### Platform Constraints
203
+ 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.
230
204
 
231
- Because of various platforms not being implement certain things, the following differences occur when Puma is used on different platforms:
205
+ For more, see the [restart documentation](https://github.com/puma/puma/blob/master/docs/restart.md).
232
206
 
233
- * **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
234
- * **JRuby**, **Windows**: cluster mode is not supported due to a lack of fork(2)
235
- * **Windows**: daemon mode is not supported due to a lack of fork(2)
207
+ ## Signals
236
208
 
237
- ## pumactl
209
+ Puma responds to several signals. A detailed guide to using UNIX signals with Puma can be found in the [signals documentation](https://github.com/puma/puma/blob/master/docs/signals.md).
238
210
 
239
- `pumactl` is a simple CLI frontend to the control/status app described above. Please refer to `pumactl --help` for available commands.
211
+ ## Platform Constraints
240
212
 
241
- ## Managing multiple Pumas / init.d / upstart scripts
213
+ Some platforms do not support all Puma features.
242
214
 
243
- If you want an easy way to manage multiple scripts at once check [tools/jungle](https://github.com/puma/puma/tree/master/tools/jungle) for init.d and upstart scripts.
215
+ * **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).
216
+ * **Windows**: daemon mode is not supported due to a lack of fork(2).
244
217
 
245
- ## Capistrano deployment
218
+ ## Known Bugs
246
219
 
247
- Puma has support for Capistrano3 with an [external gem](https://github.com/seuros/capistrano-puma), you just need require that in Gemfile:
220
+ For MRI versions 2.2.7, 2.2.8, 2.2.9, 2.3.4 and 2.4.1, you may see ```stream closed in another thread (IOError)```. It may be caused by a [Ruby bug](https://bugs.ruby-lang.org/issues/13632). It can be fixed with the gem https://rubygems.org/gems/stopgap_13632:
248
221
 
249
222
  ```ruby
250
- gem 'capistrano3-puma'
251
- ```
252
- And then execute:
253
-
254
- ```bash
255
- bundle
223
+ if %w(2.2.7 2.2.8 2.2.9 2.3.4 2.4.1).include? RUBY_VERSION
224
+ begin
225
+ require 'stopgap_13632'
226
+ rescue LoadError
227
+ end
228
+ end
256
229
  ```
257
230
 
258
- Then add to Capfile
231
+ ## Deployment
259
232
 
260
- ```ruby
261
- require 'capistrano/puma'
262
- ```
233
+ Puma has support for Capistrano with an [external gem](https://github.com/seuros/capistrano-puma).
263
234
 
264
- and then
235
+ It is common to use process monitors with Puma. Modern process monitors like systemd or upstart
236
+ provide continuous monitoring and restarts for increased
237
+ reliability in production environments:
265
238
 
266
- ```bash
267
- $ bundle exec cap puma:start
268
- $ bundle exec cap puma:restart
269
- $ bundle exec cap puma:stop
270
- $ bundle exec cap puma:phased-restart
271
- ```
239
+ * [tools/jungle](https://github.com/puma/puma/tree/master/tools/jungle) for sysvinit (init.d) and upstart
240
+ * [docs/systemd](https://github.com/puma/puma/blob/master/docs/systemd.md)
272
241
 
273
242
  ## Contributing
274
243
 
@@ -281,4 +250,4 @@ $ bundle exec rake
281
250
 
282
251
  ## License
283
252
 
284
- Puma is copyright 2014 Evan Phoenix and contributors. It is licensed under the BSD 3-Clause license. See the include LICENSE file for details.
253
+ Puma is copyright Evan Phoenix and contributors, licensed under the BSD 3-Clause license. See the included LICENSE file for details.
@@ -0,0 +1,36 @@
1
+ # Architecture
2
+
3
+ ## Overview
4
+
5
+ ![http://bit.ly/2iJuFky](images/puma-general-arch.png)
6
+
7
+ Puma is a threaded web server, processing requests across a TCP or UNIX socket.
8
+
9
+ Workers accept connections from the socket and a thread in the worker's thread pool processes the client's request.
10
+
11
+ Clustered mode is shown/discussed here. Single mode is analogous to having a single worker process.
12
+
13
+ ## Connection pipeline
14
+
15
+ ![http://bit.ly/2zwzhEK](images/puma-connection-flow.png)
16
+
17
+ * Upon startup, Puma listens on a TCP or UNIX socket.
18
+ * The backlog of this socket is configured (with a default of 1024), determining how many established but unaccepted connections can exist concurrently.
19
+ * This socket backlog is distinct from the "backlog" of work as reported by the control server stats. The latter is the number of connections in that worker's "todo" set waiting for a worker thread.
20
+ * By default, a single, separate thread is used to receive HTTP requests across the socket.
21
+ * When at least one worker thread is available for work, a connection is accepted and placed in this request buffer
22
+ * This thread waits for entire HTTP requests to be received over the connection
23
+ * Once received, the connection is pushed into the "todo" set
24
+ * Worker threads pop work off the "todo" set for processing
25
+ * The thread processes the request via the rack application (which generates the HTTP response)
26
+ * The thread writes the response to the connection
27
+ * Finally, the thread become available to process another connection in the "todo" set
28
+
29
+ ### Disabling `queue_requests`
30
+
31
+ ![http://bit.ly/2zxCJ1Z](images/puma-connection-flow-no-reactor.png)
32
+
33
+ The `queue_requests` option is `true` by default, enabling the separate thread used to buffer requests as described above.
34
+
35
+ If set to `false`, this buffer will not be used for connections while waiting for the request to arrive.
36
+ In this mode, when a connection is accepted, it is added to the "todo" queue immediately, and a worker will synchronously do any waiting necessary to read the HTTP request from the socket.
@@ -27,7 +27,7 @@ Here are some rules of thumb:
27
27
  * Use cluster mode and set the number of workers to 1.5x the number of cpu cores
28
28
  in the machine, minimum 2.
29
29
  * Set the number of threads to desired concurrent requests / number of workers.
30
- Puma defaults to 8 and that's a decent number.
30
+ Puma defaults to 16 and that's a decent number.
31
31
 
32
32
  #### Migrating from Unicorn
33
33
 
Binary file
data/docs/nginx.md CHANGED
@@ -34,7 +34,7 @@ server {
34
34
  proxy_set_header Host $http_host;
35
35
 
36
36
  # If the file exists as a static file serve it directly without
37
- # running all the other rewite tests on it
37
+ # running all the other rewrite tests on it
38
38
  if (-f $request_filename) {
39
39
  break;
40
40
  }
@@ -50,7 +50,7 @@ server {
50
50
  # this is the meat of the rack page caching config
51
51
  # it adds .html to the end of the url and then checks
52
52
  # the filesystem for that file. If it exists, then we
53
- # rewite the url to have explicit .html on the end
53
+ # rewrite the url to have explicit .html on the end
54
54
  # and then send it on its way to the next config rule.
55
55
  # if there is no file on the fs then it sets all the
56
56
  # necessary headers and proxies to our upstream pumas
data/docs/plugins.md ADDED
@@ -0,0 +1,28 @@
1
+ ## Plugins
2
+
3
+ Puma 3.0 added support for plugins that can augment configuration and service operations.
4
+
5
+ 2 canonical plugins to look to aid in development of further plugins:
6
+
7
+ * [tmp\_restart](https://github.com/puma/puma/blob/master/lib/puma/plugin/tmp_restart.rb): Restarts the server if the file `tmp/restart.txt` is touched
8
+ * [heroku](https://github.com/puma/puma-heroku/blob/master/lib/puma/plugin/heroku.rb): Packages up the default configuration used by puma on Heroku
9
+
10
+ Plugins are activated in a puma configuration file (such as `config/puma.rb'`) by adding `plugin "name"`, such as `plugin "heroku"`.
11
+
12
+ Plugins are activated based simply on path requirements so, activating the `heroku` plugin will simply be doing `require "puma/plugin/heroku"`. This allows gems to provide multiple plugins (as well as unrelated gems to provide puma plugins).
13
+
14
+ The `tmp_restart` plugin is bundled with puma, so it can always be used.
15
+
16
+ To use the `heroku` plugin, add `puma-heroku` to your Gemfile or install it.
17
+
18
+ ### API
19
+
20
+ At present, there are 2 hooks that plugins can use: `start` and `config`.
21
+
22
+ `start` runs when the server has started and allows the plugin to start other functionality to augment puma.
23
+
24
+ `config` runs when the server is being configured and is passed a `Puma::DSL` object that can be used to add additional configuration.
25
+
26
+ Any public methods in `Puma::Plugin` are the public API that any plugin may use.
27
+
28
+ In the future, more hooks and APIs will be added.
data/docs/restart.md ADDED
@@ -0,0 +1,39 @@
1
+ # Restarts
2
+
3
+ To perform a restart, there are 3 builtin mechanisms:
4
+
5
+ * Send the `puma` process the `SIGUSR2` signal
6
+ * Send the `puma` process the `SIGUSR1` signal (rolling restart, cluster mode only)
7
+ * Use the status server and issue `/restart`
8
+
9
+ No code is shared between the current and restarted process, so it should be safe to issue a restart any place where you would manually stop Puma and start it again.
10
+
11
+ If the new process is unable to load, it will simply exit. You should therefore run Puma under a process monitor (see below) when using it in production.
12
+
13
+ ### Normal vs Hot vs Phased Restart
14
+
15
+ A hot restart means that no requests will be lost while deploying your new code, since the server socket is kept open between restarts.
16
+
17
+ But beware, hot restart does not mean that the incoming requests won’t hang for multiple seconds while your new code has not fully deployed. If you need a zero downtime and zero hanging requests deploy, you must use phased restart.
18
+
19
+ When you run pumactl phased-restart, Puma kills workers one-by-one, meaning that at least another worker is still available to serve requests, which lead to zero hanging requests (yay!).
20
+
21
+ But again beware, upgrading an application sometimes involves upgrading the database schema. With phased restart, there may be a moment during the deployment where processes belonging to the previous version and processes belonging to the new version both exist at the same time. Any database schema upgrades you perform must therefore be backwards-compatible with the old application version.
22
+
23
+ If you perform a lot of database migrations, you probably should not use phased restart and use a normal/hot restart instead (`pumactl restart`). That way, no code is shared while deploying (in that case, `preload_app!` might help for quicker deployment, see ["Clustered Mode" in the README](../README.md#clustered-mode)).
24
+
25
+ ### Release Directory
26
+
27
+ If your symlink releases into a common working directory (i.e., `/current` from Capistrano), Puma won't pick up your new changes when running phased restarts without additional configuration. You should set your working directory within Puma's config to specify the directory it should use. This is a change from earlier versions of Puma (< 2.15) that would infer the directory for you.
28
+
29
+ ```ruby
30
+ # config/puma.rb
31
+
32
+ directory '/var/www/current'
33
+ ```
34
+
35
+ ### Cleanup Code
36
+
37
+ Puma isn't able to understand all the resources that your app may use, so it provides a hook in the configuration file you pass to `-C` called `on_restart`. The block passed to `on_restart` will be called, unsurprisingly, just before Puma restarts itself.
38
+
39
+ You should place code to close global log files, redis connections, etc. in this block so that their file descriptors don't leak into the restarted process. Failure to do so will result in slowly running out of descriptors and eventually obscure crashes as the server is restarted many times.