puma 4.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of puma might be problematic. Click here for more details.

Files changed (80) hide show
  1. checksums.yaml +7 -0
  2. data/History.md +1513 -0
  3. data/LICENSE +26 -0
  4. data/README.md +309 -0
  5. data/bin/puma +10 -0
  6. data/bin/puma-wild +31 -0
  7. data/bin/pumactl +12 -0
  8. data/docs/architecture.md +37 -0
  9. data/docs/deployment.md +111 -0
  10. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  11. data/docs/images/puma-connection-flow.png +0 -0
  12. data/docs/images/puma-general-arch.png +0 -0
  13. data/docs/nginx.md +80 -0
  14. data/docs/plugins.md +28 -0
  15. data/docs/restart.md +41 -0
  16. data/docs/signals.md +96 -0
  17. data/docs/systemd.md +290 -0
  18. data/ext/puma_http11/PumaHttp11Service.java +19 -0
  19. data/ext/puma_http11/ext_help.h +15 -0
  20. data/ext/puma_http11/extconf.rb +23 -0
  21. data/ext/puma_http11/http11_parser.c +1044 -0
  22. data/ext/puma_http11/http11_parser.h +65 -0
  23. data/ext/puma_http11/http11_parser.java.rl +161 -0
  24. data/ext/puma_http11/http11_parser.rl +147 -0
  25. data/ext/puma_http11/http11_parser_common.rl +54 -0
  26. data/ext/puma_http11/io_buffer.c +155 -0
  27. data/ext/puma_http11/mini_ssl.c +553 -0
  28. data/ext/puma_http11/org/jruby/puma/Http11.java +234 -0
  29. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +470 -0
  30. data/ext/puma_http11/org/jruby/puma/IOBuffer.java +72 -0
  31. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +363 -0
  32. data/ext/puma_http11/puma_http11.c +500 -0
  33. data/lib/puma.rb +31 -0
  34. data/lib/puma/accept_nonblock.rb +29 -0
  35. data/lib/puma/app/status.rb +80 -0
  36. data/lib/puma/binder.rb +439 -0
  37. data/lib/puma/cli.rb +239 -0
  38. data/lib/puma/client.rb +494 -0
  39. data/lib/puma/cluster.rb +555 -0
  40. data/lib/puma/commonlogger.rb +108 -0
  41. data/lib/puma/configuration.rb +362 -0
  42. data/lib/puma/const.rb +235 -0
  43. data/lib/puma/control_cli.rb +281 -0
  44. data/lib/puma/convenient.rb +25 -0
  45. data/lib/puma/delegation.rb +13 -0
  46. data/lib/puma/detect.rb +15 -0
  47. data/lib/puma/dsl.rb +738 -0
  48. data/lib/puma/events.rb +156 -0
  49. data/lib/puma/io_buffer.rb +4 -0
  50. data/lib/puma/jruby_restart.rb +84 -0
  51. data/lib/puma/launcher.rb +478 -0
  52. data/lib/puma/minissl.rb +278 -0
  53. data/lib/puma/null_io.rb +44 -0
  54. data/lib/puma/plugin.rb +120 -0
  55. data/lib/puma/plugin/tmp_restart.rb +36 -0
  56. data/lib/puma/rack/builder.rb +301 -0
  57. data/lib/puma/rack/urlmap.rb +93 -0
  58. data/lib/puma/rack_default.rb +9 -0
  59. data/lib/puma/reactor.rb +399 -0
  60. data/lib/puma/runner.rb +185 -0
  61. data/lib/puma/server.rb +1033 -0
  62. data/lib/puma/single.rb +124 -0
  63. data/lib/puma/state_file.rb +31 -0
  64. data/lib/puma/tcp_logger.rb +41 -0
  65. data/lib/puma/thread_pool.rb +328 -0
  66. data/lib/puma/util.rb +124 -0
  67. data/lib/rack/handler/puma.rb +115 -0
  68. data/tools/docker/Dockerfile +16 -0
  69. data/tools/jungle/README.md +19 -0
  70. data/tools/jungle/init.d/README.md +61 -0
  71. data/tools/jungle/init.d/puma +421 -0
  72. data/tools/jungle/init.d/run-puma +18 -0
  73. data/tools/jungle/rc.d/README.md +74 -0
  74. data/tools/jungle/rc.d/puma +61 -0
  75. data/tools/jungle/rc.d/puma.conf +10 -0
  76. data/tools/jungle/upstart/README.md +61 -0
  77. data/tools/jungle/upstart/puma-manager.conf +31 -0
  78. data/tools/jungle/upstart/puma.conf +69 -0
  79. data/tools/trickletest.rb +44 -0
  80. metadata +144 -0
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Some code copyright (c) 2005, Zed Shaw
2
+ Copyright (c) 2011, Evan Phoenix
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ * Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+ * Redistributions in binary form must reproduce the above copyright notice
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+ * Neither the name of the Evan Phoenix nor the names of its contributors
14
+ may be used to endorse or promote products derived from this software
15
+ without specific prior written permission.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,309 @@
1
+ <p align="center">
2
+ <img src="https://puma.io/images/logos/puma-logo-large.png">
3
+ </p>
4
+
5
+ # Puma: A Ruby Web Server Built For Concurrency
6
+
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
+ [![Travis Build Status](https://travis-ci.org/puma/puma.svg?branch=master)](https://travis-ci.org/puma/puma)
9
+
10
+ [![Code Climate](https://codeclimate.com/github/puma/puma.svg)](https://codeclimate.com/github/puma/puma)
11
+ [![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)
12
+
13
+ Puma is a **simple, fast, multi-threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications**.
14
+
15
+ ## Built For Speed &amp; Concurrency
16
+
17
+ 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.
18
+
19
+ Puma was designed to be the go-to server for [Rubinius](https://rubinius.com), but also works well with JRuby and MRI.
20
+
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 IO waiting to be done in parallel.
22
+
23
+ ## Quick Start
24
+
25
+ ```
26
+ $ gem install puma
27
+ $ puma
28
+ ```
29
+
30
+ Without arguments, puma will look for a rackup (.ru) file in the current working directory called `config.ru`.
31
+
32
+ ## Frameworks
33
+
34
+ ### Rails
35
+
36
+ Puma is the default server for Rails, included in the generated Gemfile.
37
+
38
+ Start your server with the `rails` command:
39
+
40
+ ```
41
+ $ rails server
42
+ ```
43
+
44
+ Many configuration options and Puma features are not available when using `rails server`. It is recommended that you use Puma's executable instead:
45
+
46
+ ```
47
+ $ bundle exec puma
48
+ ```
49
+
50
+ ### Sinatra
51
+
52
+ You can run your Sinatra application with Puma from the command line like this:
53
+
54
+ ```
55
+ $ ruby app.rb -s Puma
56
+ ```
57
+
58
+ Or you can configure your Sinatra application to always use Puma:
59
+
60
+ ```ruby
61
+ require 'sinatra'
62
+ configure { set :server, :puma }
63
+ ```
64
+
65
+ ## Configuration
66
+
67
+ 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).
68
+
69
+ You can also find several configuration examples as part of the
70
+ [test](test/config) suite.
71
+
72
+ ### Thread Pool
73
+
74
+ 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:
75
+
76
+ ```
77
+ $ puma -t 8:32
78
+ ```
79
+
80
+ 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).
81
+
82
+ 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.
83
+
84
+ ### Clustered mode
85
+
86
+ 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:
87
+
88
+ ```
89
+ $ puma -t 8:32 -w 3
90
+ ```
91
+
92
+ 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.
93
+
94
+ 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:
95
+
96
+ ```
97
+ $ puma -w 3 --preload
98
+ ```
99
+
100
+ If you're using a configuration file, use the `preload_app!` method:
101
+
102
+ ```ruby
103
+ # config/puma.rb
104
+ workers 3
105
+ preload_app!
106
+ ```
107
+
108
+ Additionally, you can specify a block in your configuration file that will be run on boot of each worker:
109
+
110
+ ```ruby
111
+ # config/puma.rb
112
+ on_worker_boot do
113
+ # configuration here
114
+ end
115
+ ```
116
+
117
+ This code can be used to setup the process before booting the application, allowing
118
+ you to do some Puma-specific things that you don't want to embed in your application.
119
+ For instance, you could fire a log notification that a worker booted or send something to statsd. This can be called multiple times.
120
+
121
+ `before_fork` specifies a block to be run before workers are forked:
122
+
123
+ ```ruby
124
+ # config/puma.rb
125
+ before_fork do
126
+ # configuration here
127
+ end
128
+ ```
129
+
130
+ 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.
131
+
132
+ ### Error handling
133
+
134
+ If puma encounters an error outside of the context of your application, it will respond with a 500 and a simple
135
+ textual error message (see `lowlevel_error` in [this file](https://github.com/puma/puma/blob/master/lib/puma/server.rb)).
136
+ You can specify custom behavior for this scenario. For example, you can report the error to your third-party
137
+ error-tracking service (in this example, [rollbar](http://rollbar.com)):
138
+
139
+ ```ruby
140
+ lowlevel_error_handler do |e|
141
+ Rollbar.critical(e)
142
+ [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"]]
143
+ end
144
+ ```
145
+
146
+ ### Binding TCP / Sockets
147
+
148
+ Bind Puma to a socket with the `-b` (or `--bind`) flag:
149
+
150
+ ```
151
+ $ puma -b tcp://127.0.0.1:9292
152
+ ```
153
+
154
+ To use a UNIX Socket instead of TCP:
155
+
156
+ ```
157
+ $ puma -b unix:///var/run/puma.sock
158
+ ```
159
+
160
+ If you need to change the permissions of the UNIX socket, just add a umask parameter:
161
+
162
+ ```
163
+ $ puma -b 'unix:///var/run/puma.sock?umask=0111'
164
+ ```
165
+
166
+ Need a bit of security? Use SSL sockets:
167
+
168
+ ```
169
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'
170
+ ```
171
+
172
+ #### Controlling SSL Cipher Suites
173
+
174
+ To use or avoid specific SSL cipher suites, use `ssl_cipher_filter` or `ssl_cipher_list` options.
175
+
176
+ ##### Ruby:
177
+
178
+ ```
179
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&ssl_cipher_filter=!aNULL:AES+SHA'
180
+ ```
181
+
182
+ ##### JRuby:
183
+
184
+ ```
185
+ $ 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'
186
+ ```
187
+
188
+ See https://www.openssl.org/docs/man1.0.2/apps/ciphers.html for cipher filter format and full list of cipher suites.
189
+
190
+ Disable TLS v1 with the `no_tlsv1` option:
191
+
192
+ ```
193
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&no_tlsv1=true'
194
+ ```
195
+
196
+ ### Control/Status Server
197
+
198
+ Puma has a built-in status and control app that can be used to query and control Puma.
199
+
200
+ ```
201
+ $ puma --control-url tcp://127.0.0.1:9293 --control-token foo
202
+ ```
203
+
204
+ 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.
205
+
206
+ You can also interact with the control server via `pumactl`. This command will restart Puma:
207
+
208
+ ```
209
+ $ pumactl --control-url 'tcp://127.0.0.1:9293' --control-token foo restart
210
+ ```
211
+
212
+ To see a list of `pumactl` options, use `pumactl --help`.
213
+
214
+ ### Configuration File
215
+
216
+ You can also provide a configuration file with the `-C` (or `--config`) flag:
217
+
218
+ ```
219
+ $ puma -C /path/to/config
220
+ ```
221
+
222
+ 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`.
223
+
224
+ 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:
225
+
226
+ ```
227
+ $ puma -C "-"
228
+ ```
229
+
230
+ Check out [dsl.rb](https://github.com/puma/puma/blob/master/lib/puma/dsl.rb) to see all available options.
231
+
232
+ ## Restart
233
+
234
+ 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.
235
+
236
+ For more, see the [restart documentation](https://github.com/puma/puma/blob/master/docs/restart.md).
237
+
238
+ ## Signals
239
+
240
+ 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).
241
+
242
+ ## Platform Constraints
243
+
244
+ Some platforms do not support all Puma features.
245
+
246
+ * **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).
247
+ * **Windows**: Cluster mode is not supported due to a lack of fork(2).
248
+
249
+ ## Known Bugs
250
+
251
+ 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](https://bugs.ruby-lang.org/issues/13632). It can be fixed with the gem https://rubygems.org/gems/stopgap_13632:
252
+
253
+ ```ruby
254
+ if %w(2.2.7 2.2.8 2.2.9 2.2.10 2.3.4 2.4.1).include? RUBY_VERSION
255
+ begin
256
+ require 'stopgap_13632'
257
+ rescue LoadError
258
+ end
259
+ end
260
+ ```
261
+
262
+ ## Deployment
263
+
264
+ Puma has support for Capistrano with an [external gem](https://github.com/seuros/capistrano-puma).
265
+
266
+ It is common to use process monitors with Puma. Modern process monitors like systemd or upstart
267
+ provide continuous monitoring and restarts for increased
268
+ reliability in production environments:
269
+
270
+ * [tools/jungle](https://github.com/puma/puma/tree/master/tools/jungle) for sysvinit (init.d) and upstart
271
+ * [docs/systemd](https://github.com/puma/puma/blob/master/docs/systemd.md)
272
+
273
+ ## Community Plugins
274
+
275
+ * [puma-heroku](https://github.com/evanphx/puma-heroku) — default Puma configuration for running on Heroku
276
+ * [puma-metrics](https://github.com/harmjanblok/puma-metrics) — export Puma metrics to Prometheus
277
+ * [puma-plugin-statsd](https://github.com/yob/puma-plugin-statsd) — send Puma metrics to statsd
278
+ * [puma-plugin-systemd](https://github.com/sj26/puma-plugin-systemd) — deeper integration with systemd for notify, status and watchdog
279
+
280
+ ## Contributing
281
+
282
+ To run the test suite:
283
+
284
+ ```bash
285
+ $ bundle install
286
+ $ bundle exec rake
287
+ ```
288
+
289
+ To run a single test file, run only that file:
290
+
291
+ ```bash
292
+ $ ruby -Ilib test/test_integration.rb
293
+ ```
294
+
295
+ Or use [`m`](https://github.com/qrush/m):
296
+
297
+ ```
298
+ $ bundle exec m test/test_binder.rb
299
+ ```
300
+
301
+ Which can also be used to run a single test case:
302
+
303
+ ```
304
+ $ bundle exec m test/test_binder.rb:37
305
+ ```
306
+
307
+ ## License
308
+
309
+ Puma is copyright Evan Phoenix and contributors, licensed under the BSD 3-Clause license. See the included LICENSE file for details.
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (c) 2011 Evan Phoenix
4
+ #
5
+
6
+ require 'puma/cli'
7
+
8
+ cli = Puma::CLI.new ARGV
9
+
10
+ cli.run
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (c) 2014 Evan Phoenix
4
+ #
5
+
6
+ require 'rubygems'
7
+
8
+ gems = ARGV.shift
9
+
10
+ inc = ""
11
+
12
+ if gems == "-I"
13
+ inc = ARGV.shift
14
+ $LOAD_PATH.concat inc.split(":")
15
+ gems = ARGV.shift
16
+ end
17
+
18
+ gems.split(",").each do |s|
19
+ name, ver = s.split(":",2)
20
+ gem name, ver
21
+ end
22
+
23
+ module Puma; end
24
+
25
+ Puma.const_set("WILD_ARGS", ["-I", inc, gems])
26
+
27
+ require 'puma/cli'
28
+
29
+ cli = Puma::CLI.new ARGV
30
+
31
+ cli.run
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'puma/control_cli'
4
+
5
+ cli = Puma::ControlCLI.new ARGV.dup
6
+
7
+ begin
8
+ cli.run
9
+ rescue => e
10
+ STDERR.puts e.message
11
+ exit 1
12
+ end
@@ -0,0 +1,37 @@
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
+ * The time spent waiting for the HTTP request body to be received is exposed to the Rack app as `env['puma.request_body_wait']` (milliseconds)
24
+ * Once received, the connection is pushed into the "todo" set
25
+ * Worker threads pop work off the "todo" set for processing
26
+ * The thread processes the request via the rack application (which generates the HTTP response)
27
+ * The thread writes the response to the connection
28
+ * Finally, the thread become available to process another connection in the "todo" set
29
+
30
+ ### Disabling `queue_requests`
31
+
32
+ ![http://bit.ly/2zxCJ1Z](images/puma-connection-flow-no-reactor.png)
33
+
34
+ The `queue_requests` option is `true` by default, enabling the separate thread used to buffer requests as described above.
35
+
36
+ If set to `false`, this buffer will not be used for connections while waiting for the request to arrive.
37
+ 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.
@@ -0,0 +1,111 @@
1
+ # Deployment engineering for puma
2
+
3
+ Puma is software that is expected to be run in a deployed environment eventually.
4
+ You can certainly use it as your dev server only, but most people look to use
5
+ it in their production deployments as well.
6
+
7
+ To that end, this is meant to serve as a foundation of wisdom how to do that
8
+ in a way that increases happiness and decreases downtime.
9
+
10
+ ## Specifying puma
11
+
12
+ Most people want to do this by putting `gem "puma"` into their Gemfile, so we'll
13
+ go ahead and assume that. Go add it now... we'll wait.
14
+
15
+
16
+ Welcome back!
17
+
18
+ ## Single vs Cluster mode
19
+
20
+ Puma was originally conceived as a thread-only webserver, but grew the ability to
21
+ also use processes in version 2.
22
+
23
+ Here are some rules of thumb:
24
+
25
+ ### MRI
26
+
27
+ * Use cluster mode and set the number of workers to 1.5x the number of cpu cores
28
+ in the machine, minimum 2.
29
+ * Set the number of threads to desired concurrent requests / number of workers.
30
+ Puma defaults to 16 and that's a decent number.
31
+
32
+ #### Migrating from Unicorn
33
+
34
+ * If you're migrating from unicorn though, here are some settings to start with:
35
+ * Set workers to half the number of unicorn workers you're using
36
+ * Set threads to 2
37
+ * Enjoy 50% memory savings
38
+ * As you grow more confident in the thread safety of your app, you can tune the
39
+ workers down and the threads up.
40
+
41
+ #### Ubuntu / Systemd (Systemctl) Installation
42
+
43
+ See [systemd.md](systemd.md)
44
+
45
+ #### Worker utilization
46
+
47
+ **How do you know if you've got enough (or too many workers)?**
48
+
49
+ A good question. Due to MRI's GIL, only one thread can be executing Ruby code at a time.
50
+ But since so many apps are waiting on IO from DBs, etc., they can utilize threads
51
+ to make better use of the process.
52
+
53
+ The rule of thumb is you never want processes that are pegged all the time. This
54
+ means that there is more work to do than the process can get through. On the other
55
+ hand, if you have processes that sit around doing nothing, then they're just eating
56
+ up resources.
57
+
58
+ Watch your CPU utilization over time and aim for about 70% on average. This means
59
+ you've got capacity still but aren't starving threads.
60
+
61
+ **Measuring utilization**
62
+
63
+ Using a timestamp header from an upstream proxy server (eg. nginx or haproxy), it's
64
+ possible to get an indication of how long requests have been waiting for a Puma
65
+ thread to become available.
66
+
67
+ * Have your upstream proxy set a header with the time it received the request:
68
+ * nginx: `proxy_set_header X-Request-Start "${msec}";`
69
+ * haproxy: `http-request set-header X-Request-Start "%t";`
70
+ * In your Rack middleware, determine the amount of time elapsed since `X-Request-Start`.
71
+ * To improve accuracy, you will want to subtract time spent waiting for slow clients:
72
+ * `env['puma.request_body_wait']` contains the number of milliseconds Puma spent
73
+ waiting for the client to send the request body.
74
+ * haproxy: `%Th` (TLS handshake time) and `%Ti` (idle time before request) can
75
+ can also be added as headers.
76
+
77
+ ## Daemonizing
78
+
79
+ I prefer to not daemonize my servers and use something like `runit` or `upstart` to
80
+ monitor them as child processes. This gives them fast response to crashes and
81
+ makes it easy to figure out what is going on. Additionally, unlike `unicorn`,
82
+ puma does not require daemonization to do zero-downtime restarts.
83
+
84
+ I see people using daemonization because they start puma directly via capistrano
85
+ task and thus want it to live on past the `cap deploy`. To these people I say:
86
+ You need to be using a process monitor. Nothing is making sure puma stays up in
87
+ this scenario! You're just waiting for something weird to happen, puma to die,
88
+ and to get paged at 3am. Do yourself a favor, at least the process monitoring
89
+ your OS comes with, be it `sysvinit`, `upstart`, or `systemd`. Or branch out
90
+ and use `runit` or hell, even `monit`.
91
+
92
+ ## Restarting
93
+
94
+ You probably will want to deploy some new code at some point, and you'd like
95
+ puma to start running that new code. Minimizing the amount of time the server
96
+ is unavailable would be nice as well. Here's how to do it:
97
+
98
+ 1. Don't use `preload!`. This dirties the master process and means it will have
99
+ to shutdown all the workers and re-exec itself to get your new code. It is not compatible with phased-restart and `prune_bundler` as well.
100
+
101
+ 1. Use `prune_bundler`. This makes it so that the cluster master will detach itself
102
+ from a Bundler context on start. This allows the cluster workers to load your app
103
+ and start a brand new Bundler context within the worker only. This means your
104
+ master remains pristine and can live on between new releases of your code.
105
+
106
+ 1. Use phased-restart (`SIGUSR1` or `pumactl phased-restart`). This tells the master
107
+ to kill off one worker at a time and restart them in your new code. This minimizes
108
+ downtime and staggers the restart nicely. **WARNING** This means that both your
109
+ old code and your new code will be running concurrently. Most deployment solutions
110
+ already cause that, but it's worth warning you about it again. Be careful with your
111
+ migrations, etc!