puma 6.4.0 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +385 -19
  3. data/README.md +95 -42
  4. data/docs/fork_worker.md +11 -1
  5. data/docs/java_options.md +54 -0
  6. data/docs/kubernetes.md +9 -7
  7. data/docs/plugins.md +4 -0
  8. data/docs/restart.md +3 -2
  9. data/docs/signals.md +11 -11
  10. data/docs/stats.md +10 -4
  11. data/docs/systemd.md +13 -6
  12. data/ext/puma_http11/extconf.rb +22 -30
  13. data/ext/puma_http11/mini_ssl.c +34 -10
  14. data/ext/puma_http11/org/jruby/puma/Http11.java +40 -9
  15. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +2 -1
  16. data/ext/puma_http11/puma_http11.c +26 -11
  17. data/lib/puma/app/status.rb +1 -1
  18. data/lib/puma/binder.rb +21 -11
  19. data/lib/puma/cli.rb +10 -8
  20. data/lib/puma/client.rb +156 -72
  21. data/lib/puma/cluster/worker.rb +16 -14
  22. data/lib/puma/cluster/worker_handle.rb +38 -8
  23. data/lib/puma/cluster.rb +128 -42
  24. data/lib/puma/cluster_accept_loop_delay.rb +91 -0
  25. data/lib/puma/commonlogger.rb +3 -3
  26. data/lib/puma/configuration.rb +125 -63
  27. data/lib/puma/const.rb +29 -11
  28. data/lib/puma/control_cli.rb +10 -6
  29. data/lib/puma/detect.rb +5 -4
  30. data/lib/puma/dsl.rb +412 -119
  31. data/lib/puma/error_logger.rb +7 -5
  32. data/lib/puma/events.rb +25 -10
  33. data/lib/puma/io_buffer.rb +8 -4
  34. data/lib/puma/jruby_restart.rb +0 -16
  35. data/lib/puma/launcher/bundle_pruner.rb +1 -1
  36. data/lib/puma/launcher.rb +71 -54
  37. data/lib/puma/log_writer.rb +9 -9
  38. data/lib/puma/minissl/context_builder.rb +3 -0
  39. data/lib/puma/minissl.rb +6 -1
  40. data/lib/puma/null_io.rb +42 -2
  41. data/lib/puma/plugin/systemd.rb +3 -3
  42. data/lib/puma/rack/urlmap.rb +1 -1
  43. data/lib/puma/reactor.rb +19 -4
  44. data/lib/puma/request.rb +62 -32
  45. data/lib/puma/runner.rb +15 -17
  46. data/lib/puma/sd_notify.rb +1 -4
  47. data/lib/puma/server.rb +151 -69
  48. data/lib/puma/single.rb +5 -2
  49. data/lib/puma/state_file.rb +5 -4
  50. data/lib/puma/thread_pool.rb +57 -80
  51. data/lib/puma/util.rb +0 -7
  52. data/lib/puma.rb +10 -0
  53. data/lib/rack/handler/puma.rb +10 -7
  54. data/tools/Dockerfile +5 -3
  55. metadata +13 -13
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  # Puma: A Ruby Web Server Built For Parallelism
6
6
 
7
- [![Actions](https://github.com/puma/puma/workflows/Tests/badge.svg?branch=master)](https://github.com/puma/puma/actions?query=workflow%3ATests)
7
+ [![Actions](https://github.com/puma/puma/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/puma/puma/actions/workflows/tests.yml?query=branch%3Amaster)
8
8
  [![Code Climate](https://codeclimate.com/github/puma/puma.svg)](https://codeclimate.com/github/puma/puma)
9
9
  [![StackOverflow](https://img.shields.io/badge/stackoverflow-Puma-blue.svg)]( https://stackoverflow.com/questions/tagged/puma )
10
10
 
@@ -12,7 +12,7 @@ Puma is a **simple, fast, multi-threaded, and highly parallel HTTP 1.1 server fo
12
12
 
13
13
  ## Built For Speed & Parallelism
14
14
 
15
- Puma is a server for [Rack](https://github.com/rack/rack)-powered HTTP applications written in Ruby. It is:
15
+ Puma is a server for [Rack](https://github.com/rack/rack)-powered HTTP applications written in Ruby. It is:
16
16
  * **Multi-threaded**. Each request is served in a separate thread. This helps you serve more requests per second with less memory use.
17
17
  * **Multi-process**. "Pre-forks" in cluster mode, using less memory per-process thanks to copy-on-write memory.
18
18
  * **Standalone**. With SSL support, zero-downtime rolling restarts and a built-in request bufferer, you can deploy Puma without any reverse proxy.
@@ -102,9 +102,9 @@ Puma will automatically scale the number of threads, from the minimum until it c
102
102
 
103
103
  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.
104
104
 
105
- ### Clustered mode
105
+ ### Cluster mode
106
106
 
107
- 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:
107
+ Puma also offers "cluster mode". Cluster 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:
108
108
 
109
109
  ```
110
110
  $ puma -t 8:32 -w 3
@@ -116,13 +116,15 @@ Or with the `WEB_CONCURRENCY` environment variable:
116
116
  $ WEB_CONCURRENCY=3 puma -t 8:32
117
117
  ```
118
118
 
119
- 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.
119
+ Note that threads are still used in cluster 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.
120
+
121
+ If the `WEB_CONCURRENCY` environment variable is set to `"auto"` and the `concurrent-ruby` gem is available in your application, Puma will set the worker process count to the result of [available processors](https://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent.html#available_processor_count-class_method).
120
122
 
121
123
  For an in-depth discussion of the tradeoffs of thread and process count settings, [see our docs](https://github.com/puma/puma/blob/9282a8efa5a0c48e39c60d22ca70051a25df9f55/docs/kubernetes.md#workers-per-pod-and-other-config-issues).
122
124
 
123
- 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).
125
+ In cluster 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).
124
126
 
125
- 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:
127
+ If the number of workers is greater than 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:
126
128
 
127
129
  ```
128
130
  $ puma -w 3 --preload
@@ -138,51 +140,106 @@ preload_app!
138
140
 
139
141
  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.
140
142
 
141
- When using clustered mode, you can specify a block in your configuration file that will be run on boot of each worker:
143
+ #### Cluster mode hooks
144
+
145
+ When using clustered mode, Puma's configuration DSL provides `before_fork`, `before_worker_boot`, and `after_worker_shutdown`
146
+ hooks to run code when the master process forks, the child workers are booted, and after each child worker exits respectively.
147
+
148
+ It is recommended to use these hooks with `preload_app!`, otherwise constants loaded by your
149
+ application (such as `Rails`) will not be available inside the hooks.
142
150
 
143
151
  ```ruby
144
152
  # config/puma.rb
145
- on_worker_boot do
146
- # configuration here
153
+ before_fork do
154
+ # Add code to run inside the Puma master process before it forks a worker child.
147
155
  end
148
- ```
149
156
 
150
- This code can be used to setup the process before booting the application, allowing
151
- you to do some Puma-specific things that you don't want to embed in your application.
152
- For instance, you could fire a log notification that a worker booted or send something to statsd. This can be called multiple times.
157
+ before_worker_boot do
158
+ # Add code to run inside the Puma worker process after forking.
159
+ end
153
160
 
154
- Constants loaded by your application (such as `Rails`) will not be available in `on_worker_boot`
155
- unless preloading is enabled.
161
+ after_worker_shutdown do |worker_handle|
162
+ # Add code to run inside the Puma master process after a worker exits. `worker.process_status` can be used to get the
163
+ # `Process::Status` of the exited worker.
164
+ end
165
+ ```
156
166
 
157
- You can also specify a block to be run before workers are forked, using `before_fork`:
167
+ In addition, there is an `before_refork` and `after_refork` hooks which are used only in [`fork_worker` mode](docs/fork_worker.md),
168
+ when the worker 0 child process forks a grandchild worker:
158
169
 
159
170
  ```ruby
160
- # config/puma.rb
161
- before_fork do
162
- # configuration here
171
+ before_refork do
172
+ # Used only when fork_worker mode is enabled. Add code to run inside the Puma worker 0
173
+ # child process before it forks a grandchild worker.
163
174
  end
164
175
  ```
165
176
 
166
- You can also specify a block to be run after puma is booted using `on_booted`:
177
+ ```ruby
178
+ after_refork do
179
+ # Used only when fork_worker mode is enabled. Add code to run inside the Puma worker 0
180
+ # child process after it forks a grandchild worker.
181
+ end
182
+ ```
183
+
184
+ Importantly, note the following considerations when Ruby forks a child process:
185
+
186
+ 1. File descriptors such as network sockets **are** copied from the parent to the forked
187
+ child process. Dual-use of the same sockets by parent and child will result in I/O conflicts
188
+ such as `SocketError`, `Errno::EPIPE`, and `EOFError`.
189
+ 2. Background Ruby threads, including threads used by various third-party gems for connection
190
+ monitoring, etc., are **not** copied to the child process. Often this does not cause
191
+ immediate problems until a third-party connection goes down, at which point there will
192
+ be no supervisor to reconnect it.
193
+
194
+ Therefore, we recommend the following:
195
+
196
+ 1. If possible, do not establish any socket connections (HTTP, database connections, etc.)
197
+ inside Puma's master process when booting.
198
+ 2. If (1) is not possible, use `before_fork` and `before_refork` to disconnect the parent's socket
199
+ connections when forking, so that they are not accidentally copied to the child process.
200
+ 3. Use `before_worker_boot` to restart any background threads on the forked child.
201
+ 4. Use `after_refork` to restart any background threads on the parent.
202
+
203
+ #### Master process lifecycle hooks
204
+
205
+ Puma's configuration DSL provides master process lifecycle hooks `after_booted`, `before_restart`, and `after_stopped`
206
+ which may be used to specify code blocks to run on each event:
167
207
 
168
208
  ```ruby
169
209
  # config/puma.rb
170
- on_booted do
171
- # configuration here
210
+ after_booted do
211
+ # Add code to run in the Puma master process after it boots,
212
+ # and also after a phased restart completes.
213
+ end
214
+
215
+ before_restart do
216
+ # Add code to run in the Puma master process when it receives
217
+ # a restart command but before it restarts.
218
+ end
219
+
220
+ after_stopped do
221
+ # Add code to run in the Puma master process when it receives
222
+ # a stop command but before it shuts down.
172
223
  end
173
224
  ```
174
225
 
175
226
  ### Error handling
176
227
 
177
- If puma encounters an error outside of the context of your application, it will respond with a 500 and a simple
228
+ If Puma encounters an error outside of the context of your application, it will respond with a 400/500 and a simple
178
229
  textual error message (see `Puma::Server#lowlevel_error` or [server.rb](https://github.com/puma/puma/blob/master/lib/puma/server.rb)).
179
230
  You can specify custom behavior for this scenario. For example, you can report the error to your third-party
180
231
  error-tracking service (in this example, [rollbar](https://rollbar.com)):
181
232
 
182
233
  ```ruby
183
- lowlevel_error_handler do |e|
184
- Rollbar.critical(e)
185
- [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"]]
234
+ lowlevel_error_handler do |e, env, status|
235
+ if status == 400
236
+ message = "The server could not process the request due to an error, such as an incorrectly typed URL, malformed syntax, or a URL that contains illegal characters.\n"
237
+ else
238
+ message = "An error has occurred, and engineers have been informed. Please reload the page. If you continue to have problems, contact support@example.com\n"
239
+ Rollbar.critical(e)
240
+ end
241
+
242
+ [status, {}, [message]]
186
243
  end
187
244
  ```
188
245
 
@@ -249,7 +306,7 @@ $ puma -b ssl://localhost:9292 -b tcp://localhost:9393 -C config/use_local_host.
249
306
 
250
307
  #### Controlling SSL Cipher Suites
251
308
 
252
- To use or avoid specific SSL cipher suites, use `ssl_cipher_filter` or `ssl_cipher_list` options.
309
+ To use or avoid specific SSL ciphers for TLSv1.2 and below, use `ssl_cipher_filter` or `ssl_cipher_list` options.
253
310
 
254
311
  ##### Ruby:
255
312
 
@@ -263,6 +320,14 @@ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&ssl_cipher_fil
263
320
  $ 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'
264
321
  ```
265
322
 
323
+ To configure the available TLSv1.3 ciphersuites, use `ssl_ciphersuites` option (not available for JRuby).
324
+
325
+ ##### Ruby:
326
+
327
+ ```
328
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&ssl_ciphersuites=TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256'
329
+ ```
330
+
266
331
  See https://www.openssl.org/docs/man1.1.1/man1/ciphers.html for cipher filter format and full list of cipher suites.
267
332
 
268
333
  Disable TLS v1 with the `no_tlsv1` option:
@@ -279,7 +344,7 @@ To enable verification flags offered by OpenSSL, use `verification_flags` (not a
279
344
  $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&verification_flags=PARTIAL_CHAIN'
280
345
  ```
281
346
 
282
- You can also set multiple verification flags (by separating them with coma):
347
+ You can also set multiple verification flags (by separating them with a comma):
283
348
 
284
349
  ```
285
350
  $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&verification_flags=PARTIAL_CHAIN,CRL_CHECK'
@@ -372,19 +437,6 @@ Some platforms do not support all Puma features.
372
437
  * **Windows**: Cluster mode is not supported due to a lack of fork(2).
373
438
  * **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.
374
439
 
375
- ## Known Bugs
376
-
377
- 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:
378
-
379
- ```ruby
380
- if %w(2.2.7 2.2.8 2.2.9 2.2.10 2.3.4 2.4.1).include? RUBY_VERSION
381
- begin
382
- require 'stopgap_13632'
383
- rescue LoadError
384
- end
385
- end
386
- ```
387
-
388
440
  ## Deployment
389
441
 
390
442
  * Puma has support for Capistrano with an [external gem](https://github.com/seuros/capistrano-puma).
@@ -410,6 +462,7 @@ Community guides:
410
462
  * [puma-plugin-statsd](https://github.com/yob/puma-plugin-statsd) — send Puma metrics to statsd
411
463
  * [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.
412
464
  * [puma-plugin-telemetry](https://github.com/babbel/puma-plugin-telemetry) - telemetry plugin for Puma offering various targets to publish
465
+ * [puma-acme](https://github.com/anchordotdev/puma-acme) - automatic SSL/HTTPS certificate provisioning and setup
413
466
 
414
467
  ### Monitoring
415
468
 
data/docs/fork_worker.md CHANGED
@@ -22,10 +22,20 @@ The `fork_worker` option allows your application to be initialized only once for
22
22
 
23
23
  You can trigger a refork by sending the cluster the `SIGURG` signal or running the `pumactl refork` command at any time. A refork will also automatically trigger once, after a certain number of requests have been processed by worker 0 (default 1000). To configure the number of requests before the auto-refork, pass a positive integer argument to `fork_worker` (e.g., `fork_worker 1000`), or `0` to disable.
24
24
 
25
+ ### Usage Considerations
26
+
27
+ - `fork_worker` introduces new `before_refork` and `after_refork` configuration hooks. Note the following:
28
+ - When initially forking the parent process to the worker 0 child, `before_fork` will trigger on the parent process and `before_worker_boot` will trigger on the worker 0 child as normal.
29
+ - When forking the worker 0 child to grandchild workers, `before_refork` and `after_refork` will trigger on the worker 0 child, and `before_worker_boot` will trigger on each grandchild worker.
30
+ - For clarity, `before_fork` does not trigger on worker 0, and `after_refork` does not trigger on the grandchild.
31
+ - As a general migration guide:
32
+ - Copy any logic within your existing `before_fork` hook to the `before_refork` hook.
33
+ - Consider to copy logic from your `before_worker_boot` hook to the `after_refork` hook, if it is needed to reset the state of worker 0 after it forks.
34
+
25
35
  ### Limitations
26
36
 
27
37
  - 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
38
 
29
39
  - 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.
30
40
 
31
- In a cluster with `n` workers, a normal phased restart stops and restarts workers one by one while the application is loaded in each process, so `n-1` workers are available serving requests during the restart. In a phased restart in fork-worker mode, the application is first loaded in worker 0 while `n-1` workers are available, then worker 0 remains stopped while the rest of the workers are reloaded one by one, leaving only `n-2` workers to be available for a brief period of time. Reloading the rest of the workers should be quick because the application is preloaded at that point, but there may be situations where it can take longer (slow clients, long-running application code, slow worker-fork hooks, etc).
41
+ - In a cluster with `n` workers, a normal phased restart stops and restarts workers one by one while the application is loaded in each process, so `n-1` workers are available serving requests during the restart. In a phased restart in fork-worker mode, the application is first loaded in worker 0 while `n-1` workers are available, then worker 0 remains stopped while the rest of the workers are reloaded one by one, leaving only `n-2` workers to be available for a brief period of time. Reloading the rest of the workers should be quick because the application is preloaded at that point, but there may be situations where it can take longer (slow clients, long-running application code, slow worker-fork hooks, etc).
@@ -0,0 +1,54 @@
1
+ # Java Options
2
+
3
+ `System Properties` or `Environment Variables` can be used to change Puma's
4
+ default configuration for its Java extension. The provided values are evaluated
5
+ during initialization, and changes while running the app have no effect.
6
+ Moreover, default values may be used in case of invalid inputs.
7
+
8
+ ## Supported Options
9
+
10
+ | ENV Name | Default Value | Validation |
11
+ |------------------------------|:-------------:|:------------------------:|
12
+ | PUMA_QUERY_STRING_MAX_LENGTH | 1024 * 10 | Positive natural number |
13
+ | PUMA_REQUEST_PATH_MAX_LENGTH | 8192 | Positive natural number |
14
+ | PUMA_REQUEST_URI_MAX_LENGTH | 1024 * 12 | Positive natural number |
15
+ | PUMA_SKIP_SIGUSR2 | nil | n/a |
16
+
17
+ ## Examples
18
+
19
+ ### Invalid inputs
20
+
21
+ An empty string will be handled as missing, and the default value will be used instead.
22
+ Puma will print an error message for other invalid values.
23
+
24
+ ```
25
+ foo@bar:~/puma$ PUMA_QUERY_STRING_MAX_LENGTH=abc PUMA_REQUEST_PATH_MAX_LENGTH='' PUMA_REQUEST_URI_MAX_LENGTH=0 bundle exec bin/puma test/rackup/hello.ru
26
+
27
+ The value 0 for PUMA_REQUEST_URI_MAX_LENGTH is invalid. Using default value 12288 instead.
28
+ The value abc for PUMA_QUERY_STRING_MAX_LENGTH is invalid. Using default value 10240 instead.
29
+ Puma starting in single mode...
30
+ ```
31
+
32
+ ### Valid inputs
33
+
34
+ ```
35
+ foo@bar:~/puma$ PUMA_REQUEST_PATH_MAX_LENGTH=9 bundle exec bin/puma test/rackup/hello.ru
36
+
37
+ Puma starting in single mode...
38
+ ```
39
+ ```
40
+ foo@bar:~ export path=/123456789 # 10 chars
41
+ foo@bar:~ curl "http://localhost:9292${path}"
42
+
43
+ Puma caught this error: HTTP element REQUEST_PATH is longer than the 9 allowed length. (Puma::HttpParserError)
44
+
45
+ foo@bar:~ export path=/12345678 # 9 chars
46
+ foo@bar:~ curl "http://localhost:9292${path}"
47
+ Hello World
48
+ ```
49
+
50
+ ### Java Flight Recorder Compatibility
51
+
52
+ Unfortunately Java Flight Recorder uses `SIGUSR2` internally. If you wish to
53
+ use JFR, turn off Puma's trapping of `SIGUSR2` by setting the environment variable
54
+ `PUMA_SKIP_SIGUSR2` to any value.
data/docs/kubernetes.md CHANGED
@@ -8,10 +8,11 @@ In general running Puma in Kubernetes works as-is, no special configuration is n
8
8
 
9
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
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
11
+ A basic Dockerfile example:
12
+
13
+ ```Dockerfile
14
+ FROM ruby:3.4.5-alpine # can be updated to newer ruby versions
15
+ RUN apk update && apk add build-base # and any other packages you need
15
16
 
16
17
  # Only rebuild gem bundle if Gemfile changes
17
18
  COPY Gemfile Gemfile.lock ./
@@ -26,7 +27,8 @@ CMD bundle exec rackup -o 0.0.0.0
26
27
  ```
27
28
 
28
29
  A sample `deployment.yaml`:
29
- ```
30
+
31
+ ```yaml
30
32
  ---
31
33
  apiVersion: apps/v1
32
34
  kind: Deployment
@@ -47,7 +49,7 @@ spec:
47
49
  image: <your image here>
48
50
  ports:
49
51
  - containerPort: 9292
50
- ```
52
+ ```
51
53
 
52
54
  ## Graceful shutdown and pod termination
53
55
 
@@ -69,7 +71,7 @@ More discussions and links to relevant articles can be found in https://github.c
69
71
 
70
72
  With containerization, you will have to make a decision about how "big" to make each pod. Should you run 2 pods with 50 workers each? 25 pods, each with 4 workers? 100 pods, with each Puma running in single mode? Each scenario represents the same total amount of capacity (100 Puma processes that can respond to requests), but there are tradeoffs to make.
71
73
 
72
- * Worker counts should be somewhere between 4 and 32 in most cases. You want more than 4 in order to minimize time spent in request queueing for a free Puma worker, but probably less than ~32 because otherwise autoscaling is working in too large of an increment or they probably won't fit very well into your nodes.
74
+ * Worker counts should be somewhere between 4 and 32 in most cases. You want more than 4 in order to minimize time spent in request queueing for a free Puma worker, but probably less than ~32 because otherwise autoscaling is working in too large of an increment or they probably won't fit very well into your nodes. In any queueing system, queue time is proportional to 1/n, where n is the number of things pulling from the queue. Each pod will have its own request queue (i.e., the socket backlog). If you have 4 pods with 1 worker each (4 request queues), wait times are, proportionally, about 4 times higher than if you had 1 pod with 4 workers (1 request queue).
73
75
  * Unless you have a very I/O-heavy application (50%+ time spent waiting on IO), use the default thread count (5 for MRI). Using higher numbers of threads with low I/O wait (<50%) will lead to additional request queueing time (latency!) and additional memory usage.
74
76
  * More processes per pod reduces memory usage per process, because of copy-on-write memory and because the cost of the single master process is "amortized" over more child processes.
75
77
  * Don't run less than 4 processes per pod if you can. Low numbers of processes per pod will lead to high request queueing, which means you will have to run more pods.
data/docs/plugins.md CHANGED
@@ -36,3 +36,7 @@ object that is useful for additional configuration.
36
36
 
37
37
  Public methods in [`Puma::Plugin`](../lib/puma/plugin.rb) are treated as a
38
38
  public API for plugins.
39
+
40
+ ## Binder hooks
41
+
42
+ There's `Puma::Binder#before_parse` method that allows to add proc to run before the body of `Puma::Binder#parse`. Example of usage can be found in [that repository](https://github.com/anchordotdev/puma-acme/blob/v0.1.3/lib/puma/acme/plugin.rb#L97-L118) (`before_parse_hook` could be renamed `before_parse`, making monkey patching of [binder.rb](https://github.com/anchordotdev/puma-acme/blob/v0.1.3/lib/puma/acme/binder.rb) is unnecessary).
data/docs/restart.md CHANGED
@@ -27,8 +27,9 @@ Any of the following will cause a Puma server to perform a hot restart:
27
27
 
28
28
  ### Additional notes
29
29
 
30
+ * The newly started Puma process changes its current working directory to the directory specified by the `directory` option. If `directory` is set to symlink, this is automatically re-evaluated, so this mechanism can be used to upgrade the application.
30
31
  * Only one version of the application is running at a time.
31
- * `on_restart` is invoked just before the server shuts down. This can be used to clean up resources (like long-lived database connections) gracefully. Since Ruby 2.0, it is not typically necessary to explicitly close file descriptors on restart. This is because any file descriptor opened by Ruby will have the `FD_CLOEXEC` flag set, meaning that file descriptors are closed on `exec`. `on_restart` is useful, though, if your application needs to perform any more graceful protocol-specific shutdown procedures before closing connections.
32
+ * `before_restart` is invoked just before the server shuts down. This can be used to clean up resources (like long-lived database connections) gracefully. Since Ruby 2.0, it is not typically necessary to explicitly close file descriptors on restart. This is because any file descriptor opened by Ruby will have the `FD_CLOEXEC` flag set, meaning that file descriptors are closed on `exec`. `before_restart` is useful, though, if your application needs to perform any more graceful protocol-specific shutdown procedures before closing connections.
32
33
 
33
34
  ## Phased restart
34
35
 
@@ -58,7 +59,7 @@ Any of the following will cause a Puma server to perform a phased restart:
58
59
 
59
60
  * When a phased restart begins, the Puma master process changes its current working directory to the directory specified by the `directory` option. If `directory` is set to symlink, this is automatically re-evaluated, so this mechanism can be used to upgrade the application.
60
61
  * On a single server, it's possible that two versions of the application are running concurrently during a phased restart.
61
- * `on_restart` is not invoked
62
+ * `before_restart` is not invoked
62
63
  * Phased restarts can be slow for Puma clusters with many workers. Hot restarts often complete more quickly, but at the cost of increased latency during the restart.
63
64
  * Phased restarts cannot be used to upgrade any gems loaded by the Puma master process, including `puma` itself, anything in `extra_runtime_dependencies`, or dependencies thereof. Upgrading other gems is safe.
64
65
  * If you remove the gems from old releases as part of your deployment strategy, there are additional considerations. Do not put any gems into `extra_runtime_dependencies` that have native extensions or have dependencies that have native extensions (one common example is `puma_worker_killer` and its dependency on `ffi`). Workers will fail on boot during a phased restart. The underlying issue is recorded in [an issue on the rubygems project](https://github.com/rubygems/rubygems/issues/4004). Hot restarts are your only option here if you need these dependencies.
data/docs/signals.md CHANGED
@@ -17,13 +17,13 @@ $ ps aux | grep tail
17
17
  schneems 87152 0.0 0.0 2432772 492 s032 S+ 12:46PM 0:00.00 tail -f my.log
18
18
  ```
19
19
 
20
- You can send a signal in Ruby using the [Process module](https://www.ruby-doc.org/core-2.1.1/Process.html#kill-method):
20
+ You can send a signal in Ruby using the [Process module](https://docs.ruby-lang.org/en/master/Process.html#method-c-kill):
21
21
 
22
22
  ```
23
23
  $ irb
24
24
  > puts pid
25
25
  => 87152
26
- Process.detach(pid) # https://ruby-doc.org/core-2.1.1/Process.html#method-c-detach
26
+ Process.detach(pid) # https://docs.ruby-lang.org/en/master/Process.html#method-c-detach
27
27
  Process.kill("TERM", pid)
28
28
  ```
29
29
 
@@ -54,12 +54,12 @@ puma configuration file reloaded, if there is one
54
54
  puma configuration file reloaded, if there is one
55
55
 
56
56
  before_fork
57
- on_worker_fork
57
+ before_worker_fork
58
58
  after_worker_fork
59
59
 
60
60
  Gemfile in context
61
61
 
62
- on_worker_boot
62
+ before_worker_boot
63
63
 
64
64
  Code of the app is loaded and running
65
65
  ```
@@ -67,18 +67,18 @@ Code of the app is loaded and running
67
67
  ### Send USR2
68
68
 
69
69
  ```
70
- on_worker_shutdown
71
- on_restart
70
+ before_worker_shutdown
71
+ before_restart
72
72
 
73
73
  puma configuration file reloaded, if there is one
74
74
 
75
75
  before_fork
76
- on_worker_fork
76
+ before_worker_fork
77
77
  after_worker_fork
78
78
 
79
79
  Gemfile in context
80
80
 
81
- on_worker_boot
81
+ before_worker_boot
82
82
 
83
83
  Code of the app is loaded and running
84
84
  ```
@@ -86,13 +86,13 @@ Code of the app is loaded and running
86
86
  ### Send USR1
87
87
 
88
88
  ```
89
- on_worker_shutdown
90
- on_worker_fork
89
+ before_worker_shutdown
90
+ before_worker_fork
91
91
  after_worker_fork
92
92
 
93
93
  Gemfile in context
94
94
 
95
- on_worker_boot
95
+ before_worker_boot
96
96
 
97
97
  Code of the app is loaded and running
98
98
  ```
data/docs/stats.md CHANGED
@@ -55,12 +55,18 @@ end
55
55
 
56
56
  When Puma runs in single mode, these stats are available at the top level. When Puma runs in cluster mode, these stats are available within the `worker_status` array in a hash labeled `last_status`, in an array of hashes where one hash represents each worker.
57
57
 
58
- * backlog: requests that are waiting for an available thread to be available. if this is above 0, you need more capacity [always true?]
59
- * running: how many threads are running
60
- * pool_capacity: the number of requests that the server is capable of taking right now. For example, if the number is 5, then it means there are 5 threads sitting idle ready to take a request. If one request comes in, then the value would be 4 until it finishes processing. If the minimum threads allowed is zero, this number will still have a maximum value of the maximum threads allowed.
58
+ * backlog: requests that are waiting for an available thread to be available. if this is frequently above 0, you need more capacity.
59
+ * running: how many threads are spawned. A spawned thread may be busy processing a request or waiting for a new request. If `min_threads` and `max_threads` are set to the same number,
60
+ this will be a never-changing number (other than rare cases when a thread dies, etc).
61
+ * busy_threads: `running` - `how many threads are waiting to receive work` + `how many requests are waiting for a thread to pick them up`.
62
+ this is a "wholistic" stat reflecting the overall current state of work to be done and the capacity to do it.
63
+ * pool_capacity: `how many threads are waiting to receive work` + `max_threads` - `running`. In a typical configuration where `min_threads`
64
+ and `max_threads` are configured to the same number, this is simply `how many threads are waiting to receive work`. This number exists only as a stat
65
+ and is not used for any internal decisions, unlike `busy_threads`, which is usually a more useful stat.
61
66
  * max_threads: the maximum number of threads Puma is configured to spool per worker
62
67
  * requests_count: the number of requests this worker has served since starting
63
-
68
+ * reactor_max: the maximum observed number of requests held in Puma's "reactor" which is used for asyncronously buffering request bodies. This stat is reset on every call, so it's the maximum value observed since the last stat call.
69
+ * backlog_max: the maximum number of requests that have been fully buffered by the reactor and placed in a ready queue, but have not yet been picked up by a server thread. This stat is reset on every call, so it's the maximum value observed since the last stat call.
64
70
 
65
71
  ### cluster mode
66
72
 
data/docs/systemd.md CHANGED
@@ -51,7 +51,7 @@ ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/puma.rb
51
51
  # Variant: Rails start.
52
52
  # ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/config/puma.rb ../config.ru
53
53
 
54
- # Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub
54
+ # Variant: Use `bundle exec puma` instead of binstub
55
55
  # Variant: Specify directives inline.
56
56
  # ExecStart=/<FULLPATH>/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem
57
57
 
@@ -72,13 +72,11 @@ systemd and Puma also support socket activation, where systemd opens the
72
72
  listening socket(s) in advance and provides them to the Puma master process on
73
73
  startup. Among other advantages, this keeps listening sockets open across puma
74
74
  restarts and achieves graceful restarts, including when upgraded Puma, and is
75
- compatible with both clustered mode and application preload.
75
+ compatible with both cluster mode and application preload.
76
76
 
77
77
  **Note:** Any wrapper scripts which `exec`, or other indirections in `ExecStart`
78
78
  may result in activated socket file descriptors being closed before reaching the
79
- puma master process. For example, if using `bundle exec`, pass the
80
- `--keep-file-descriptors` flag. `bundle exec` can be avoided by using a `puma`
81
- executable generated by `bundle binstubs puma`. This is tracked in [#1499].
79
+ puma master process.
82
80
 
83
81
  **Note:** Socket activation doesn't currently work on JRuby. This is tracked in
84
82
  [#1367].
@@ -101,9 +99,11 @@ ListenStream=0.0.0.0:9293
101
99
  # ListenStream=/run/puma.sock
102
100
 
103
101
  # Socket options matching Puma defaults
104
- NoDelay=true
105
102
  ReusePort=true
106
103
  Backlog=1024
104
+ # Enable this if you're using Puma with the "low_latency" option, read more in Puma DSL docs and systemd docs:
105
+ # https://www.freedesktop.org/software/systemd/man/latest/systemd.socket.html#NoDelay=
106
+ # NoDelay=true
107
107
 
108
108
  [Install]
109
109
  WantedBy=sockets.target
@@ -241,6 +241,13 @@ cap $stage puma:start --dry-run
241
241
  cap $stage puma:stop --dry-run
242
242
  ~~~~
243
243
 
244
+ ### Disabling Puma Systemd Integration
245
+
246
+ If you would like to disable Puma's systemd integration, for example if you handle it elsewhere
247
+ in your code yourself, simply set the the environment variable `PUMA_SKIP_SYSTEMD` to any value.
248
+
249
+
250
+
244
251
  [Restart]: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Restart=
245
252
  [#1367]: https://github.com/puma/puma/issues/1367
246
253
  [#1499]: https://github.com/puma/puma/issues/1499
@@ -10,11 +10,13 @@ end
10
10
 
11
11
  unless ENV["PUMA_DISABLE_SSL"]
12
12
  # don't use pkg_config('openssl') if '--with-openssl-dir' is used
13
- has_openssl_dir = dir_config('openssl').any?
13
+ has_openssl_dir = dir_config('openssl').any? ||
14
+ RbConfig::CONFIG['configure_args']&.include?('openssl')
15
+
14
16
  found_pkg_config = !has_openssl_dir && pkg_config('openssl')
15
17
 
16
18
  found_ssl = if !$mingw && found_pkg_config
17
- puts 'using OpenSSL pkgconfig (openssl.pc)'
19
+ puts '──── Using OpenSSL pkgconfig (openssl.pc) ────'
18
20
  true
19
21
  elsif have_library('libcrypto', 'BIO_read') && have_library('libssl', 'SSL_CTX_new')
20
22
  true
@@ -29,45 +31,35 @@ unless ENV["PUMA_DISABLE_SSL"]
29
31
  if found_ssl
30
32
  have_header "openssl/bio.h"
31
33
 
32
- # below is yes for 1.0.2 & later
33
- have_func "DTLS_method" , "openssl/ssl.h"
34
- have_func "SSL_CTX_set_session_cache_mode(NULL, 0)", "openssl/ssl.h"
34
+ ssl_h = "openssl/ssl.h".freeze
35
+
36
+ puts "\n──── Below are yes for 1.0.2 & later ────"
37
+ have_func "DTLS_method" , ssl_h
38
+ have_func "SSL_CTX_set_session_cache_mode(NULL, 0)", ssl_h
35
39
 
36
- # below are yes for 1.1.0 & later
37
- have_func "TLS_server_method" , "openssl/ssl.h"
38
- have_func "SSL_CTX_set_min_proto_version(NULL, 0)" , "openssl/ssl.h"
40
+ puts "\n──── Below are yes for 1.1.0 & later ────"
41
+ have_func "TLS_server_method" , ssl_h
42
+ have_func "SSL_CTX_set_min_proto_version(NULL, 0)" , ssl_h
39
43
 
40
- have_func "X509_STORE_up_ref"
41
- have_func "SSL_CTX_set_ecdh_auto(NULL, 0)" , "openssl/ssl.h"
44
+ puts "\n──── Below is yes for 1.1.0 and later, but isn't documented until 3.0.0 ────"
45
+ # https://github.com/openssl/openssl/blob/OpenSSL_1_1_0/include/openssl/ssl.h#L1159
46
+ have_func "SSL_CTX_set_dh_auto(NULL, 0)" , ssl_h
42
47
 
43
- # below exists in 1.1.0 and later, but isn't documented until 3.0.0
44
- have_func "SSL_CTX_set_dh_auto(NULL, 0)" , "openssl/ssl.h"
48
+ puts "\n──── Below is yes for 1.1.1 & later ────"
49
+ have_func "SSL_CTX_set_ciphersuites(NULL, \"\")" , ssl_h
45
50
 
46
- # below is yes for 3.0.0 & later
47
- have_func "SSL_get1_peer_certificate" , "openssl/ssl.h"
51
+ puts "\n──── Below is yes for 3.0.0 & later ────"
52
+ have_func "SSL_get1_peer_certificate" , ssl_h
48
53
 
49
- # Random.bytes available in Ruby 2.5 and later, Random::DEFAULT deprecated in 3.0
50
- if Random.respond_to?(:bytes)
51
- $defs.push "-DHAVE_RANDOM_BYTES"
52
- puts "checking for Random.bytes... yes"
53
- else
54
- puts "checking for Random.bytes... no"
55
- end
54
+ puts ''
56
55
  end
57
56
  end
58
57
 
59
58
  if ENV["PUMA_MAKE_WARNINGS_INTO_ERRORS"]
60
59
  # Make all warnings into errors
61
60
  # Except `implicit-fallthrough` since most failures comes from ragel state machine generated code
62
- if respond_to?(:append_cflags, true) # Ruby 2.5 and later
63
- append_cflags(config_string('WERRORFLAG') || '-Werror')
64
- append_cflags '-Wno-implicit-fallthrough'
65
- else
66
- # flag may not exist on some platforms, -Werror may not be defined on some platforms, but
67
- # works with all in current CI
68
- $CFLAGS << " #{config_string('WERRORFLAG') || '-Werror'}"
69
- $CFLAGS << ' -Wno-implicit-fallthrough'
70
- end
61
+ append_cflags(config_string('WERRORFLAG') || '-Werror')
62
+ append_cflags '-Wno-implicit-fallthrough'
71
63
  end
72
64
 
73
65
  create_makefile("puma/puma_http11")