puma 5.2.2 → 6.3.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 (74) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +483 -4
  3. data/README.md +101 -20
  4. data/bin/puma-wild +1 -1
  5. data/docs/architecture.md +50 -16
  6. data/docs/compile_options.md +38 -2
  7. data/docs/deployment.md +53 -67
  8. data/docs/fork_worker.md +1 -3
  9. data/docs/jungle/rc.d/README.md +1 -1
  10. data/docs/kubernetes.md +1 -1
  11. data/docs/nginx.md +1 -1
  12. data/docs/plugins.md +15 -15
  13. data/docs/rails_dev_mode.md +2 -3
  14. data/docs/restart.md +7 -7
  15. data/docs/signals.md +11 -10
  16. data/docs/stats.md +8 -8
  17. data/docs/systemd.md +65 -69
  18. data/docs/testing_benchmarks_local_files.md +150 -0
  19. data/docs/testing_test_rackup_ci_files.md +36 -0
  20. data/ext/puma_http11/extconf.rb +44 -13
  21. data/ext/puma_http11/http11_parser.c +24 -11
  22. data/ext/puma_http11/http11_parser.h +2 -2
  23. data/ext/puma_http11/http11_parser.java.rl +2 -2
  24. data/ext/puma_http11/http11_parser.rl +2 -2
  25. data/ext/puma_http11/http11_parser_common.rl +3 -3
  26. data/ext/puma_http11/mini_ssl.c +150 -23
  27. data/ext/puma_http11/org/jruby/puma/Http11.java +3 -3
  28. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +50 -48
  29. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +188 -102
  30. data/ext/puma_http11/puma_http11.c +18 -10
  31. data/lib/puma/app/status.rb +10 -7
  32. data/lib/puma/binder.rb +112 -62
  33. data/lib/puma/cli.rb +24 -20
  34. data/lib/puma/client.rb +162 -36
  35. data/lib/puma/cluster/worker.rb +31 -27
  36. data/lib/puma/cluster/worker_handle.rb +12 -1
  37. data/lib/puma/cluster.rb +102 -61
  38. data/lib/puma/commonlogger.rb +21 -14
  39. data/lib/puma/configuration.rb +78 -54
  40. data/lib/puma/const.rb +135 -97
  41. data/lib/puma/control_cli.rb +25 -20
  42. data/lib/puma/detect.rb +12 -2
  43. data/lib/puma/dsl.rb +308 -58
  44. data/lib/puma/error_logger.rb +20 -11
  45. data/lib/puma/events.rb +6 -126
  46. data/lib/puma/io_buffer.rb +39 -4
  47. data/lib/puma/jruby_restart.rb +2 -1
  48. data/lib/puma/{json.rb → json_serialization.rb} +1 -1
  49. data/lib/puma/launcher/bundle_pruner.rb +104 -0
  50. data/lib/puma/launcher.rb +114 -173
  51. data/lib/puma/log_writer.rb +147 -0
  52. data/lib/puma/minissl/context_builder.rb +30 -16
  53. data/lib/puma/minissl.rb +132 -38
  54. data/lib/puma/null_io.rb +5 -0
  55. data/lib/puma/plugin/systemd.rb +90 -0
  56. data/lib/puma/plugin/tmp_restart.rb +1 -1
  57. data/lib/puma/plugin.rb +2 -2
  58. data/lib/puma/rack/builder.rb +7 -7
  59. data/lib/puma/rack_default.rb +19 -4
  60. data/lib/puma/reactor.rb +19 -10
  61. data/lib/puma/request.rb +373 -153
  62. data/lib/puma/runner.rb +74 -28
  63. data/lib/puma/sd_notify.rb +149 -0
  64. data/lib/puma/server.rb +127 -136
  65. data/lib/puma/single.rb +13 -11
  66. data/lib/puma/state_file.rb +39 -7
  67. data/lib/puma/thread_pool.rb +33 -26
  68. data/lib/puma/util.rb +20 -15
  69. data/lib/puma.rb +28 -11
  70. data/lib/rack/handler/puma.rb +113 -86
  71. data/tools/Dockerfile +1 -1
  72. metadata +15 -10
  73. data/lib/puma/queue_close.rb +0 -26
  74. data/lib/puma/systemd.rb +0 -46
data/README.md CHANGED
@@ -2,21 +2,19 @@
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
- [![Actions MRI](https://github.com/puma/puma/workflows/MRI/badge.svg?branch=master)](https://github.com/puma/puma/actions?query=workflow%3AMRI)
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)
7
+ [![Actions](https://github.com/puma/puma/workflows/Tests/badge.svg?branch=master)](https://github.com/puma/puma/actions?query=workflow%3ATests)
9
8
  [![Code Climate](https://codeclimate.com/github/puma/puma.svg)](https://codeclimate.com/github/puma/puma)
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
9
  [![StackOverflow](https://img.shields.io/badge/stackoverflow-Puma-blue.svg)]( https://stackoverflow.com/questions/tagged/puma )
12
10
 
13
- Puma is a **simple, fast, multi-threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications**.
11
+ Puma is a **simple, fast, multi-threaded, and highly parallel HTTP 1.1 server for Ruby/Rack applications**.
14
12
 
15
- ## Built For Speed &amp; Concurrency
13
+ ## Built For Speed &amp; Parallelism
16
14
 
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.
15
+ 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
16
 
19
- Puma was designed to be the go-to server for [Rubinius](https://rubinius.com), but also works well with JRuby and MRI.
17
+ Originally designed as a server for [Rubinius](https://github.com/rubinius/rubinius), Puma also works well with Ruby (MRI) and JRuby.
20
18
 
21
19
  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
20
 
@@ -108,15 +106,23 @@ Puma also offers "clustered mode". Clustered mode `fork`s workers from a master
108
106
  $ puma -t 8:32 -w 3
109
107
  ```
110
108
 
109
+ Or with the `WEB_CONCURRENCY` environment variable:
110
+
111
+ ```
112
+ $ WEB_CONCURRENCY=3 puma -t 8:32
113
+ ```
114
+
111
115
  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.
112
116
 
113
- 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:
117
+ 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).
118
+
119
+ If the `WEB_CONCURRENCY` environment variable is set to a value > 1 (and `--prune-bundler` has not been specified), preloading will be enabled by default. Otherwise, you can use the `--preload` flag from the command line:
114
120
 
115
121
  ```
116
122
  $ puma -w 3 --preload
117
123
  ```
118
124
 
119
- If you're using a configuration file, use the `preload_app!` method:
125
+ Or, if you're using a configuration file, you can use the `preload_app!` method:
120
126
 
121
127
  ```ruby
122
128
  # config/puma.rb
@@ -124,7 +130,9 @@ workers 3
124
130
  preload_app!
125
131
  ```
126
132
 
127
- Additionally, you can specify a block in your configuration file that will be run on boot of each worker:
133
+ Preloading can’t be used with phased restart, since phased restart kills and restarts workers one-by-one, and preloading copies the code of master into the workers.
134
+
135
+ When using clustered mode, you can specify a block in your configuration file that will be run on boot of each worker:
128
136
 
129
137
  ```ruby
130
138
  # config/puma.rb
@@ -137,7 +145,10 @@ This code can be used to setup the process before booting the application, allow
137
145
  you to do some Puma-specific things that you don't want to embed in your application.
138
146
  For instance, you could fire a log notification that a worker booted or send something to statsd. This can be called multiple times.
139
147
 
140
- `before_fork` specifies a block to be run before workers are forked:
148
+ Constants loaded by your application (such as `Rails`) will not be available in `on_worker_boot`
149
+ unless preloading is enabled.
150
+
151
+ You can also specify a block to be run before workers are forked, using `before_fork`:
141
152
 
142
153
  ```ruby
143
154
  # config/puma.rb
@@ -146,7 +157,14 @@ before_fork do
146
157
  end
147
158
  ```
148
159
 
149
- 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.
160
+ You can also specify a block to be run after puma is booted using `on_booted`:
161
+
162
+ ```ruby
163
+ # config/puma.rb
164
+ on_booted do
165
+ # configuration here
166
+ end
167
+ ```
150
168
 
151
169
  ### Error handling
152
170
 
@@ -187,6 +205,38 @@ Need a bit of security? Use SSL sockets:
187
205
  ```
188
206
  $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'
189
207
  ```
208
+ #### Self-signed SSL certificates (via the [`localhost`] gem, for development use):
209
+
210
+ 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.
211
+
212
+ Puma automatically configures SSL when the [`localhost`] gem is loaded in a `development` environment:
213
+
214
+ ```ruby
215
+ # Add the gem to your Gemfile
216
+ group(:development) do
217
+ gem 'localhost'
218
+ end
219
+
220
+ # And require it implicitly using bundler
221
+ require "bundler"
222
+ Bundler.require(:default, ENV["RACK_ENV"].to_sym)
223
+
224
+ # Alternatively, you can require the gem in config.ru:
225
+ require './app'
226
+ require 'localhost'
227
+ run Sinatra::Application
228
+ ```
229
+
230
+ Additionally, Puma must be listening to an SSL socket:
231
+
232
+ ```shell
233
+ $ puma -b 'ssl://localhost:9292' config.ru
234
+
235
+ # The following options allow you to reach Puma over HTTP as well:
236
+ $ puma -b ssl://localhost:9292 -b tcp://localhost:9393 config.ru
237
+ ```
238
+
239
+ [`localhost`]: https://github.com/socketry/localhost
190
240
 
191
241
  #### Controlling SSL Cipher Suites
192
242
 
@@ -229,6 +279,30 @@ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&verification_f
229
279
  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`
230
280
  (see https://www.openssl.org/docs/manmaster/man3/X509_VERIFY_PARAM_set_hostflags.html#VERIFICATION-FLAGS).
231
281
 
282
+ #### Controlling OpenSSL Password Decryption
283
+
284
+ To enable runtime decryption of an encrypted SSL key (not available for JRuby), use `key_password_command`:
285
+
286
+ ```
287
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&key_password_command=/path/to/command.sh'
288
+ ```
289
+
290
+ `key_password_command` must:
291
+
292
+ 1. Be executable by Puma.
293
+ 2. Print the decryption password to stdout.
294
+
295
+ For example:
296
+
297
+ ```shell
298
+ #!/bin/sh
299
+
300
+ echo "this is my password"
301
+ ```
302
+
303
+ `key_password_command` can be used with `key` or `key_pem`. If the key
304
+ is not encrypted, the executable will not be called.
305
+
232
306
  ### Control/Status Server
233
307
 
234
308
  Puma has a built-in status and control app that can be used to query and control Puma.
@@ -255,11 +329,15 @@ You can also provide a configuration file with the `-C` (or `--config`) flag:
255
329
  $ puma -C /path/to/config
256
330
  ```
257
331
 
258
- 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 first looks for configuration at `config/puma/<environment_name>.rb`, and then falls back to `config/puma.rb`.
332
+ 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`.
259
333
 
260
- 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:
334
+ If you want to prevent Puma from looking for a configuration file in those locations, include the `--no-config` flag:
261
335
 
262
336
  ```
337
+ $ puma --no-config
338
+
339
+ # or
340
+
263
341
  $ puma -C "-"
264
342
  ```
265
343
 
@@ -300,16 +378,18 @@ end
300
378
 
301
379
  ## Deployment
302
380
 
303
- Puma has support for Capistrano with an [external gem](https://github.com/seuros/capistrano-puma).
381
+ * Puma has support for Capistrano with an [external gem](https://github.com/seuros/capistrano-puma).
382
+
383
+ * Additionally, Puma has support for built-in daemonization via the [puma-daemon](https://github.com/kigster/puma-daemon) ruby gem. The gem restores the `daemonize` option that was removed from Puma starting version 5, but only for MRI Ruby.
384
+
304
385
 
305
386
  It is common to use process monitors with Puma. Modern process monitors like systemd or rc.d
306
- provide continuous monitoring and restarts for increased
307
- reliability in production environments:
387
+ provide continuous monitoring and restarts for increased reliability in production environments:
308
388
 
309
389
  * [rc.d](docs/jungle/rc.d/README.md)
310
390
  * [systemd](docs/systemd.md)
311
391
 
312
- Community guides:
392
+ Community guides:
313
393
 
314
394
  * [Deploying Puma on OpenBSD using relayd and httpd](https://gist.github.com/anon987654321/4532cf8d6c59c1f43ec8973faa031103)
315
395
 
@@ -319,7 +399,8 @@ Community guides:
319
399
 
320
400
  * [puma-metrics](https://github.com/harmjanblok/puma-metrics) — export Puma metrics to Prometheus
321
401
  * [puma-plugin-statsd](https://github.com/yob/puma-plugin-statsd) — send Puma metrics to statsd
322
- * [puma-plugin-systemd](https://github.com/sj26/puma-plugin-systemd) — deeper integration with systemd for notify, status and watchdog
402
+ * [puma-plugin-systemd](https://github.com/sj26/puma-plugin-systemd) — deeper integration with systemd for notify, status and watchdog. Puma 5.1.0 integrated notify and watchdog, which probably conflicts with this plugin. Puma 6.1.0 added status support which obsoletes the plugin entirely.
403
+ * [puma-plugin-telemetry](https://github.com/babbel/puma-plugin-telemetry) - telemetry plugin for Puma offering various targets to publish
323
404
 
324
405
  ### Monitoring
325
406
 
data/bin/puma-wild CHANGED
@@ -16,7 +16,7 @@ end
16
16
 
17
17
  module Puma; end
18
18
 
19
- Puma.const_set("WILD_ARGS", ["-I", inc])
19
+ Puma.const_set(:WILD_ARGS, ["-I", inc])
20
20
 
21
21
  require 'puma/cli'
22
22
 
data/docs/architecture.md CHANGED
@@ -4,37 +4,71 @@
4
4
 
5
5
  ![https://bit.ly/2iJuFky](images/puma-general-arch.png)
6
6
 
7
- Puma is a threaded Ruby HTTP application 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
- Puma processes (there can be one or many) accept connections from the socket via a thread (in the `Reactor` class). The connection, once fully buffered and read, moves in to the `todo` list, where it will be picked up by a free/waiting thread in the threadpool (the `ThreadPool` class).
10
10
 
11
- Puma works in two main modes: cluster and single. In single mode, only one Puma process is booted. In cluster mode, a `master` process is booted, which prepares (and may boot) the application, and then uses the `fork()` system call to create 1 or more `child` processes. These `child` processes all listen to the same socket. The `master` process does not listen to the socket or process requests - its purpose is mostly to manage and listen for UNIX signals and possibly kill or boot `child` processes.
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
- We sometimes call `child` processes (or Puma processes in `single` mode) _workers_, and we sometimes call the threads created by Puma's `ThreadPool` _worker threads_.
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_.
14
28
 
15
29
  ## How Requests Work
16
30
 
17
31
  ![https://bit.ly/2zwzhEK](images/puma-connection-flow.png)
18
32
 
19
33
  * Upon startup, Puma listens on a TCP or UNIX socket.
20
- * The backlog of this socket is configured (with a default of 1024). This determines the size of the queue for unaccepted connections. Generally, this setting is unimportant and will never be hit in production use. If the backlog is full, the connection will be refused by the operating system.
21
- * This socket backlog is distinct from the `backlog` of work as reported by `Puma.stats` or the control server. The backlog as reported by Puma is the number of connections in the process' `todo` set waiting for a thread from the `ThreadPool`.
22
- * By default, a single, separate thread (created by the `Reactor` class) is used to read and buffer requests from the socket.
23
- * When at least one worker thread is available for work, the reactor thread listens to the socket and accepts a request, if one is waiting.
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).
24
47
  * The reactor thread waits for the entire HTTP request to be received.
25
- * 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).
26
- * Once fully buffered and received, the connection is pushed into the "todo" set.
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.
27
53
  * Worker threads pop work off the "todo" set for processing.
28
- * The worker thread processes the request via `call`ing the configured Rack application. The Rack application generates the HTTP response.
29
- * The worker thread writes the response to the connection. Note that while Puma buffers requests via a separate thread, it does not use a separate thread for responses.
30
- * Once done, the thread become available to process another connection in the "todo" set.
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.
31
61
 
32
62
  ### `queue_requests`
33
63
 
34
64
  ![https://bit.ly/2zxCJ1Z](images/puma-connection-flow-no-reactor.png)
35
65
 
36
- The `queue_requests` option is `true` by default, enabling the separate reactor 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.
37
68
 
38
- If set to `false`, this buffer will not be used for connections while waiting for the request to arrive.
69
+ If set to `false`, this buffer will not be used for connections while waiting
70
+ for the request to arrive.
39
71
 
40
- 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.
@@ -1,10 +1,12 @@
1
1
  # Compile Options
2
2
 
3
- There are some `cflags` provided to change Puma's default configuration for its C extension.
3
+ There are some `cflags` provided to change Puma's default configuration for its
4
+ C extension.
4
5
 
5
6
  ## Query String, `PUMA_QUERY_STRING_MAX_LENGTH`
6
7
 
7
- By default, the max length of `QUERY_STRING` is `1024 * 10`. But you may want to adjust it to allow accept larger queries in GET requests.
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.
8
10
 
9
11
  For manual install, pass the `PUMA_QUERY_STRING_MAX_LENGTH` option like this:
10
12
 
@@ -17,3 +19,37 @@ For Bundler, use its configuration system:
17
19
  ```
18
20
  bundle config build.puma "--with-cflags='-D PUMA_QUERY_STRING_MAX_LENGTH=64000'"
19
21
  ```
22
+
23
+ ## Request Path, `PUMA_REQUEST_PATH_MAX_LENGTH`
24
+
25
+ By default, the max length of `REQUEST_PATH` is `8192`. But you may want to
26
+ adjust it to accept longer paths in requests.
27
+
28
+ For manual install, pass the `PUMA_REQUEST_PATH_MAX_LENGTH` option like this:
29
+
30
+ ```
31
+ gem install puma -- --with-cflags="-D PUMA_REQUEST_PATH_MAX_LENGTH=64000"
32
+ ```
33
+
34
+ For Bundler, use its configuration system:
35
+
36
+ ```
37
+ bundle config build.puma "--with-cflags='-D PUMA_REQUEST_PATH_MAX_LENGTH=64000'"
38
+ ```
39
+
40
+ ## Request URI, `PUMA_REQUEST_URI_MAX_LENGTH`
41
+
42
+ By default, the max length of `REQUEST_URI` is `1024 * 12`. But you may want to
43
+ adjust it to accept longer URIs in requests.
44
+
45
+ For manual install, pass the `PUMA_REQUEST_URI_MAX_LENGTH` option like this:
46
+
47
+ ```
48
+ gem install puma -- --with-cflags="-D PUMA_REQUEST_URI_MAX_LENGTH=64000"
49
+ ```
50
+
51
+ For Bundler, use its configuration system:
52
+
53
+ ```
54
+ bundle config build.puma "--with-cflags='-D PUMA_REQUEST_URI_MAX_LENGTH=64000'"
55
+ ```
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 5 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
@@ -10,7 +10,7 @@ Puma 5 introduces an experimental new cluster-mode configuration option, `fork_w
10
10
  10004 \_ puma: cluster worker 3: 10000 [puma]
11
11
  ```
12
12
 
13
- Similar to the `preload_app!` option, the `fork_worker` option allows your application to be initialized only once for copy-on-write memory savings, and it has two additional advantages:
13
+ The `fork_worker` option allows your application to be initialized only once for copy-on-write memory savings, and it has two additional advantages:
14
14
 
15
15
  1. **Compatible with phased restart.** Because the master process itself doesn't preload the application, this mode works with phased restart (`SIGUSR1` or `pumactl phased-restart`). When worker 0 reloads as part of a phased restart, it initializes a new copy of your application first, then the other workers reload by forking from this new worker already containing the new preloaded application.
16
16
 
@@ -24,8 +24,6 @@ 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
-
29
27
  - 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.
30
28
 
31
29
  - 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.
@@ -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
 
data/docs/kubernetes.md CHANGED
@@ -61,6 +61,6 @@ For some high-throughput systems, it is possible that some HTTP requests will re
61
61
 
62
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
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 guarantuee 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.
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
65
 
66
66
  More discussions and links to relevant articles can be found in https://github.com/puma/puma/issues/2343.
data/docs/nginx.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This is a very common setup using an upstream. It was adapted from some Capistrano recipe I found on the Internet a while ago.
4
4
 
5
- ```
5
+ ```nginx
6
6
  upstream myapp {
7
7
  server unix:///myapp/tmp/puma.sock;
8
8
  }