puma 5.0.4 → 5.6.4

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 (79) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +322 -48
  3. data/LICENSE +0 -0
  4. data/README.md +95 -24
  5. data/bin/puma-wild +0 -0
  6. data/docs/architecture.md +57 -20
  7. data/docs/compile_options.md +21 -0
  8. data/docs/deployment.md +53 -67
  9. data/docs/fork_worker.md +2 -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/jungle/README.md +0 -0
  14. data/docs/jungle/rc.d/README.md +1 -1
  15. data/docs/jungle/rc.d/puma.conf +0 -0
  16. data/docs/kubernetes.md +66 -0
  17. data/docs/nginx.md +0 -0
  18. data/docs/plugins.md +15 -15
  19. data/docs/rails_dev_mode.md +28 -0
  20. data/docs/restart.md +7 -7
  21. data/docs/signals.md +11 -10
  22. data/docs/stats.md +142 -0
  23. data/docs/systemd.md +85 -66
  24. data/ext/puma_http11/PumaHttp11Service.java +0 -0
  25. data/ext/puma_http11/ext_help.h +0 -0
  26. data/ext/puma_http11/extconf.rb +42 -6
  27. data/ext/puma_http11/http11_parser.c +68 -57
  28. data/ext/puma_http11/http11_parser.h +1 -1
  29. data/ext/puma_http11/http11_parser.java.rl +1 -1
  30. data/ext/puma_http11/http11_parser.rl +1 -1
  31. data/ext/puma_http11/http11_parser_common.rl +1 -1
  32. data/ext/puma_http11/mini_ssl.c +226 -88
  33. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +0 -0
  34. data/ext/puma_http11/org/jruby/puma/Http11.java +0 -0
  35. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +51 -51
  36. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +28 -43
  37. data/ext/puma_http11/puma_http11.c +9 -3
  38. data/lib/puma/app/status.rb +4 -7
  39. data/lib/puma/binder.rb +138 -49
  40. data/lib/puma/cli.rb +18 -4
  41. data/lib/puma/client.rb +113 -31
  42. data/lib/puma/cluster/worker.rb +22 -19
  43. data/lib/puma/cluster/worker_handle.rb +13 -2
  44. data/lib/puma/cluster.rb +75 -33
  45. data/lib/puma/commonlogger.rb +0 -0
  46. data/lib/puma/configuration.rb +21 -2
  47. data/lib/puma/const.rb +17 -8
  48. data/lib/puma/control_cli.rb +76 -71
  49. data/lib/puma/detect.rb +19 -9
  50. data/lib/puma/dsl.rb +225 -31
  51. data/lib/puma/error_logger.rb +12 -5
  52. data/lib/puma/events.rb +18 -3
  53. data/lib/puma/io_buffer.rb +0 -0
  54. data/lib/puma/jruby_restart.rb +0 -0
  55. data/lib/puma/json_serialization.rb +96 -0
  56. data/lib/puma/launcher.rb +56 -7
  57. data/lib/puma/minissl/context_builder.rb +14 -6
  58. data/lib/puma/minissl.rb +72 -40
  59. data/lib/puma/null_io.rb +12 -0
  60. data/lib/puma/plugin/tmp_restart.rb +0 -0
  61. data/lib/puma/plugin.rb +2 -2
  62. data/lib/puma/queue_close.rb +7 -7
  63. data/lib/puma/rack/builder.rb +1 -1
  64. data/lib/puma/rack/urlmap.rb +0 -0
  65. data/lib/puma/rack_default.rb +0 -0
  66. data/lib/puma/reactor.rb +19 -12
  67. data/lib/puma/request.rb +55 -21
  68. data/lib/puma/runner.rb +39 -13
  69. data/lib/puma/server.rb +78 -142
  70. data/lib/puma/single.rb +0 -0
  71. data/lib/puma/state_file.rb +45 -9
  72. data/lib/puma/systemd.rb +46 -0
  73. data/lib/puma/thread_pool.rb +11 -8
  74. data/lib/puma/util.rb +8 -1
  75. data/lib/puma.rb +36 -10
  76. data/lib/rack/handler/puma.rb +1 -0
  77. data/tools/Dockerfile +1 -1
  78. data/tools/trickletest.rb +0 -0
  79. metadata +15 -9
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  <img src="https://puma.io/images/logos/puma-logo-large.png">
3
3
  </p>
4
4
 
5
- # Puma: A Ruby Web Server Built For Concurrency
5
+ # Puma: A Ruby Web Server Built For Parallelism
6
6
 
7
7
  [![Actions MRI](https://github.com/puma/puma/workflows/MRI/badge.svg?branch=master)](https://github.com/puma/puma/actions?query=workflow%3AMRI)
8
8
  [![Actions non MRI](https://github.com/puma/puma/workflows/non_MRI/badge.svg?branch=master)](https://github.com/puma/puma/actions?query=workflow%3Anon_MRI)
@@ -10,13 +10,13 @@
10
10
  [![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)
11
11
  [![StackOverflow](https://img.shields.io/badge/stackoverflow-Puma-blue.svg)]( https://stackoverflow.com/questions/tagged/puma )
12
12
 
13
- Puma is a **simple, fast, multi-threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications**.
13
+ Puma is a **simple, fast, multi-threaded, and highly parallel HTTP 1.1 server for Ruby/Rack applications**.
14
14
 
15
- ## Built For Speed &amp; Concurrency
15
+ ## Built For Speed &amp; Parallelism
16
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.
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 parallel Ruby implementations (JRuby, Rubinius) will use all available CPU cores.
18
18
 
19
- Puma was designed to be the go-to server for [Rubinius](https://rubinius.com), but also works well with JRuby and MRI.
19
+ Originally designed as a server for [Rubinius](https://github.com/rubinius/rubinius), Puma also works well with Ruby (MRI) and JRuby.
20
20
 
21
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
22
 
@@ -64,20 +64,30 @@ You can run your Sinatra application with Puma from the command line like this:
64
64
  $ ruby app.rb -s Puma
65
65
  ```
66
66
 
67
- Or you can configure your Sinatra application to always use Puma:
67
+ In order to actually configure Puma using a config file, like `puma.rb`, however, you need to use the `puma` executable. To do this, you must add a rackup file to your Sinatra app:
68
68
 
69
69
  ```ruby
70
- require 'sinatra'
71
- configure { set :server, :puma }
70
+ # config.ru
71
+ require './app'
72
+ run Sinatra::Application
73
+ ```
74
+
75
+ You can then start your application using:
76
+
77
+ ```
78
+ $ bundle exec puma
72
79
  ```
73
80
 
74
81
  ## Configuration
75
82
 
76
- 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).
83
+ Puma provides numerous options. Consult `puma -h` (or `puma --help`) for a full list of CLI options, or see `Puma::DSL` or [dsl.rb](https://github.com/puma/puma/blob/master/lib/puma/dsl.rb).
77
84
 
78
85
  You can also find several configuration examples as part of the
79
86
  [test](https://github.com/puma/puma/tree/master/test/config) suite.
80
87
 
88
+ For debugging purposes, you can set the environment variable `PUMA_LOG_CONFIG` with a value
89
+ and the loaded configuration will be printed as part of the boot process.
90
+
81
91
  ### Thread Pool
82
92
 
83
93
  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:
@@ -127,6 +137,11 @@ This code can be used to setup the process before booting the application, allow
127
137
  you to do some Puma-specific things that you don't want to embed in your application.
128
138
  For instance, you could fire a log notification that a worker booted or send something to statsd. This can be called multiple times.
129
139
 
140
+ Constants loaded by your application (such as `Rails`) will not be available in `on_worker_boot`.
141
+ However, these constants _will_ be available if `preload_app!` is enabled, either explicitly in your `puma` config or automatically if
142
+ using 2 or more workers in cluster mode.
143
+ If `preload_app!` is not enabled and 1 worker is used, then `on_worker_boot` will fire, but your app will not be preloaded and constants will not be available.
144
+
130
145
  `before_fork` specifies a block to be run before workers are forked:
131
146
 
132
147
  ```ruby
@@ -136,12 +151,12 @@ before_fork do
136
151
  end
137
152
  ```
138
153
 
139
- 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.
154
+ 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.
140
155
 
141
156
  ### Error handling
142
157
 
143
158
  If puma encounters an error outside of the context of your application, it will respond with a 500 and a simple
144
- textual error message (see `lowlevel_error` in [this file](https://github.com/puma/puma/blob/master/lib/puma/server.rb)).
159
+ textual error message (see `Puma::Server#lowlevel_error` or [server.rb](https://github.com/puma/puma/blob/master/lib/puma/server.rb)).
145
160
  You can specify custom behavior for this scenario. For example, you can report the error to your third-party
146
161
  error-tracking service (in this example, [rollbar](https://rollbar.com)):
147
162
 
@@ -177,6 +192,38 @@ Need a bit of security? Use SSL sockets:
177
192
  ```
178
193
  $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'
179
194
  ```
195
+ #### Self-signed SSL certificates (via the [`localhost`] gem, for development use):
196
+
197
+ Puma supports the [`localhost`] gem for self-signed certificates. This is particularly useful if you want to use Puma with SSL locally, and self-signed certificates will work for your use-case. Currently, the integration can only be used in MRI.
198
+
199
+ Puma automatically configures SSL when the [`localhost`] gem is loaded in a `development` environment:
200
+
201
+ ```ruby
202
+ # Add the gem to your Gemfile
203
+ group(:development) do
204
+ gem 'localhost'
205
+ end
206
+
207
+ # And require it implicitly using bundler
208
+ require "bundler"
209
+ Bundler.require(:default, ENV["RACK_ENV"].to_sym)
210
+
211
+ # Alternatively, you can require the gem in config.ru:
212
+ require './app'
213
+ require 'localhost'
214
+ run Sinatra::Application
215
+ ```
216
+
217
+ Additionally, Puma must be listening to an SSL socket:
218
+
219
+ ```shell
220
+ $ puma -b 'ssl://localhost:9292' config.ru
221
+
222
+ # The following options allow you to reach Puma over HTTP as well:
223
+ $ puma -b ssl://localhost:9292 -b tcp://localhost:9393 config.ru
224
+ ```
225
+
226
+ [`localhost`]: https://github.com/socketry/localhost
180
227
 
181
228
  #### Controlling SSL Cipher Suites
182
229
 
@@ -194,7 +241,7 @@ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&ssl_cipher_fil
194
241
  $ 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'
195
242
  ```
196
243
 
197
- See https://www.openssl.org/docs/man1.0.2/apps/ciphers.html for cipher filter format and full list of cipher suites.
244
+ See https://www.openssl.org/docs/man1.1.1/man1/ciphers.html for cipher filter format and full list of cipher suites.
198
245
 
199
246
  Disable TLS v1 with the `no_tlsv1` option:
200
247
 
@@ -202,6 +249,23 @@ Disable TLS v1 with the `no_tlsv1` option:
202
249
  $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&no_tlsv1=true'
203
250
  ```
204
251
 
252
+ #### Controlling OpenSSL Verification Flags
253
+
254
+ To enable verification flags offered by OpenSSL, use `verification_flags` (not available for JRuby):
255
+
256
+ ```
257
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&verification_flags=PARTIAL_CHAIN'
258
+ ```
259
+
260
+ You can also set multiple verification flags (by separating them with coma):
261
+
262
+ ```
263
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&verification_flags=PARTIAL_CHAIN,CRL_CHECK'
264
+ ```
265
+
266
+ List of available flags: `USE_CHECK_TIME`, `CRL_CHECK`, `CRL_CHECK_ALL`, `IGNORE_CRITICAL`, `X509_STRICT`, `ALLOW_PROXY_CERTS`, `POLICY_CHECK`, `EXPLICIT_POLICY`, `INHIBIT_ANY`, `INHIBIT_MAP`, `NOTIFY_POLICY`, `EXTENDED_CRL_SUPPORT`, `USE_DELTAS`, `CHECK_SS_SIGNATURE`, `TRUSTED_FIRST`, `SUITEB_128_LOS_ONLY`, `SUITEB_192_LOS`, `SUITEB_128_LOS`, `PARTIAL_CHAIN`, `NO_ALT_CHAINS`, `NO_CHECK_TIME`
267
+ (see https://www.openssl.org/docs/manmaster/man3/X509_VERIFY_PARAM_set_hostflags.html#VERIFICATION-FLAGS).
268
+
205
269
  ### Control/Status Server
206
270
 
207
271
  Puma has a built-in status and control app that can be used to query and control Puma.
@@ -210,7 +274,7 @@ Puma has a built-in status and control app that can be used to query and control
210
274
  $ puma --control-url tcp://127.0.0.1:9293 --control-token foo
211
275
  ```
212
276
 
213
- 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.
277
+ 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 `Puma::App::Status` or [status.rb](https://github.com/puma/puma/blob/master/lib/puma/app/status.rb) to see what the status app has available.
214
278
 
215
279
  You can also interact with the control server via `pumactl`. This command will restart Puma:
216
280
 
@@ -228,27 +292,31 @@ You can also provide a configuration file with the `-C` (or `--config`) flag:
228
292
  $ puma -C /path/to/config
229
293
  ```
230
294
 
231
- 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` or the `RAILS_ENV` environment variables, Puma looks for configuration at `config/puma/<environment_name>.rb`.
295
+ If no configuration file is specified, Puma will look for a configuration file at `config/puma.rb`. If an environment is specified (via the `--environment` flag or through the `APP_ENV`, `RACK_ENV`, or `RAILS_ENV` environment variables) Puma looks for a configuration file at `config/puma/<environment_name>.rb` and then falls back to `config/puma.rb`.
232
296
 
233
- 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:
297
+ If you want to prevent Puma from looking for a configuration file in those locations, include the `--no-config` flag:
234
298
 
235
299
  ```
300
+ $ puma --no-config
301
+
302
+ # or
303
+
236
304
  $ puma -C "-"
237
305
  ```
238
306
 
239
- The other side-effects of setting the environment are whether to show stack traces (in `development` or `test`), and setting RACK_ENV may potentially affect middleware looking for this value to change their behavior. The default puma RACK_ENV value is `development`. You can see all config default values [here](https://github.com/puma/puma/blob/12d1706ddc71b89ed2ee26275e31c788e94ff541/lib/puma/configuration.rb#L170).
307
+ The other side-effects of setting the environment are whether to show stack traces (in `development` or `test`), and setting RACK_ENV may potentially affect middleware looking for this value to change their behavior. The default puma RACK_ENV value is `development`. You can see all config default values in `Puma::Configuration#puma_default_options` or [configuration.rb](https://github.com/puma/puma/blob/61c6213fbab/lib/puma/configuration.rb#L182-L204).
240
308
 
241
- Check out [dsl.rb](https://github.com/puma/puma/blob/master/lib/puma/dsl.rb) to see all available options.
309
+ Check out `Puma::DSL` or [dsl.rb](https://github.com/puma/puma/blob/master/lib/puma/dsl.rb) to see all available options.
242
310
 
243
311
  ## Restart
244
312
 
245
313
  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.
246
314
 
247
- For more, see the [restart documentation](https://github.com/puma/puma/blob/master/docs/restart.md).
315
+ For more, see the [Restart documentation](docs/restart.md).
248
316
 
249
317
  ## Signals
250
318
 
251
- 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).
319
+ Puma responds to several signals. A detailed guide to using UNIX signals with Puma can be found in the [Signals documentation](docs/signals.md).
252
320
 
253
321
  ## Platform Constraints
254
322
 
@@ -256,6 +324,7 @@ Some platforms do not support all Puma features.
256
324
 
257
325
  * **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).
258
326
  * **Windows**: Cluster mode is not supported due to a lack of fork(2).
327
+ * **Kubernetes**: The way Kubernetes handles pod shutdowns interacts poorly with server processes implementing graceful shutdown, like Puma. See the [kubernetes section of the documentation](docs/kubernetes.md) for more details.
259
328
 
260
329
  ## Known Bugs
261
330
 
@@ -278,8 +347,12 @@ It is common to use process monitors with Puma. Modern process monitors like sys
278
347
  provide continuous monitoring and restarts for increased
279
348
  reliability in production environments:
280
349
 
281
- * [docs/jungle](https://github.com/puma/puma/tree/master/docs/jungle) for rc.d
282
- * [docs/systemd](https://github.com/puma/puma/blob/master/docs/systemd.md)
350
+ * [rc.d](docs/jungle/rc.d/README.md)
351
+ * [systemd](docs/systemd.md)
352
+
353
+ Community guides:
354
+
355
+ * [Deploying Puma on OpenBSD using relayd and httpd](https://gist.github.com/anon987654321/4532cf8d6c59c1f43ec8973faa031103)
283
356
 
284
357
  ## Community Extensions
285
358
 
@@ -295,9 +368,7 @@ reliability in production environments:
295
368
 
296
369
  ## Contributing
297
370
 
298
- Find details for contributing in the [contribution guide].
299
-
300
- [contribution guide]: https://github.com/puma/puma/blob/master/CONTRIBUTING.md
371
+ Find details for contributing in the [contribution guide](CONTRIBUTING.md).
301
372
 
302
373
  ## License
303
374
 
data/bin/puma-wild CHANGED
File without changes
data/docs/architecture.md CHANGED
@@ -4,34 +4,71 @@
4
4
 
5
5
  ![https://bit.ly/2iJuFky](images/puma-general-arch.png)
6
6
 
7
- Puma is a threaded web server, processing requests across a TCP or UNIX socket.
7
+ Puma is a threaded Ruby HTTP application server processing requests across a TCP
8
+ and/or UNIX socket.
8
9
 
9
- Workers accept connections from the socket and a thread in the worker's thread pool processes the client's request.
10
10
 
11
- Clustered mode is shown/discussed here. Single mode is analogous to having a single worker process.
11
+ Puma processes (there can be one or many) accept connections from the socket via
12
+ a thread (in the [`Reactor`](../lib/puma/reactor.rb) class). The connection,
13
+ once fully buffered and read, moves into the `todo` list, where an available
14
+ thread will pick it up (in the [`ThreadPool`](../lib/puma/thread_pool.rb)
15
+ class).
12
16
 
13
- ## Connection pipeline
17
+ Puma works in two main modes: cluster and single. In single mode, only one Puma
18
+ process boots. In cluster mode, a `master` process is booted, which prepares
19
+ (and may boot) the application and then uses the `fork()` system call to create
20
+ one or more `child` processes. These `child` processes all listen to the same
21
+ socket. The `master` process does not listen to the socket or process requests -
22
+ its purpose is primarily to manage and listen for UNIX signals and possibly kill
23
+ or boot `child` processes.
24
+
25
+ We sometimes call `child` processes (or Puma processes in `single` mode)
26
+ _workers_, and we sometimes call the threads created by Puma's
27
+ [`ThreadPool`](../lib/puma/thread_pool.rb) _worker threads_.
28
+
29
+ ## How Requests Work
14
30
 
15
31
  ![https://bit.ly/2zwzhEK](images/puma-connection-flow.png)
16
32
 
17
33
  * 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`
34
+ * The backlog of this socket is configured with a default of 1024, but the
35
+ actual backlog value is capped by the `net.core.somaxconn` sysctl value.
36
+ The backlog determines the size of the queue for unaccepted connections. If
37
+ the backlog is full, the operating system is not accepting new connections.
38
+ * This socket backlog is distinct from the `backlog` of work as reported by
39
+ `Puma.stats` or the control server. The backlog that `Puma.stats` refers to
40
+ represents the number of connections in the process' `todo` set waiting for
41
+ a thread from the [`ThreadPool`](../lib/puma/thread_pool.rb).
42
+ * By default, a single, separate thread (created by the
43
+ [`Reactor`](../lib/puma/reactor.rb) class) reads and buffers requests from the
44
+ socket.
45
+ * When at least one worker thread is available for work, the reactor thread
46
+ listens to the socket and accepts a request (if one is waiting).
47
+ * The reactor thread waits for the entire HTTP request to be received.
48
+ * Puma exposes the time spent waiting for the HTTP request body to be
49
+ received to the Rack app as `env['puma.request_body_wait']`
50
+ (milliseconds).
51
+ * Once fully buffered and received, the connection is pushed into the "todo"
52
+ set.
53
+ * Worker threads pop work off the "todo" set for processing.
54
+ * The worker thread processes the request via `call`ing the configured Rack
55
+ application. The Rack application generates the HTTP response.
56
+ * The worker thread writes the response to the connection. While Puma buffers
57
+ requests via a separate thread, it does not use a separate thread for
58
+ responses.
59
+ * Once done, the thread becomes available to process another connection in the
60
+ "todo" set.
61
+
62
+ ### `queue_requests`
31
63
 
32
64
  ![https://bit.ly/2zxCJ1Z](images/puma-connection-flow-no-reactor.png)
33
65
 
34
- The `queue_requests` option is `true` by default, enabling the separate thread used to buffer requests as described above.
66
+ The `queue_requests` option is `true` by default, enabling the separate reactor
67
+ thread used to buffer requests as described above.
68
+
69
+ If set to `false`, this buffer will not be used for connections while waiting
70
+ for the request to arrive.
35
71
 
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.
72
+ In this mode, when a connection is accepted, it is added to the "todo" queue
73
+ immediately, and a worker will synchronously do any waiting necessary to read
74
+ the HTTP request from the socket.
@@ -0,0 +1,21 @@
1
+ # Compile Options
2
+
3
+ There are some `cflags` provided to change Puma's default configuration for its
4
+ C extension.
5
+
6
+ ## Query String, `PUMA_QUERY_STRING_MAX_LENGTH`
7
+
8
+ By default, the max length of `QUERY_STRING` is `1024 * 10`. But you may want to
9
+ adjust it to accept longer queries in GET requests.
10
+
11
+ For manual install, pass the `PUMA_QUERY_STRING_MAX_LENGTH` option like this:
12
+
13
+ ```
14
+ gem install puma -- --with-cflags="-D PUMA_QUERY_STRING_MAX_LENGTH=64000"
15
+ ```
16
+
17
+ For Bundler, use its configuration system:
18
+
19
+ ```
20
+ bundle config build.puma "--with-cflags='-D PUMA_QUERY_STRING_MAX_LENGTH=64000'"
21
+ ```
data/docs/deployment.md CHANGED
@@ -1,35 +1,32 @@
1
1
  # Deployment engineering for Puma
2
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.
3
+ Puma expects to be run in a deployed environment eventually. You can use it as
4
+ your development server, but most people use it in their production deployments.
6
5
 
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.
6
+ To that end, this document serves as a foundation of wisdom regarding deploying
7
+ Puma to production while increasing happiness and decreasing downtime.
9
8
 
10
9
  ## Specifying Puma
11
10
 
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.
11
+ Most people will specify Puma by including `gem "puma"` in a Gemfile, so we'll
12
+ assume this is how you're using Puma.
14
13
 
15
- Welcome back!
14
+ ## Single vs. Cluster mode
16
15
 
17
- ## Single vs Cluster mode
16
+ Initially, Puma was conceived as a thread-only web server, but support for
17
+ processes was added in version 2.
18
18
 
19
- Puma was originally conceived as a thread-only webserver, but grew the ability to
20
- also use processes in version 2.
19
+ To run `puma` in single mode (i.e., as a development environment), set the
20
+ number of workers to 0; anything higher will run in cluster mode.
21
21
 
22
- To run `puma` in single mode (e.g. for a development environment) you will need to
23
- set the number of workers to 0, anything above will run in cluster mode.
24
-
25
- Here are some rules of thumb for cluster mode:
22
+ Here are some tips for cluster mode:
26
23
 
27
24
  ### MRI
28
25
 
29
- * Use cluster mode and set the number of workers to 1.5x the number of cpu cores
30
- in the machine, minimum 2.
31
- * Set the number of threads to desired concurrent requests / number of workers.
32
- Puma defaults to 16 and that's a decent number.
26
+ * Use cluster mode and set the number of workers to 1.5x the number of CPU cores
27
+ in the machine, starting from a minimum of 2.
28
+ * Set the number of threads to desired concurrent requests/number of workers.
29
+ Puma defaults to 5, and that's a decent number.
33
30
 
34
31
  #### Migrating from Unicorn
35
32
 
@@ -37,7 +34,7 @@ Here are some rules of thumb for cluster mode:
37
34
  * Set workers to half the number of unicorn workers you're using
38
35
  * Set threads to 2
39
36
  * Enjoy 50% memory savings
40
- * As you grow more confident in the thread safety of your app, you can tune the
37
+ * As you grow more confident in the thread-safety of your app, you can tune the
41
38
  workers down and the threads up.
42
39
 
43
40
  #### Ubuntu / Systemd (Systemctl) Installation
@@ -48,69 +45,58 @@ See [systemd.md](systemd.md)
48
45
 
49
46
  **How do you know if you've got enough (or too many workers)?**
50
47
 
51
- A good question. Due to MRI's GIL, only one thread can be executing Ruby code at a time.
52
- But since so many apps are waiting on IO from DBs, etc., they can utilize threads
53
- to make better use of the process.
48
+ A good question. Due to MRI's GIL, only one thread can be executing Ruby code at
49
+ a time. But since so many apps are waiting on IO from DBs, etc., they can
50
+ utilize threads to use the process more efficiently.
54
51
 
55
- The rule of thumb is you never want processes that are pegged all the time. This
56
- means that there is more work to do than the process can get through. On the other
57
- hand, if you have processes that sit around doing nothing, then they're just eating
58
- up resources.
52
+ Generally, you never want processes that are pegged all the time. That can mean
53
+ there is more work to do than the process can get through. On the other hand, if
54
+ you have processes that sit around doing nothing, then they're just eating up
55
+ resources.
59
56
 
60
- Watch your CPU utilization over time and aim for about 70% on average. This means
61
- you've got capacity still but aren't starving threads.
57
+ Watch your CPU utilization over time and aim for about 70% on average. 70%
58
+ utilization means you've got capacity still but aren't starving threads.
62
59
 
63
60
  **Measuring utilization**
64
61
 
65
- Using a timestamp header from an upstream proxy server (eg. nginx or haproxy), it's
66
- possible to get an indication of how long requests have been waiting for a Puma
67
- thread to become available.
62
+ Using a timestamp header from an upstream proxy server (e.g., `nginx` or
63
+ `haproxy`) makes it possible to indicate how long requests have been waiting for
64
+ a Puma thread to become available.
68
65
 
69
66
  * Have your upstream proxy set a header with the time it received the request:
70
67
  * nginx: `proxy_set_header X-Request-Start "${msec}";`
71
- * haproxy >= 1.9: `http-request set-header X-Request-Start t=%[date()]%[date_us()]`
68
+ * haproxy >= 1.9: `http-request set-header X-Request-Start
69
+ t=%[date()]%[date_us()]`
72
70
  * haproxy < 1.9: `http-request set-header X-Request-Start t=%[date()]`
73
- * In your Rack middleware, determine the amount of time elapsed since `X-Request-Start`.
74
- * To improve accuracy, you will want to subtract time spent waiting for slow clients:
75
- * `env['puma.request_body_wait']` contains the number of milliseconds Puma spent
76
- waiting for the client to send the request body.
77
- * haproxy: `%Th` (TLS handshake time) and `%Ti` (idle time before request) can
78
- can also be added as headers.
71
+ * In your Rack middleware, determine the amount of time elapsed since
72
+ `X-Request-Start`.
73
+ * To improve accuracy, you will want to subtract time spent waiting for slow
74
+ clients:
75
+ * `env['puma.request_body_wait']` contains the number of milliseconds Puma
76
+ spent waiting for the client to send the request body.
77
+ * haproxy: `%Th` (TLS handshake time) and `%Ti` (idle time before request)
78
+ can can also be added as headers.
79
79
 
80
80
  ## Should I daemonize?
81
81
 
82
- Daemonization was removed in Puma 5.0. For alternatives, continue reading.
82
+ The Puma 5.0 release removed daemonization. For older versions and alternatives,
83
+ continue reading.
83
84
 
84
- I prefer to not daemonize my servers and use something like `runit` or `systemd` to
85
- monitor them as child processes. This gives them fast response to crashes and
85
+ I prefer not to daemonize my servers and use something like `runit` or `systemd`
86
+ to monitor them as child processes. This gives them fast response to crashes and
86
87
  makes it easy to figure out what is going on. Additionally, unlike `unicorn`,
87
- puma does not require daemonization to do zero-downtime restarts.
88
+ Puma does not require daemonization to do zero-downtime restarts.
88
89
 
89
- I see people using daemonization because they start puma directly via capistrano
90
- task and thus want it to live on past the `cap deploy`. To these people I say:
91
- You need to be using a process monitor. Nothing is making sure puma stays up in
92
- this scenario! You're just waiting for something weird to happen, puma to die,
93
- and to get paged at 3am. Do yourself a favor, at least the process monitoring
94
- your OS comes with, be it `sysvinit` or `systemd`. Or branch out
95
- and use `runit` or hell, even `monit`.
90
+ I see people using daemonization because they start puma directly via Capistrano
91
+ task and thus want it to live on past the `cap deploy`. To these people, I say:
92
+ You need to be using a process monitor. Nothing is making sure Puma stays up in
93
+ this scenario! You're just waiting for something weird to happen, Puma to die,
94
+ and to get paged at 3 AM. Do yourself a favor, at least the process monitoring
95
+ your OS comes with, be it `sysvinit` or `systemd`. Or branch out and use `runit`
96
+ or hell, even `monit`.
96
97
 
97
98
  ## Restarting
98
99
 
99
100
  You probably will want to deploy some new code at some point, and you'd like
100
- puma to start running that new code. Minimizing the amount of time the server
101
- is unavailable would be nice as well. Here's how to do it:
102
-
103
- 1. Don't use `preload!`. This dirties the master process and means it will have
104
- 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.
105
-
106
- 1. Use `prune_bundler`. This makes it so that the cluster master will detach itself
107
- from a Bundler context on start. This allows the cluster workers to load your app
108
- and start a brand new Bundler context within the worker only. This means your
109
- master remains pristine and can live on between new releases of your code.
110
-
111
- 1. Use phased-restart (`SIGUSR1` or `pumactl phased-restart`). This tells the master
112
- to kill off one worker at a time and restart them in your new code. This minimizes
113
- downtime and staggers the restart nicely. **WARNING** This means that both your
114
- old code and your new code will be running concurrently. Most deployment solutions
115
- already cause that, but it's worth warning you about it again. Be careful with your
116
- migrations, etc!
101
+ Puma to start running that new code. There are a few options for restarting
102
+ Puma, described separately in our [restart documentation](restart.md).
data/docs/fork_worker.md CHANGED
@@ -24,6 +24,8 @@ Similar to the `preload_app!` option, the `fork_worker` option allows your appli
24
24
 
25
25
  ### Limitations
26
26
 
27
+ - Not compatible with the `preload_app!` option
28
+
27
29
  - This mode is still very experimental so there may be bugs or edge-cases, particularly around expected behavior of existing hooks. Please open a [bug report](https://github.com/puma/puma/issues/new?template=bug_report.md) if you encounter any issues.
28
30
 
29
31
  - In order to fork new workers cleanly, worker 0 shuts down its server and stops serving requests so there are no open file descriptors or other kinds of shared global state between processes, and to maximize copy-on-write efficiency across the newly-forked workers. This may temporarily reduce total capacity of the cluster during a phased restart / refork.
File without changes
File without changes
File without changes
File without changes
@@ -1,6 +1,6 @@
1
1
  # Puma as a service using rc.d
2
2
 
3
- Manage multilpe Puma servers as services on one box using FreeBSD's rc.d service.
3
+ Manage multiple Puma servers as services on one box using FreeBSD's rc.d service.
4
4
 
5
5
  ## Dependencies
6
6
 
File without changes
@@ -0,0 +1,66 @@
1
+ # Kubernetes
2
+
3
+ ## Running Puma in Kubernetes
4
+
5
+ In general running Puma in Kubernetes works as-is, no special configuration is needed beyond what you would write anyway to get a new Kubernetes Deployment going. There is one known interaction between the way Kubernetes handles pod termination and how Puma handles `SIGINT`, where some request might be sent to Puma after it has already entered graceful shutdown mode and is no longer accepting requests. This can lead to dropped requests during rolling deploys. A workaround for this is listed at the end of this article.
6
+
7
+ ## Basic setup
8
+
9
+ Assuming you already have a running cluster and docker image repository, you can run a simple Puma app with the following example Dockerfile and Deployment specification. These are meant as examples only and are deliberately very minimal to the point of skipping many options that are recommended for running in production, like healthchecks and envvar configuration with ConfigMaps. In general you should check the [Kubernetes documentation](https://kubernetes.io/docs/home/) and [Docker documentation](https://docs.docker.com/) for a more comprehensive overview of the available options.
10
+
11
+ A basic Dockerfile example:
12
+ ```
13
+ FROM ruby:2.5.1-alpine # can be updated to newer ruby versions
14
+ RUN apk update && apk add build-base # and any other packages you need
15
+
16
+ # Only rebuild gem bundle if Gemfile changes
17
+ COPY Gemfile Gemfile.lock ./
18
+ RUN bundle install
19
+
20
+ # Copy over the rest of the files
21
+ COPY . .
22
+
23
+ # Open up port and start the service
24
+ EXPOSE 9292
25
+ CMD bundle exec rackup -o 0.0.0.0
26
+ ```
27
+
28
+ A sample `deployment.yaml`:
29
+ ```
30
+ ---
31
+ apiVersion: apps/v1
32
+ kind: Deployment
33
+ metadata:
34
+ name: my-awesome-puma-app
35
+ spec:
36
+ selector:
37
+ matchLabels:
38
+ app: my-awesome-puma-app
39
+ template:
40
+ metadata:
41
+ labels:
42
+ app: my-awesome-puma-app
43
+ service: my-awesome-puma-app
44
+ spec:
45
+ containers:
46
+ - name: my-awesome-puma-app
47
+ image: <your image here>
48
+ ports:
49
+ - containerPort: 9292
50
+ ```
51
+
52
+ ## Graceful shutdown and pod termination
53
+
54
+ For some high-throughput systems, it is possible that some HTTP requests will return responses with response codes in the 5XX range during a rolling deploy to a new version. This is caused by [the way that Kubernetes terminates a pod during rolling deploys](https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-terminating-with-grace):
55
+
56
+ 1. The replication controller determines a pod should be shut down.
57
+ 2. The Pod is set to the “Terminating” State and removed from the endpoints list of all Services, so that it receives no more requests.
58
+ 3. The pods pre-stop hook get called. The default for this is to send `SIGTERM` to the process inside the pod.
59
+ 4. The pod has up to `terminationGracePeriodSeconds` (default: 30 seconds) to gracefully shut down. Puma will do this (after it receives SIGTERM) by closing down the socket that accepts new requests and finishing any requests already running before exiting the Puma process.
60
+ 5. If the pod is still running after `terminationGracePeriodSeconds` has elapsed, the pod receives `SIGKILL` to make sure the process inside it stops. After that, the container exits and all other Kubernetes objects associated with it are cleaned up.
61
+
62
+ There is a subtle race condition between step 2 and 3: The replication controller does not synchronously remove the pod from the Services AND THEN call the pre-stop hook of the pod, but rather it asynchronously sends "remove this pod from your endpoints" requests to the Services and then immediately proceeds to invoke the pods' pre-stop hook. If the Service controller (typically something like nginx or haproxy) receives this request handles this request "too" late (due to internal lag or network latency between the replication and Service controllers) then it is possible that the Service controller will send one or more requests to a Puma process which has already shut down its listening socket. These requests will then fail with 5XX error codes.
63
+
64
+ The way Kubernetes works this way, rather than handling step 2 synchronously, is due to the CAP theorem: in a distributed system there is no way to guarantee that any message will arrive promptly. In particular, waiting for all Service controllers to report back might get stuck for an indefinite time if one of them has already been terminated or if there has been a net split. A way to work around this is to add a sleep to the pre-stop hook of the same time as the `terminationGracePeriodSeconds` time. This will allow the Puma process to keep serving new requests during the entire grace period, although it will no longer receive new requests after all Service controllers have propagated the removal of the pod from their endpoint lists. Then, after `terminationGracePeriodSeconds`, the pod receives `SIGKILL` and closes down. If your process can't handle SIGKILL properly, for example because it needs to release locks in different services, you can also sleep for a shorter period (and/or increase `terminationGracePeriodSeconds`) as long as the time slept is longer than the time that your Service controllers take to propagate the pod removal. The downside of this workaround is that all pods will take at minimum the amount of time slept to shut down and this will increase the time required for your rolling deploy.
65
+
66
+ More discussions and links to relevant articles can be found in https://github.com/puma/puma/issues/2343.
data/docs/nginx.md CHANGED
File without changes