puma 2.2.0 → 8.0.2

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 (136) hide show
  1. checksums.yaml +7 -0
  2. data/History.md +3334 -0
  3. data/LICENSE +23 -20
  4. data/README.md +387 -100
  5. data/bin/puma-wild +25 -0
  6. data/bin/pumactl +1 -1
  7. data/docs/5.0-Upgrade.md +98 -0
  8. data/docs/6.0-Upgrade.md +56 -0
  9. data/docs/7.0-Upgrade.md +52 -0
  10. data/docs/8.0-Upgrade.md +100 -0
  11. data/docs/architecture.md +74 -0
  12. data/docs/compile_options.md +55 -0
  13. data/docs/deployment.md +137 -0
  14. data/docs/fork_worker.md +41 -0
  15. data/docs/grpc.md +62 -0
  16. data/docs/images/favicon.svg +1 -0
  17. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  18. data/docs/images/puma-connection-flow.png +0 -0
  19. data/docs/images/puma-general-arch.png +0 -0
  20. data/docs/images/running-puma.svg +1 -0
  21. data/docs/images/standard-logo.svg +1 -0
  22. data/docs/java_options.md +54 -0
  23. data/docs/jungle/README.md +9 -0
  24. data/docs/jungle/rc.d/README.md +74 -0
  25. data/docs/jungle/rc.d/puma +61 -0
  26. data/docs/jungle/rc.d/puma.conf +10 -0
  27. data/docs/kubernetes.md +73 -0
  28. data/docs/nginx.md +5 -5
  29. data/docs/plugins.md +42 -0
  30. data/docs/rails_dev_mode.md +28 -0
  31. data/docs/restart.md +65 -0
  32. data/docs/signals.md +98 -0
  33. data/docs/stats.md +148 -0
  34. data/docs/systemd.md +253 -0
  35. data/docs/testing_benchmarks_local_files.md +150 -0
  36. data/docs/testing_test_rackup_ci_files.md +36 -0
  37. data/ext/puma_http11/PumaHttp11Service.java +2 -2
  38. data/ext/puma_http11/extconf.rb +59 -2
  39. data/ext/puma_http11/http11_parser.c +319 -487
  40. data/ext/puma_http11/http11_parser.h +15 -13
  41. data/ext/puma_http11/http11_parser.java.rl +64 -94
  42. data/ext/puma_http11/http11_parser.rl +27 -24
  43. data/ext/puma_http11/http11_parser_common.rl +8 -8
  44. data/ext/puma_http11/mini_ssl.c +696 -38
  45. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  46. data/ext/puma_http11/org/jruby/puma/EnvKey.java +241 -0
  47. data/ext/puma_http11/org/jruby/puma/Http11.java +241 -145
  48. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +174 -221
  49. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +413 -193
  50. data/ext/puma_http11/puma_http11.c +183 -175
  51. data/lib/puma/app/status.rb +77 -29
  52. data/lib/puma/binder.rb +349 -119
  53. data/lib/puma/cli.rb +163 -801
  54. data/lib/puma/client.rb +627 -144
  55. data/lib/puma/client_env.rb +171 -0
  56. data/lib/puma/cluster/worker.rb +183 -0
  57. data/lib/puma/cluster/worker_handle.rb +127 -0
  58. data/lib/puma/cluster.rb +634 -0
  59. data/lib/puma/cluster_accept_loop_delay.rb +91 -0
  60. data/lib/puma/commonlogger.rb +115 -0
  61. data/lib/puma/configuration.rb +420 -198
  62. data/lib/puma/const.rb +266 -115
  63. data/lib/puma/control_cli.rb +219 -120
  64. data/lib/puma/detect.rb +56 -2
  65. data/lib/puma/dsl.rb +1562 -0
  66. data/lib/puma/error_logger.rb +115 -0
  67. data/lib/puma/events.rb +44 -60
  68. data/lib/puma/io_buffer.rb +48 -5
  69. data/lib/puma/jruby_restart.rb +2 -51
  70. data/lib/puma/json_serialization.rb +96 -0
  71. data/lib/puma/launcher/bundle_pruner.rb +102 -0
  72. data/lib/puma/launcher.rb +501 -0
  73. data/lib/puma/log_writer.rb +153 -0
  74. data/lib/puma/minissl/context_builder.rb +96 -0
  75. data/lib/puma/minissl.rb +355 -37
  76. data/lib/puma/null_io.rb +79 -12
  77. data/lib/puma/plugin/systemd.rb +90 -0
  78. data/lib/puma/plugin/tmp_restart.rb +36 -0
  79. data/lib/puma/plugin.rb +111 -0
  80. data/lib/puma/rack/builder.rb +297 -0
  81. data/lib/puma/rack/urlmap.rb +93 -0
  82. data/lib/puma/rack_default.rb +21 -4
  83. data/lib/puma/reactor.rb +102 -133
  84. data/lib/puma/response.rb +532 -0
  85. data/lib/puma/runner.rb +211 -0
  86. data/lib/puma/sd_notify.rb +146 -0
  87. data/lib/puma/server.rb +582 -445
  88. data/lib/puma/server_plugin_control.rb +32 -0
  89. data/lib/puma/single.rb +72 -0
  90. data/lib/puma/state_file.rb +69 -0
  91. data/lib/puma/thread_pool.rb +389 -57
  92. data/lib/puma/util.rb +125 -0
  93. data/lib/puma.rb +80 -6
  94. data/lib/rack/handler/puma.rb +125 -47
  95. data/tools/Dockerfile +26 -0
  96. data/tools/trickletest.rb +44 -0
  97. metadata +88 -148
  98. data/COPYING +0 -55
  99. data/Gemfile +0 -10
  100. data/History.txt +0 -302
  101. data/Manifest.txt +0 -60
  102. data/Rakefile +0 -165
  103. data/TODO +0 -5
  104. data/docs/config.md +0 -0
  105. data/ext/puma_http11/ext_help.h +0 -15
  106. data/ext/puma_http11/io_buffer.c +0 -154
  107. data/lib/puma/accept_nonblock.rb +0 -23
  108. data/lib/puma/capistrano.rb +0 -34
  109. data/lib/puma/compat.rb +0 -11
  110. data/lib/puma/daemon_ext.rb +0 -20
  111. data/lib/puma/delegation.rb +0 -11
  112. data/lib/puma/java_io_buffer.rb +0 -45
  113. data/lib/puma/rack_patch.rb +0 -25
  114. data/puma.gemspec +0 -45
  115. data/test/test_app_status.rb +0 -88
  116. data/test/test_cli.rb +0 -171
  117. data/test/test_config.rb +0 -16
  118. data/test/test_http10.rb +0 -27
  119. data/test/test_http11.rb +0 -126
  120. data/test/test_integration.rb +0 -154
  121. data/test/test_iobuffer.rb +0 -38
  122. data/test/test_minissl.rb +0 -25
  123. data/test/test_null_io.rb +0 -31
  124. data/test/test_persistent.rb +0 -238
  125. data/test/test_puma_server.rb +0 -224
  126. data/test/test_rack_handler.rb +0 -10
  127. data/test/test_rack_server.rb +0 -141
  128. data/test/test_thread_pool.rb +0 -146
  129. data/test/test_unix_socket.rb +0 -39
  130. data/test/test_ws.rb +0 -89
  131. data/tools/jungle/init.d/README.md +0 -54
  132. data/tools/jungle/init.d/puma +0 -332
  133. data/tools/jungle/init.d/run-puma +0 -3
  134. data/tools/jungle/upstart/README.md +0 -61
  135. data/tools/jungle/upstart/puma-manager.conf +0 -31
  136. data/tools/jungle/upstart/puma.conf +0 -52
data/bin/puma-wild ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (c) 2014 Evan Phoenix
4
+ #
5
+
6
+ require 'rubygems'
7
+
8
+ cli_arg = ARGV.shift
9
+
10
+ inc = ""
11
+
12
+ if cli_arg == "-I"
13
+ inc = ARGV.shift
14
+ $LOAD_PATH.concat inc.split(":")
15
+ end
16
+
17
+ module Puma; end
18
+
19
+ Puma.const_set(:WILD_ARGS, ["-I", inc])
20
+
21
+ require 'puma/cli'
22
+
23
+ cli = Puma::CLI.new ARGV
24
+
25
+ cli.run
data/bin/pumactl CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'puma/control_cli'
4
4
 
5
- cli = Puma::ControlCLI.new ARGV
5
+ cli = Puma::ControlCLI.new ARGV.dup
6
6
 
7
7
  begin
8
8
  cli.run
@@ -0,0 +1,98 @@
1
+ # Welcome to Puma 5: Spoony Bard.
2
+
3
+ ![Spoony Bard](https://i1.kym-cdn.com/entries/icons/original/000/006/385/Spoony_Bard.jpg "Spoony Bard")
4
+
5
+ >Note: Puma 5 now automatically uses `WEB_CONCURRENCY` env var if set see [this post for an explanation](https://github.com/puma/puma/issues/2393#issuecomment-702352208). If your memory use goes up after upgrading to Puma 5 it indicates you're now running with multiple workers (processes). You can decrease memory use by tuning this number to be lower.
6
+
7
+ Puma 5 brings new experimental performance features, a few quality-of-life features and loads of bugfixes. Here's what you should do:
8
+
9
+ 1. Review the Upgrade section below to see if any of 5.0's breaking changes will affect you.
10
+ 2. Upgrade to version 5.0 in your Gemfile and deploy.
11
+ 3. Try the new performance experiments outlined below and report your results back to the Puma issue tracker.
12
+
13
+ Puma 5 was named Spoony Bard by our newest supercontributor, [@wjordan](https://github.com/puma/puma/commits?author=wjordan). Will brought you one of our new perf features for this release, as well as [many other fixes and refactors.](https://github.com/puma/puma/commits?author=wjordan) If you'd like to name a Puma release in the future, take a look at [CONTRIBUTING.md](../CONTRIBUTING.md) and get started helping us out :)
14
+
15
+ Puma 5 also welcomes [@MSP-Greg](https://github.com/puma/puma/commits?author=MSP-Greg) as our newest committer. Greg has been instrumental in improving our CI setup and SSL features. Greg also [named our 4.3.0 release](https://github.com/puma/puma/releases/tag/v4.3.0): Mysterious Traveller.
16
+
17
+ ## What's New
18
+
19
+ Puma 5 contains three new "experimental" performance features for cluster-mode Pumas running on MRI.
20
+
21
+ If you try any of these features, please report your results to [our report issue](https://github.com/puma/puma/issues/2258).
22
+
23
+ Part of the reason we're calling them _experimental_ is because we're not sure if they'll actually have any benefit. People's workloads in the real world are often not what we anticipate, and synthetic benchmarks are usually not of any help in figuring out if a change will be beneficial or not.
24
+
25
+ We do not believe any of the new features will have a negative effect or impact the stability of your application. This is either a "it works" or "it does nothing" experiment.
26
+
27
+ If any of the features turn out to be particularly beneficial, we may make them defaults in future versions of Puma.
28
+
29
+ ### Lower latency, better throughput
30
+
31
+ From our friends at GitLab, the new experimental `wait_for_less_busy_worker` config option may reduce latency and improve throughput for high-load Puma apps on MRI. See the [pull request](https://github.com/puma/puma/pull/2079) for more discussion.
32
+
33
+ Users of this option should see reduced request queue latency and possibly less overall latency.
34
+
35
+ Add the following to your `puma.rb` to try it:
36
+
37
+ ```ruby
38
+ wait_for_less_busy_worker
39
+ # or
40
+ wait_for_less_busy_worker 0.001
41
+ ```
42
+
43
+ Production testing at GitLab suggests values between `0.001` and `0.010` are best.
44
+
45
+ ### Better memory usage
46
+
47
+ 5.0 brings two new options to your config which may improve memory usage.
48
+
49
+ #### nakayoshi_fork
50
+
51
+ `nakayoshi_fork` calls GC a handful of times and compacts the heap on Ruby 2.7+ before forking. This may reduce memory usage of Puma on MRI with preload enabled. It's inspired by [Koichi Sasada's work](https://github.com/ko1/nakayoshi_fork).
52
+
53
+ To use it, you can add this to your `puma.rb`:
54
+
55
+ ```ruby
56
+ nakayoshi_fork
57
+ ```
58
+
59
+ #### fork_worker
60
+
61
+ Puma 5 introduces an experimental new cluster-mode configuration option, `fork_worker` (`--fork-worker` from the CLI). This mode causes Puma to fork additional workers from worker 0, instead of directly from the master process:
62
+
63
+ ```
64
+ 10000 \_ puma 4.3.3 (tcp://0.0.0.0:9292) [puma]
65
+ 10001 \_ puma: cluster worker 0: 10000 [puma]
66
+ 10002 \_ puma: cluster worker 1: 10000 [puma]
67
+ 10003 \_ puma: cluster worker 2: 10000 [puma]
68
+ 10004 \_ puma: cluster worker 3: 10000 [puma]
69
+ ```
70
+
71
+ It is compatible with phased restarts. It also may improve memory usage because the worker process loads additional code after processing requests.
72
+
73
+ To learn more about using `refork` and `fork_worker`, see ['Fork Worker'](fork_worker.md).
74
+
75
+ ### What else is new?
76
+
77
+ * **Loads of bugfixes**.
78
+ * Faster phased restarts and worker timeouts.
79
+ * pumactl now has a `thread-backtraces` command to print thread backtraces, bringing thread backtrace printing to all platforms, not just *BSD and Mac. (#2053)
80
+ * Added incrementing `requests_count` to `Puma.stats`. (#2106)
81
+ * Faster phased restart and worker timeout. (#2220)
82
+ * Added `state_permission` to config DSL to set state file permissions (#2238)
83
+ * Ruby 2.2 support will be dropped in Puma 6. This is the final major release series for Ruby 2.2.
84
+
85
+ ## Upgrade
86
+
87
+ * Setting the `WEB_CONCURRENCY` environment variable will now configure the number of workers (processes) that Puma will boot and enable preloading of the application.
88
+ * If you did not explicitly set `environment` before, Puma now checks `RAILS_ENV` and will use that, if available in addition to `RACK_ENV`.
89
+ * If you have been using the `--control` CLI option, update your scripts to use `--control-url`.
90
+ * If you are using `worker_directory` in your config file, change it to `directory`.
91
+ * If you are running MRI, default thread count on Puma is now 5, not 16. This may change the amount of threads running in your threadpool. We believe 5 is a better default for most Ruby web applications on MRI. Higher settings increase latency by causing GVL contention.
92
+ * If you are using a worker count of more than 1, set using `WEB_CONCURRENCY`, Puma will now preload the application by default (disable with `preload_app! false`). We believe this is a better default, but may cause issues in non-Rails applications if you do not have the proper `before` and `after` fork hooks configured. See documentation for your framework. Rails users do not need to change anything. **Please note that it is not possible to use [the phased restart](restart.md) with preloading.**
93
+ * tcp mode and daemonization have been removed without replacement. For daemonization, please use a modern process management solution, such as systemd or monit.
94
+ * `connected_port` was renamed to `connected_ports` and now returns an Array, not an Integer.
95
+
96
+ Then, update your Gemfile:
97
+
98
+ `gem 'puma', '< 6'`
@@ -0,0 +1,56 @@
1
+ # Welcome to Puma 6: Sunflower.
2
+
3
+ ![Image by Todd Trapani, Unsplash](https://user-images.githubusercontent.com/845662/192706685-774d3d0d-f4a9-4b93-b27b-5a3b7f44ff31.jpg)
4
+
5
+ Puma 6 brings performance improvements for most applications, experimental Rack 3 support, support for Sidekiq 7 Capsules, and more.
6
+
7
+ Here's what you should do:
8
+
9
+ 1. Review the Upgrade section below to look for breaking changes that could affect you.
10
+ 2. Upgrade to version 6.0 in your Gemfile and deploy.
11
+ 3. Open up a new bug issue if you find any problems.
12
+ 4. Join us in building Puma! We welcome first-timers. See [CONTRIBUTING.md](../CONTRIBUTING.md).
13
+
14
+ For a complete list of changes, see [History.md](../History.md).
15
+
16
+ ## What's New
17
+
18
+ Puma 6 is mostly about a few nice-to-have performance changes, and then a few breaking API changes we've been putting off for a while.
19
+
20
+ ### Improved Performance
21
+
22
+ We've improved throughput and latency in Puma 6 in a few areas.
23
+
24
+ 1. **Large chunked response body throughput 3-10x higher** Chunked response bodies >100kb should be 3 to 10 times faster than in Puma 5. String response bodies should be ~10% faster.
25
+ 2. **File response throughput is 3x higher.** File responses (e.g. assets) should be about 3x faster.
26
+ 3. **wait_for_less_busy_worker is now default, meaning lower latencies for high-utilization servers** `wait_for_less_busy_worker` was an experimental feature in Puma 5 and it's now the default in Puma 6. This feature makes each Puma child worker in cluster mode wait before listening on the socket, and that wait time is proportional to N * `number_of_threads_responding_to_requests`. This means that it's more likely that a request is picked up by the least-loaded Puma child worker listening on the socket. Many users reported back that this option was stable and decreased average latency, particularly in environments with high load and utilization.
27
+
28
+ ### Experimental Rack 3 Support
29
+
30
+ [Rack 3 is now out](https://github.com/rack/rack/blob/main/UPGRADE-GUIDE.md) and we've started on Rack 3 support. Please open a bug if you find any incompatibilites.
31
+
32
+ ### Sidekiq 7 Capsules
33
+
34
+ Sidekiq 7 (releasing soon) introduces Capsules, which allows you to run a Sidekiq server inside your Puma server (or any other Ruby process for that matter). We've added support by allowing you to pass data into `run_hooks`, see [issue #2915](https://github.com/puma/puma/issues/2915).
35
+
36
+ ## Upgrade
37
+
38
+ Check the following list to see if you're depending on any of these behaviors:
39
+
40
+ 1. Configuration constants like `DefaultRackup` removed, see [#2928](https://github.com/puma/puma/pull/2928/files#diff-2dc4e3e83be7fd97cebc482ae07d6a8216944003de82458783fb00b5ae9524c8) for the full list.
41
+ 1. We have changed the names of the following environment variables: `DISABLE_SSL` is now `PUMA_DISABLE_SSL`, and `MAKE_WARNINGS_INTO_ERRORS` is now `PUMA_MAKE_WARNINGS_INTO_ERRORS`.
42
+ 1. Nakayoshi GC (`nakayoshi_fork` option in config) has been removed without replacement.
43
+ 1. `wait_for_less_busy_worker` is now on by default. If you don't want to use this feature, you must add `wait_for_less_busy_worker 0` in your config.
44
+ 1. We've removed the following public methods on Puma::Server: `Puma::Server#min_threads`, `Puma::Server#max_threads`. Instead, you can pass in configuration as an option to Puma::Server#new. This might make certain gems break (`capybara` for example).
45
+ 1. We've removed the following constants: `Puma::StateFile::FIELDS`, `Puma::CLI::KEYS_NOT_TO_PERSIST_IN_STATE` and `Puma::Launcher::KEYS_NOT_TO_PERSIST_IN_STATE`, and `Puma::ControlCLI::COMMANDS`.
46
+ 1. We no longer support Ruby 2.2, 2.3, or JRuby on Java 1.7 or below.
47
+ 1. The behavior of `remote_addr` has changed. When using the set_remote_address header: "header_name" functionality, if the header is not passed, REMOTE_ADDR is now set to the physical peeraddr instead of always being set to 127.0.0.1. When an error occurs preventing the physical peeraddr from being fetched, REMOTE_ADDR is now set to the unspecified source address ('0.0.0.0') instead of to '127.0.0.1'
48
+ 1. Previously, Puma supported anything as an HTTP method and passed it to the app. We now only accept the following 8 HTTP methods, based on [RFC 9110, section 9.1](https://www.rfc-editor.org/rfc/rfc9110.html#section-9.1). The [IANA HTTP Method Registry](https://www.iana.org/assignments/http-methods/http-methods.xhtml) contains a full list of HTTP methods.
49
+ ```
50
+ HEAD GET POST PUT DELETE OPTIONS TRACE PATCH
51
+ ```
52
+ As of Puma 6.2, these can be overridden by `supported_http_methods` in your config file, see `Puma::DSL#supported_http_methods`.
53
+
54
+ Then, update your Gemfile:
55
+
56
+ `gem 'puma', '< 7'`
@@ -0,0 +1,52 @@
1
+ # Welcome to Puma 7: Romantic Warrior.
2
+
3
+ Puma 7 brings better tail latency for keepalive-heavy traffic, support for fiber-per-request runtimes, and a handful of cleanup and compatibility changes across the server.
4
+
5
+ Here's what you should do:
6
+
7
+ 1. Review the Upgrade section below to look for breaking changes that could affect you.
8
+ 2. Upgrade to version 7.0 in your Gemfile and deploy.
9
+ 3. Open up a new bug issue if you find any problems.
10
+ 4. Join us in building Puma! We welcome first-timers. See [CONTRIBUTING.md](../CONTRIBUTING.md).
11
+
12
+ For a complete list of changes, see [History.md](../History.md).
13
+
14
+ ## What's New
15
+
16
+ Puma 7 is focused on request lifecycle improvements and long-request correctness.
17
+
18
+ 1. **Better tail behavior for keepalive connections.** Puma 7 includes a high-effort fix for long tail response behavior with keepalive clients.
19
+ 2. **Fiber-per-request support.** Puma now supports fiber-per-request execution.
20
+ 3. **`rack.response_finished` support.** Puma now supports the Rack hook for response completion.
21
+ 4. **Custom request logging support.** You can plug in a custom logger for request logs.
22
+
23
+ ## Upgrade
24
+
25
+ Check the following list to see if you're depending on any of these behaviors:
26
+
27
+ 1. The default `max_keep_alive` is now `999`.
28
+ 1. The default `persistent_timeout` is now `65` seconds.
29
+ 1. Hook methods now raise `ArgumentError` if called without a block.
30
+ 1. For Rack > 3.1, Puma no longer sets `env['HTTP_VERSION']`.
31
+ 1. `Puma::Runner#ruby_engine` has been removed.
32
+ 1. `preload_app!` is now the default in cluster mode. If you need the old behavior, set `preload_app! false` explicitly.
33
+ 1. `Puma::Configuration` must be `clamp`-ed before reading values.
34
+ 1. Response headers are now normalized to lowercase.
35
+ 1. The minimum supported Ruby version is now 3.0.
36
+ 1. Callback hook names have been renamed:
37
+
38
+ | Old hook name | New hook name |
39
+ |---|---|
40
+ | `on_worker_boot` | `before_worker_boot` |
41
+ | `on_worker_shutdown` | `before_worker_shutdown` |
42
+ | `on_restart` | `before_restart` |
43
+ | `on_booted` | `after_booted` |
44
+ | `on_stopped` | `after_stopped` |
45
+ | `on_refork` | `before_refork` |
46
+ | `on_thread_start` | `before_thread_start` |
47
+ | `on_thread_exit` | `before_thread_exit` |
48
+ | `on_worker_fork` | `before_worker_fork` |
49
+
50
+ Then, update your Gemfile:
51
+
52
+ `gem 'puma', '< 8'`
@@ -0,0 +1,100 @@
1
+ # Welcome to Puma 8.0: Into the Arena.
2
+
3
+ Puma 8 brings IPv6 by default, increased control over the threadpool, and more.
4
+
5
+ Here's what you should do:
6
+
7
+ 1. Review the Upgrade section below to look for breaking changes that could affect you.
8
+ 2. Upgrade to version 8.0 in your Gemfile and deploy.
9
+ 3. Open up a new bug issue if you find any problems.
10
+ 4. Join us in building Puma! We welcome first-timers. See [CONTRIBUTING.md](./CONTRIBUTING.md).
11
+
12
+ For a complete list of changes, see [History.md](./History.md).
13
+
14
+ ## What's New
15
+
16
+ ### Smarter concurrency controls
17
+
18
+ **IO-bound requests can now go past your normal thread ceiling.** Puma 8 adds `max_io_threads` and injects `env["puma.mark_as_io_bound"]` into the Rack env so your app or middleware can tell Puma when a request has become mostly I/O wait. That helps mixed workloads a lot: slow API calls, report generation, and similar wait-heavy requests no longer need to crowd out CPU-bound work as aggressively. This landed in [#3816](https://github.com/puma/puma/pull/3816) and was refined in [#3894](https://github.com/puma/puma/pull/3894).
19
+
20
+ ```ruby
21
+ # config/puma.rb
22
+ threads 0, 5
23
+ max_io_threads 5
24
+ ```
25
+
26
+ ```ruby
27
+ # config.ru
28
+ run lambda { |env|
29
+ env['puma.mark_as_io_bound'].call
30
+ report = SlowReportService.fetch
31
+
32
+ [200, { 'content-type' => 'application/json' }, [report]]
33
+ }
34
+ ```
35
+
36
+ We anticipate this will mainly by used by framework authors who have threads or types of requests they know are extremely IO-bound, and don't recommend it for use at the application level.
37
+
38
+ **Thread pool limits can be changed at runtime.** Puma now exposes `Puma::Server#update_thread_pool_min_max`, and hook/plugin code can do the same through `Puma::ServerPluginControl`.
39
+
40
+ ```ruby
41
+ # from a plugin or other trusted in-process integration
42
+ server.update_thread_pool_min_max(min: 2, max: 12)
43
+ ```
44
+
45
+ If you already use `before_thread_start`, note that hook behavior changed in this release; see the Upgrade section below.
46
+
47
+ ### Cleaner config for single and cluster mode
48
+
49
+ **New `single` and `cluster` blocks let one config file express both modes cleanly.** These blocks run after config files are loaded, so you can keep the mode-specific settings in one obvious place instead of scattering `if` logic through `config/puma.rb`. This makes shared configs much easier to read and reuse across development, review apps, and production. See [#3621](https://github.com/puma/puma/pull/3621).
50
+
51
+ ```ruby
52
+ workers ENV.fetch('WEB_CONCURRENCY', 0)
53
+
54
+ single do
55
+ silence_fork_callback_warning
56
+ end
57
+
58
+ # Only runs if workers > 0
59
+ cluster do
60
+ preload_app!
61
+ before_worker_boot do
62
+ # Do a thing
63
+ end
64
+ end
65
+ ```
66
+
67
+ ### Better debugging and operations
68
+
69
+ **`shutdown_debug` can now be limited to forced shutdowns.** If you want thread backtraces only when a graceful shutdown turns into a forced one, use `shutdown_debug on_force: true`. That keeps normal deploy logs quieter while still giving you the "what is hanging?" escape hatch when you need it. See [#3671](https://github.com/puma/puma/pull/3671).
70
+
71
+ ```ruby
72
+ shutdown_debug on_force: true
73
+ force_shutdown_after 30
74
+ ```
75
+
76
+ **Thread backtrace via signal works on more platforms.** On systems without `SIGINFO`, Puma now uses `SIGPWR` for thread backtrace dumps. See [#3829](https://github.com/puma/puma/pull/3829).
77
+
78
+ **Phased restart is safer with `fork_worker`.** Puma no longer reforks from a stale worker 0 during phased restarts in `fork_worker` mode. There is nothing new to configure, but if you have tooling that watches worker order, check the Upgrade section because the rollout sequence changed. See [#3853](https://github.com/puma/puma/pull/3853).
79
+
80
+ ### Networking and performance
81
+
82
+ **Puma now prefers IPv6 wildcard binds when the host supports them.** When Puma detects a non-loopback IPv6 interface, the default TCP host becomes `::` and the default bind becomes `tcp://[::]:PORT`. That lines Puma up better with modern dual-stack hosts, while still falling back to IPv4 when IPv6 is unavailable. See [#3847](https://github.com/puma/puma/pull/3847). If you need IPv4-only behavior, pin the bind explicitly.
83
+
84
+ ```ruby
85
+ bind 'tcp://0.0.0.0:9292'
86
+ ```
87
+
88
+ **There are also a couple of hot-path performance wins.** JRuby gets a faster HTTP parser with fewer copies and cheaper lookups, and Puma now avoids redundant header key downcasing when building responses. See [#3838](https://github.com/puma/puma/pull/3838) and [#3874](https://github.com/puma/puma/pull/3874).
89
+
90
+ ## Upgrade
91
+
92
+ Check the following list to see if you're depending on any of these behaviors:
93
+
94
+ 1. Puma will now listen on `::` (IPv6) by default. Previously, it listened to `0.0.0.0` (IPv4). Systems that support both IPv6 and IPv4 (Ubuntu and Mac commonly do) will still support receiving to `0.0.0.0`. See [[the support table in the PR](https://github.com/puma/puma/pull/3847)](https://github.com/puma/puma/pull/3847) for a binding compatibility. For systems that ONLY bind to IPv6 (without IPv4 support) this may be a breaking change. You can overwrite this default behavior by setting `bind 'tcp://0.0.0.0:9292'`, `port ENV.fetch('PORT', 9292), '0.0.0.0'`, or `set_default_host '0.0.0.0'` explicitly to remain IPv4 only. Review any firewall rules, health checks, deploy scripts, or host-string parsing code that assumed `0.0.0.0`, `127.0.0.1`, or unbracketed `HOST:PORT` formatting.
95
+ 2. If you explicitly configure `bind 'tcp://[::]:...'`, `bind 'ssl://[::]:...'`, or equivalent `ssl_bind '::', ...`, Puma will now rewrite that unspecified IPv6 bind to `0.0.0.0` when the host has no non-loopback IPv6 interface, and it will warn on boot. If you were using `::` to force IPv6-only behavior or to detect IPv6 availability, switch to a concrete IPv6 address instead of `::`, or ensure the machine actually has a usable IPv6 interface before Puma starts.
96
+ 3. `before_thread_start` hooks now receive a `Puma::ServerPluginControl` argument. Update any zero-arity lambdas, method objects, or other strict-arity hook code to accept one argument, for example `before_thread_start { |_control| ... }`, or Puma can raise `ArgumentError` when the hook runs.
97
+ 4. On platforms without `SIGINFO`, Puma now traps `SIGPWR`, and `pumactl info` sends `SIGPWR` there. If your supervisor, init script, or container tooling already uses `SIGPWR` for something else, change that wiring before upgrading and update any runbooks that assumed Puma would ignore `SIGPWR`.
98
+ 5. Requests rejected by `supported_http_methods` are now treated as parser/client errors earlier in request processing. If you use `supported_http_methods`, re-test unsupported-method requests and do not depend on the old 501 timing, connection handling, or `lowlevel_error_handler` behavior; if your error handler still sees these requests, make sure it tolerates a less-normalized Rack `env`.
99
+ 6. `http_content_length_limit` enforcement is stricter now. A `413` on an HTTP/1.1 keep-alive request now forces `connection: close`, and chunked request bodies are rejected as soon as they cross the limit during parsing. Update clients, proxies, and tests not to reuse the socket after a `413`, and re-test any upload flows or custom error handling that depended on Puma's previous oversized-body behavior.
100
+ 7. If you use `fork_worker`, phased restart order changed. During `USR1` or `pumactl phased-restart`, worker `0` is now reinserted and restarted/reforked first after replacement. Update deployment scripts, canary logic, or monitoring that assumed the old worker sequence or keyed rollout steps off worker index order.
@@ -0,0 +1,74 @@
1
+ # Architecture
2
+
3
+ ## Overview
4
+
5
+ ![https://bit.ly/2iJuFky](images/puma-general-arch.png)
6
+
7
+ Puma is a threaded Ruby HTTP application server processing requests across a TCP
8
+ and/or UNIX socket.
9
+
10
+
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).
16
+
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
30
+
31
+ ![https://bit.ly/2zwzhEK](images/puma-connection-flow.png)
32
+
33
+ * Upon startup, Puma listens on a TCP or UNIX socket.
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`
63
+
64
+ ![https://bit.ly/2zxCJ1Z](images/puma-connection-flow-no-reactor.png)
65
+
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.
71
+
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,55 @@
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
+ ```
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
+ ```
@@ -0,0 +1,137 @@
1
+ # Deployment engineering for Puma
2
+
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.
5
+
6
+ To that end, this document serves as a foundation of wisdom regarding deploying
7
+ Puma to production while increasing happiness and decreasing downtime.
8
+
9
+ ## Specifying Puma
10
+
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.
13
+
14
+ ## Single vs. Cluster mode
15
+
16
+ Initially, Puma was conceived as a thread-only web server, but support for
17
+ processes was added in version 2.
18
+
19
+ In general, use single mode only if:
20
+
21
+ * You are using JRuby, TruffleRuby or another fully-multithreaded implementation of Ruby
22
+ * You are using MRI but in an environment where only 1 CPU core is available.
23
+
24
+ Otherwise, you'll want to use cluster mode to utilize all available CPU resources.
25
+
26
+ To run `puma` in single mode (i.e., as a development environment), set the
27
+ number of workers to 0; anything higher will run in cluster mode.
28
+
29
+ ## Cluster Mode Tips
30
+
31
+ For the purposes of Puma provisioning, "CPU cores" means:
32
+
33
+ 1. On ARM, the number of physical cores.
34
+ 2. On x86, the number of logical cores, hyperthreads, or vCPUs (these words all mean the same thing).
35
+
36
+ Set your config with the following process:
37
+
38
+ * Use cluster mode and set `workers :auto` (requires the `concurrent-ruby` gem) to match the number of CPU cores on the machine (minimum 2, otherwise use single mode!). If you can't add the gem, set the worker count manually to the available CPU cores.
39
+ * Set the number of threads to desired concurrent requests/number of workers.
40
+ Puma defaults to 5, and that's a decent number.
41
+
42
+ For most deployments, adding `concurrent-ruby` and using `workers :auto` is the right starting point.
43
+
44
+ See [`workers :auto` gotchas](../lib/puma/dsl.rb).
45
+
46
+ ## Worker utilization
47
+
48
+ **How do you know if you've got enough (or too many workers)?**
49
+
50
+ A good question. Due to MRI's GIL, only one thread can be executing Ruby code at
51
+ a time. But since so many apps are waiting on IO from DBs, etc., they can
52
+ utilize threads to use the process more efficiently.
53
+
54
+ Generally, you never want processes that are pegged all the time. That can mean
55
+ there is more work to do than the process can get through, and requests will end up with additional latency. On the other hand, if
56
+ you have processes that sit around doing nothing, then you're wasting resources and money.
57
+
58
+ In general, you are making a tradeoff between:
59
+
60
+ 1. CPU and memory utilization.
61
+ 2. Time spent queueing for a Puma worker to `accept` requests and additional latency caused by CPU contention.
62
+
63
+ If latency is important to you, you will have to accept lower utilization, and vice versa.
64
+
65
+ ## Container/VPS sizing
66
+
67
+ You will have to make a decision about how "big" to make each pod/VPS/server/dyno.
68
+
69
+ **TL:DR;**: 80% of Puma apps will end up deploying "pods" of 4 workers, 5 threads each, 4 vCPU and 8GB of RAM.
70
+
71
+ For the rest of this discussion, we'll adopt the Kubernetes term of "pods".
72
+
73
+ 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:
74
+
75
+ * **Increasing worker counts decreases latency, but means you scale in bigger "chunks"**. 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).
76
+ * **Increasing thread counts will increase throughput, but also latency and memory use** 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% of wall clock time) will lead to additional request latency and additional memory usage.
77
+ * **Increasing worker counts decreases memory per worker on average**. 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.
78
+ * **Low worker counts (<4) have exceptionally poor throughput**. Don't run less than 4 processes per pod if you can. Low numbers of processes per pod will lead to high request queueing (see discussion above), which means you will have to run more pods and resources.
79
+ * **CPU-core-to-worker ratios should be around 1**. If running Puma with `threads > 1`, allocate 1 CPU core (see definition above!) per worker. If single threaded, allocate ~0.75 cpus per worker. Most web applications spend about 25% of their time in I/O - but when you're running multi-threaded, your Puma process will have higher CPU usage and should be able to fully saturate a CPU core. Using `workers :auto` will size workers to this guidance on most platforms.
80
+ * **Don't set memory limits unless necessary**. Most Puma processes will use about ~512MB-1GB per worker, and about 1GB for the master process. However, you probably shouldn't bother with setting memory limits lower than around 2GB per process, because most places you are deploying will have 2GB of RAM per CPU. A sensible memory limit for a Puma configuration of 4 child workers might be something like 8 GB (1 GB for the master, 7GB for the 4 children).
81
+
82
+ **Measuring utilization and queue time**
83
+
84
+ Using a timestamp header from an upstream proxy server (e.g., `nginx` or
85
+ `haproxy`) makes it possible to indicate how long requests have been waiting for
86
+ a Puma thread to become available.
87
+
88
+ * Have your upstream proxy set a header with the time it received the request:
89
+ * nginx: `proxy_set_header X-Request-Start "${msec}";`
90
+ * haproxy >= 1.9: `http-request set-header X-Request-Start
91
+ t=%[date()]%[date_us()]`
92
+ * haproxy < 1.9: `http-request set-header X-Request-Start t=%[date()]`
93
+ * In your Rack middleware, determine the amount of time elapsed since
94
+ `X-Request-Start`.
95
+ * To improve accuracy, you will want to subtract time spent waiting for slow
96
+ clients:
97
+ * `env['puma.request_body_wait']` contains the number of milliseconds Puma
98
+ spent waiting for the client to send the request body.
99
+ * haproxy: `%Th` (TLS handshake time) and `%Ti` (idle time before request)
100
+ can also be added as headers.
101
+
102
+ ## Should I daemonize?
103
+
104
+ The Puma 5.0 release removed daemonization. For older versions and alternatives,
105
+ continue reading.
106
+
107
+ I prefer not to daemonize my servers and use something like `runit` or `systemd`
108
+ to monitor them as child processes. This gives them fast response to crashes and
109
+ makes it easy to figure out what is going on. Additionally, unlike `unicorn`,
110
+ Puma does not require daemonization to do zero-downtime restarts.
111
+
112
+ I see people using daemonization because they start puma directly via Capistrano
113
+ task and thus want it to live on past the `cap deploy`. To these people, I say:
114
+ You need to be using a process monitor. Nothing is making sure Puma stays up in
115
+ this scenario! You're just waiting for something weird to happen, Puma to die,
116
+ and to get paged at 3 AM. Do yourself a favor, at least the process monitoring
117
+ your OS comes with, be it `sysvinit` or `systemd`. Or branch out and use `runit`
118
+ or hell, even `monit`.
119
+
120
+ ## Restarting
121
+
122
+ You probably will want to deploy some new code at some point, and you'd like
123
+ Puma to start running that new code. There are a few options for restarting
124
+ Puma, described separately in our [restart documentation](restart.md).
125
+
126
+ ## Migrating from Unicorn
127
+
128
+ * If you're migrating from unicorn though, here are some settings to start with:
129
+ * Set workers to half the number of unicorn workers you're using
130
+ * Set threads to 2
131
+ * Enjoy 50% memory savings
132
+ * As you grow more confident in the thread-safety of your app, you can tune the
133
+ workers down and the threads up.
134
+
135
+ ## Ubuntu / Systemd (Systemctl) Installation
136
+
137
+ See [systemd.md](systemd.md)