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/LICENSE CHANGED
@@ -1,26 +1,29 @@
1
- Some code copyright (c) 2005, Zed Shaw
2
- Copyright (c) 2011, Evan Phoenix
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2019, Evan Phoenix. Some code by Zed Shaw, (c) 2005.
3
4
  All rights reserved.
4
5
 
5
- Redistribution and use in source and binary forms, with or without
6
+ Redistribution and use in source and binary forms, with or without
6
7
  modification, are permitted provided that the following conditions are met:
7
8
 
8
- * Redistributions of source code must retain the above copyright notice, this
9
- list of conditions and the following disclaimer.
10
- * Redistributions in binary form must reproduce the above copyright notice
11
- this list of conditions and the following disclaimer in the documentation
12
- and/or other materials provided with the distribution.
13
- * Neither the name of the Evan Phoenix nor the names of its contributors
14
- may be used to endorse or promote products derived from this software
15
- without specific prior written permission.
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
16
19
 
17
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
29
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md CHANGED
@@ -1,197 +1,484 @@
1
- # Puma: A Ruby Web Server Built For Concurrency
1
+ <p align="center">
2
+ <img src="docs/images/standard-logo.svg" alt="Puma logo">
3
+ </p>
2
4
 
3
- [![Build Status](https://secure.travis-ci.org/puma/puma.png)](http://travis-ci.org/puma/puma) [![Dependency Status](https://gemnasium.com/puma/puma.png)](https://gemnasium.com/puma/puma)
5
+ # Puma: A Ruby Web Server Built For Parallelism
4
6
 
5
- ## Description
7
+ [![Actions](https://github.com/puma/puma/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/puma/puma/actions/workflows/tests.yml?query=branch%3Amain)
6
8
 
7
- Puma is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications. Puma is intended for use in both development and production environments. In order to get the best throughput, it is highly recommended that you use a Ruby implementation with real threads like Rubinius or JRuby.
9
+ Puma is a **simple, fast, multi-threaded, and highly parallel HTTP 1.1 server for Ruby/Rack applications**.
8
10
 
9
- ## Built For Speed &amp; Concurrency
11
+ ## Built For Speed &amp; Parallelism
10
12
 
11
- Puma is a simple, fast, and highly concurrent HTTP 1.1 server for Ruby web applications. It can be used with any application that supports Rack, and is considered the replacement for Webrick and Mongrel. It was designed to be the go-to server for [Rubinius](http://rubini.us), but also works well with JRuby and MRI. Puma is intended for use in both development and production environments.
13
+ Puma is a server for [Rack](https://github.com/rack/rack)-powered HTTP applications written in Ruby. It is:
14
+ * **Multi-threaded**. Each request is served in a separate thread. This helps you serve more requests per second with less memory use.
15
+ * **Multi-process**. "Pre-forks" in cluster mode, using less memory per-process thanks to copy-on-write memory.
16
+ * **Standalone**. With SSL support, zero-downtime rolling restarts and a built-in request bufferer, you can deploy Puma without any reverse proxy.
17
+ * **Battle-tested**. Our HTTP parser is inherited from Mongrel and has over 15 years of production use. Puma is currently the most popular Ruby webserver, and is the default server for Ruby on Rails.
12
18
 
13
- Under the hood, 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 in a thread from an internal thread pool (which you can control). This allows Puma to provide real concurrency for your web application!
19
+ Originally designed as a server for [Rubinius](https://github.com/rubinius/rubinius), Puma also works well with Ruby (MRI) and JRuby.
14
20
 
15
- With Rubinius 2.0, Puma will utilize all cores on your CPU with real threads, meaning you won't have to spawn multiple processes to increase throughput. You can expect to see a similar benefit from JRuby.
16
-
17
- On MRI, there is a Global Interpreter Lock (GIL) that ensures only one thread can be run 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 blocking IO to be run concurrently (EventMachine-based servers such as Thin turn off this ability, requiring you to use special libraries). Your mileage may vary. In order to get the best throughput, it is highly recommended that you use a Ruby implementation with real threads like [Rubinius](http://rubini.us) or [JRuby](http://jruby.org).
21
+ On MRI, there is a Global VM Lock (GVL) that ensures only one thread can run Ruby code at a time. But if you're doing a lot of blocking IO (such as HTTP calls to external APIs like Twitter), Puma still improves MRI's throughput by allowing IO waiting to be done in parallel. Truly parallel Ruby implementations (TruffleRuby, JRuby) don't have this limitation.
18
22
 
19
23
  ## Quick Start
20
24
 
21
- The easiest way to get started with Puma is to install it via RubyGems. You can do this easily:
25
+ ```
26
+ $ gem install puma
27
+ $ puma
28
+ ```
29
+
30
+ Without arguments, puma will look for a rackup (.ru) file in
31
+ working directory called `config.ru`.
32
+
33
+ ## SSL Connection Support
22
34
 
23
- $ gem install puma
35
+ Puma will install/compile with support for ssl sockets, assuming OpenSSL
36
+ development files are installed on the system.
24
37
 
25
- Now you should have the puma command available in your PATH, so just do the following in the root folder of your Rack application:
38
+ If the system does not have OpenSSL development files installed, Puma will
39
+ install/compile, but it will not allow ssl connections.
26
40
 
27
- $ puma app.ru
41
+ ## Frameworks
28
42
 
29
- ## Advanced Setup
43
+ ### Rails
44
+
45
+ Puma is the default server for Rails, included in the generated Gemfile.
46
+
47
+ Start your server with the `rails` command:
48
+
49
+ ```
50
+ $ rails server
51
+ ```
52
+
53
+ Many configuration options and Puma features are not available when using `rails server`. It is recommended that you use Puma's executable instead:
54
+
55
+ ```
56
+ $ bundle exec puma
57
+ ```
30
58
 
31
59
  ### Sinatra
32
60
 
33
61
  You can run your Sinatra application with Puma from the command line like this:
34
62
 
35
- $ ruby app.rb -s Puma
63
+ ```
64
+ $ ruby app.rb -s Puma
65
+ ```
36
66
 
37
- Or you can configure your application to always use Puma:
67
+ In order to actually configure Puma using a config file, like `puma.rb`, however, you need to use the `puma` executable. To do this, you must add a rackup file to your Sinatra app:
68
+
69
+ ```ruby
70
+ # config.ru
71
+ require './app'
72
+ run Sinatra::Application
73
+ ```
38
74
 
39
- require 'sinatra'
40
- configure { set :server, :puma }
75
+ You can then start your application using:
41
76
 
42
- If you use Bundler, make sure you add Puma to your Gemfile (see below).
77
+ ```
78
+ $ bundle exec puma
79
+ ```
43
80
 
44
- ### Rails
81
+ ## Configuration
45
82
 
46
- First, make sure Puma is in your Gemfile:
83
+ Puma provides numerous options. Consult `puma -h` (or `puma --help`) for a full list of CLI options, or see `Puma::DSL` or [dsl.rb](https://github.com/puma/puma/blob/main/lib/puma/dsl.rb).
47
84
 
48
- gem 'puma'
85
+ You can also find several configuration examples as part of the
86
+ [test](https://github.com/puma/puma/tree/main/test/config) suite.
49
87
 
50
- Then start your server with the `rails` command:
88
+ For debugging purposes, you can set the environment variable `PUMA_LOG_CONFIG` with a value
89
+ and the loaded configuration will be printed as part of the boot process.
51
90
 
52
- $ rails s Puma
91
+ ### Thread Pool
53
92
 
54
- ### Rackup
93
+ Puma uses a thread pool. You can set the minimum and maximum number of threads that are available in the pool with the `-t` (or `--threads`) flag:
55
94
 
56
- You can pass it as an option to `rackup`:
95
+ ```
96
+ $ puma -t 8:32
97
+ ```
57
98
 
58
- $ rackup -s Puma
99
+ Puma will automatically scale the number of threads, from the minimum until it caps out at the maximum, based on how much traffic is present. The current default is `0:16` and on MRI is `0:5`. Feel free to experiment, but be careful not to set the number of maximum threads to a large number, as you may exhaust resources on the system (or cause contention for the Global VM Lock, when using MRI).
59
100
 
60
- Alternatively, you can modify your `config.ru` to choose Puma by default, by adding the following as the first line:
101
+ 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.
61
102
 
62
- #\ -s puma
103
+ ### Cluster mode
63
104
 
64
- ## Configuration
105
+ 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:
65
106
 
66
- Puma provides numerous options for controlling the operation of the server. Consult `puma -h` (or `puma --help`) for a full list.
107
+ ```
108
+ $ puma -t 8:32 -w 3
109
+ ```
67
110
 
68
- ### Thread Pool
111
+ Or with the `WEB_CONCURRENCY` environment variable:
69
112
 
70
- Puma utilizes a dynamic thread pool which you can modify. You can set the minimum and maximum number of threads that are available in the pool with the `-t` (or `--threads`) flag:
113
+ ```
114
+ $ WEB_CONCURRENCY=3 puma -t 8:32
115
+ ```
71
116
 
72
- $ puma -t 8:32
117
+ When using a config file, most applications can simply set `workers :auto` (requires the `concurrent-ruby` gem) to match the number of worker processes to the available processors:
73
118
 
74
- Puma will automatically scale the number of threads based on how much traffic is present. The current default is `0:16`. Feel free to experiment, but be careful not to set the number of maximum threads to a very large number, as you may exhaust resources on the system (or hit resource limits).
119
+ ```ruby
120
+ # config/puma.rb
121
+ workers :auto
122
+ ```
75
123
 
76
- ### Clustered mode
124
+ See [`workers :auto` gotchas](lib/puma/dsl.rb).
77
125
 
78
- Puma 2 offers clustered mode, allowing you to use forked processes to handle multiple incoming requests concurrently, in addition to threads already provided. You can tune the number of workers with the `-w` (or `--workers`) flag:
126
+ 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.
79
127
 
80
- $ puma -t 8:32 -w 3
81
-
82
- On a ruby implementation that offers native threads, you should tune this number to match the number of cores available.
83
- 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 be 32 threads.
128
+ If `workers` is set to `:auto`, or 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://msp-greg.github.io/concurrent-ruby/Concurrent.html#available_processor_count-class_method).
84
129
 
85
- Additionally, you can specify a block in your configuration that will be run on boot of each worker:
130
+ For an in-depth discussion of the tradeoffs of thread and process count settings, [see our docs](docs/deployment.md).
86
131
 
87
- # config/puma.rb
88
- on_worker_boot do
89
- # configuration here
90
- end
132
+ 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).
91
133
 
92
- This code can be used to setup the process before booting the application, allowing
93
- you to do some puma-specific things that you don't want to embed in your application.
94
- For instance, you could fire a log notification that a worker booted or send something to statsd.
95
- This can be called multiple times to add hooks.
134
+ 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:
96
135
 
97
- Be sure to specify the location of your configuration file:
136
+ ```
137
+ $ puma -w 3 --preload
138
+ ```
98
139
 
99
- $ puma -t 8:32 -w 3 -C config/puma.rb
140
+ Or, if you're using a configuration file, you can use the `preload_app!` method:
141
+
142
+ ```ruby
143
+ # config/puma.rb
144
+ workers 3
145
+ preload_app!
146
+ ```
147
+
148
+ 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.
149
+
150
+ #### Cluster mode hooks
151
+
152
+ When using clustered mode, Puma's configuration DSL provides `before_fork`, `before_worker_boot`, and `after_worker_shutdown`
153
+ hooks to run code when the master process forks, the child workers are booted, and after each child worker exits respectively.
154
+
155
+ It is recommended to use these hooks with `preload_app!`, otherwise constants loaded by your
156
+ application (such as `Rails`) will not be available inside the hooks.
157
+
158
+ ```ruby
159
+ # config/puma.rb
160
+ before_fork do
161
+ # Add code to run inside the Puma master process before it forks a worker child.
162
+ end
163
+
164
+ before_worker_boot do
165
+ # Add code to run inside the Puma worker process after forking.
166
+ end
167
+
168
+ after_worker_shutdown do |worker_handle|
169
+ # Add code to run inside the Puma master process after a worker exits. `worker.process_status` can be used to get the
170
+ # `Process::Status` of the exited worker.
171
+ end
172
+ ```
173
+
174
+ In addition, there is an `before_refork` and `after_refork` hooks which are used only in [`fork_worker` mode](docs/fork_worker.md),
175
+ when the worker 0 child process forks a grandchild worker:
176
+
177
+ ```ruby
178
+ before_refork do
179
+ # Used only when fork_worker mode is enabled. Add code to run inside the Puma worker 0
180
+ # child process before it forks a grandchild worker.
181
+ end
182
+ ```
183
+
184
+ ```ruby
185
+ after_refork do
186
+ # Used only when fork_worker mode is enabled. Add code to run inside the Puma worker 0
187
+ # child process after it forks a grandchild worker.
188
+ end
189
+ ```
190
+
191
+ Importantly, note the following considerations when Ruby forks a child process:
192
+
193
+ 1. File descriptors such as network sockets **are** copied from the parent to the forked
194
+ child process. Dual-use of the same sockets by parent and child will result in I/O conflicts
195
+ such as `SocketError`, `Errno::EPIPE`, and `EOFError`.
196
+ 2. Background Ruby threads, including threads used by various third-party gems for connection
197
+ monitoring, etc., are **not** copied to the child process. Often this does not cause
198
+ immediate problems until a third-party connection goes down, at which point there will
199
+ be no supervisor to reconnect it.
200
+
201
+ Therefore, we recommend the following:
202
+
203
+ 1. If possible, do not establish any socket connections (HTTP, database connections, etc.)
204
+ inside Puma's master process when booting.
205
+ 2. If (1) is not possible, use `before_fork` and `before_refork` to disconnect the parent's socket
206
+ connections when forking, so that they are not accidentally copied to the child process.
207
+ 3. Use `before_worker_boot` to restart any background threads on the forked child.
208
+ 4. Use `after_refork` to restart any background threads on the parent.
209
+
210
+ #### Master process lifecycle hooks
211
+
212
+ Puma's configuration DSL provides master process lifecycle hooks `after_booted`, `before_restart`, and `after_stopped`
213
+ which may be used to specify code blocks to run on each event:
214
+
215
+ ```ruby
216
+ # config/puma.rb
217
+ after_booted do
218
+ # Add code to run in the Puma master process after it boots,
219
+ # and also after a phased restart completes.
220
+ end
221
+
222
+ before_restart do
223
+ # Add code to run in the Puma master process when it receives
224
+ # a restart command but before it restarts.
225
+ end
226
+
227
+ after_stopped do
228
+ # Add code to run in the Puma master process when it receives
229
+ # a stop command but before it shuts down.
230
+ end
231
+ ```
232
+
233
+ ### Error handling
234
+
235
+ If Puma encounters an error outside of the context of your application, it will respond with a 400/500 and a simple
236
+ textual error message (see `Puma::Server#lowlevel_error` or [server.rb](https://github.com/puma/puma/blob/main/lib/puma/server.rb)).
237
+ You can specify custom behavior for this scenario. For example, you can report the error to your third-party
238
+ error-tracking service (in this example, [rollbar](https://rollbar.com)):
239
+
240
+ ```ruby
241
+ lowlevel_error_handler do |e, env, status|
242
+ if status == 400
243
+ 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"
244
+ else
245
+ 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"
246
+ Rollbar.critical(e)
247
+ end
248
+
249
+ [status, {}, [message]]
250
+ end
251
+ ```
100
252
 
101
253
  ### Binding TCP / Sockets
102
254
 
103
- In contrast to many other server configs which require multiple flags, Puma simply uses one URI parameter with the `-b` (or `--bind`) flag:
255
+ Bind Puma to a socket with the `-b` (or `--bind`) flag:
104
256
 
105
- $ puma -b tcp://127.0.0.1:9292
257
+ ```
258
+ $ puma -b tcp://127.0.0.1:9292
259
+ ```
106
260
 
107
- Want to use UNIX Sockets instead of TCP (which can provide a 5-10% performance boost)? No problem!
261
+ To use a UNIX Socket instead of TCP:
108
262
 
109
- $ puma -b unix:///var/run/puma.sock
263
+ ```
264
+ $ puma -b unix:///var/run/puma.sock
265
+ ```
110
266
 
111
267
  If you need to change the permissions of the UNIX socket, just add a umask parameter:
112
268
 
113
- $ puma -b 'unix:///var/run/puma.sock?umask=0777'
269
+ ```
270
+ $ puma -b 'unix:///var/run/puma.sock?umask=0111'
271
+ ```
272
+
273
+ Need a bit of security? Use SSL sockets:
114
274
 
115
- Need a bit of security? Use SSL sockets!
275
+ ```
276
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'
277
+ ```
278
+ #### Self-signed SSL certificates (via the [`localhost`] gem, for development use):
116
279
 
117
- $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'
280
+ 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.
118
281
 
119
- ### Control/Status Server
282
+ Puma automatically configures SSL when the [`localhost`] gem is loaded in a `development` environment:
120
283
 
121
- Puma comes with a builtin status/control app that can be used query and control puma itself. Here is an example of starting puma with the control server:
284
+ Add the gem to your Gemfile:
285
+ ```ruby
286
+ group(:development) do
287
+ gem 'localhost'
288
+ end
289
+ ```
122
290
 
123
- $ puma --control tcp://127.0.0.1:9293 --control-token foo
291
+ And require it implicitly using bundler:
292
+ ```ruby
293
+ require "bundler"
294
+ Bundler.require(:default, ENV["RACK_ENV"].to_sym)
295
+ ```
124
296
 
125
- This directs puma to start the control server on localhost port 9293. Additionally, all requests to the control server will need to include `token=foo` as a query parameter. This allows for simple authentication. Check out https://github.com/puma/puma/blob/master/lib/puma/app/status.rb to see what the app has available.
297
+ Alternatively, you can require the gem in your configuration file, either `config/puma/development.rb`, `config/puma.rb`, or set via the `-C` cli option:
298
+ ```ruby
299
+ require 'localhost'
300
+ # configuration methods (from Puma::DSL) as needed
301
+ ```
126
302
 
127
- ### Configuration file
303
+ Additionally, Puma must be listening to an SSL socket:
128
304
 
129
- You can also provide a configuration file which puma will use:
305
+ ```shell
306
+ $ puma -b 'ssl://localhost:9292' -C config/use_local_host.rb
130
307
 
131
- $ puma --config /path/to/config
308
+ # The following options allow you to reach Puma over HTTP as well:
309
+ $ puma -b ssl://localhost:9292 -b tcp://localhost:9393 -C config/use_local_host.rb
310
+ ```
132
311
 
133
- or
312
+ [`localhost`]: https://github.com/socketry/localhost
134
313
 
135
- $ puma -C /path/to/config
314
+ #### Controlling SSL Cipher Suites
136
315
 
137
- Take the following [sample configuration](https://github.com/puma/puma/blob/master/examples/config.rb) as inspiration or check out [configuration.rb](https://github.com/puma/puma/blob/master/lib/puma/configuration.rb) to see all available options.
316
+ To use or avoid specific SSL ciphers for TLSv1.2 and below, use `ssl_cipher_filter` or `ssl_cipher_list` options.
138
317
 
139
- ## Restart
318
+ ##### Ruby:
319
+
320
+ ```
321
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&ssl_cipher_filter=!aNULL:AES+SHA'
322
+ ```
323
+
324
+ ##### JRuby:
325
+
326
+ ```
327
+ $ 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'
328
+ ```
329
+
330
+ To configure the available TLSv1.3 ciphersuites, use `ssl_ciphersuites` option (not available for JRuby).
140
331
 
141
- Puma includes the ability to restart itself, allowing for new versions to be easily upgraded to. When available (MRI, Rubinius, JRuby), puma performs a "hot restart". This is the same functionality available in *unicorn* and *nginx* which keep the server sockets open between restarts. This makes sure that no pending requests are dropped while the restart is taking place.
332
+ ##### Ruby:
142
333
 
143
- To perform a restart, there are 2 builtin mechanisms:
334
+ ```
335
+ $ 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'
336
+ ```
144
337
 
145
- * Send the puma process the `SIGUSR2` signal
146
- * Use the status server and issue `/restart`
338
+ See https://www.openssl.org/docs/man1.1.1/man1/ciphers.html for cipher filter format and full list of cipher suites.
147
339
 
148
- No code is shared between the current and restarted process, so it should be safe to issue a restart any place where you would manually stop puma and start it again.
340
+ Disable TLS v1 with the `no_tlsv1` option:
149
341
 
150
- If the new process is unable to load, it will simply exit. You should therefore run puma under a supervisor when using it in production.
342
+ ```
343
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&no_tlsv1=true'
344
+ ```
151
345
 
152
- ### Cleanup Code
346
+ #### Controlling OpenSSL Verification Flags
153
347
 
154
- Puma isn't able to understand all the resources that your app may use, so it provides a hook in the configuration file you pass to `-C` call `on_restart`. The block passed to `on_restart` will be called, unsurprisingly, just before puma restarts itself.
348
+ To enable verification flags offered by OpenSSL, use `verification_flags` (not available for JRuby):
155
349
 
156
- You should place code to close global log files, redis connections, etc in this block so that their file descriptors don't leak into the restarted process. Failure to do so will result in slowly running out of descriptors and eventually obscure crashes as the server is restart many times.
350
+ ```
351
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&verification_flags=PARTIAL_CHAIN'
352
+ ```
157
353
 
158
- ## pumactl
354
+ You can also set multiple verification flags (by separating them with a comma):
159
355
 
160
- If you start puma with `-S some/path` then you can pass that same path to the `pumactl` program to control your server. For instance:
356
+ ```
357
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&verification_flags=PARTIAL_CHAIN,CRL_CHECK'
358
+ ```
161
359
 
162
- $ pumactl -S some/path command
360
+ 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`
361
+ (see https://www.openssl.org/docs/manmaster/man3/X509_VERIFY_PARAM_set_hostflags.html#VERIFICATION-FLAGS).
163
362
 
164
- or
363
+ #### Controlling OpenSSL Password Decryption
165
364
 
166
- $ pumactl -C url -T token command
365
+ To enable runtime decryption of an encrypted SSL key (not available for JRuby), use `key_password_command`:
167
366
 
168
- will cause the server to perform a restart. `pumactl` is a simple CLI frontend to the control/status app described above.
367
+ ```
368
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&key_password_command=/path/to/command.sh'
369
+ ```
169
370
 
170
- Allowed commands: status, restart, halt, stop
371
+ `key_password_command` must:
171
372
 
172
- ## Managing multiple Pumas / init.d / upstart scripts
373
+ 1. Be executable by Puma.
374
+ 2. Print the decryption password to stdout.
173
375
 
174
- If you want an easy way to manage multiple scripts at once check [tools/jungle](https://github.com/puma/puma/tree/master/tools/jungle) for init.d and upstart scripts.
376
+ For example:
175
377
 
176
- ## Capistrano deployment
378
+ ```shell
379
+ #!/bin/sh
177
380
 
178
- Puma has included Capistrano [deploy script](https://github.com/puma/puma/blob/master/lib/puma/capistrano.rb), you just need require that:
381
+ echo "this is my password"
382
+ ```
179
383
 
180
- config/deploy.rb
384
+ `key_password_command` can be used with `key` or `key_pem`. If the key
385
+ is not encrypted, the executable will not be called.
386
+
387
+ ### Control/Status Server
388
+
389
+ Puma has a built-in status and control app that can be used to query and control Puma.
181
390
 
182
- ```ruby
183
- require 'puma/capistrano'
184
391
  ```
392
+ $ puma --control-url tcp://127.0.0.1:9293 --control-token foo
393
+ ```
394
+
395
+ Puma will start the control server on localhost port 9293. All requests to the control server will need to include control token (in this case, `token=foo`) as a query parameter. This allows for simple authentication. Check out `Puma::App::Status` or [status.rb](https://github.com/puma/puma/blob/main/lib/puma/app/status.rb) to see what the status app has available.
185
396
 
186
- and then
397
+ You can also interact with the control server via `pumactl`. This command will restart Puma:
187
398
 
188
- ```bash
189
- $ bundle exec cap puma:start
190
- $ bundle exec cap puma:restart
191
- $ bundle exec cap puma:stop
192
399
  ```
400
+ $ pumactl --control-url 'tcp://127.0.0.1:9293' --control-token foo restart
401
+ ```
402
+
403
+ To see a list of `pumactl` options, use `pumactl --help`.
404
+
405
+ ### Configuration File
406
+
407
+ You can also provide a configuration file with the `-C` (or `--config`) flag:
408
+
409
+ ```
410
+ $ puma -C /path/to/config
411
+ ```
412
+
413
+ 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`.
414
+
415
+ If you want to prevent Puma from looking for a configuration file in those locations, include the `--no-config` flag:
416
+
417
+ ```
418
+ $ puma --no-config
419
+
420
+ # or
421
+
422
+ $ puma -C "-"
423
+ ```
424
+
425
+ The other side-effects of setting the environment are whether to show stack traces (in `development` or `test`), and setting RACK_ENV may potentially affect middleware looking for this value to change their behavior. The default puma RACK_ENV value is `development`. You can see all config default values in `Puma::Configuration#puma_default_options` or [configuration.rb](https://github.com/puma/puma/blob/61c6213fbab/lib/puma/configuration.rb#L182-L204).
426
+
427
+ Check out `Puma::DSL` or [dsl.rb](https://github.com/puma/puma/blob/main/lib/puma/dsl.rb) to see all available options.
428
+
429
+ ## Restart
430
+
431
+ Puma includes the ability to restart itself. When available (MRI, Rubinius, JRuby), Puma performs a "hot restart". This is the same functionality available in *Unicorn* and *NGINX* which keep the server sockets open between restarts. This makes sure that no pending requests are dropped while the restart is taking place.
432
+
433
+ For more, see the [Restart documentation](docs/restart.md).
434
+
435
+ ## Signals
436
+
437
+ Puma responds to several signals. A detailed guide to using UNIX signals with Puma can be found in the [Signals documentation](docs/signals.md).
438
+
439
+ ## Platform Constraints
440
+
441
+ Some platforms do not support all Puma features.
442
+
443
+ * **JRuby**, **Windows**: server sockets are not seamless on restart, they must be closed and reopened. These platforms have no way to pass descriptors into a new process that is exposed to Ruby. Also, cluster mode is not supported due to a lack of fork(2).
444
+ * **Windows**: Cluster mode is not supported due to a lack of fork(2).
445
+ * **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.
446
+
447
+ ## Deployment
448
+
449
+ * Puma has support for Capistrano with an [external gem](https://github.com/seuros/capistrano-puma).
450
+
451
+ * 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.
452
+
453
+
454
+ It is common to use process monitors with Puma. Modern process monitors like systemd or rc.d
455
+ provide continuous monitoring and restarts for increased reliability in production environments:
456
+
457
+ * [rc.d](docs/jungle/rc.d/README.md)
458
+ * [systemd](docs/systemd.md)
459
+
460
+ Community guides:
461
+
462
+ * [Deploying Puma on OpenBSD using relayd and httpd](https://gist.github.com/anon987654321/4532cf8d6c59c1f43ec8973faa031103)
463
+
464
+ ## Community Extensions
465
+
466
+ ### Plugins
467
+
468
+ * [puma-metrics](https://github.com/harmjanblok/puma-metrics) — export Puma metrics to Prometheus
469
+ * [puma-plugin-statsd](https://github.com/yob/puma-plugin-statsd) — send Puma metrics to statsd
470
+ * [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.
471
+ * [puma-plugin-telemetry](https://github.com/babbel/puma-plugin-telemetry) - telemetry plugin for Puma offering various targets to publish
472
+ * [puma-acme](https://github.com/anchordotdev/puma-acme) - automatic SSL/HTTPS certificate provisioning and setup
473
+
474
+ ### Monitoring
475
+
476
+ * [puma-status](https://github.com/ylecuyer/puma-status) — Monitor CPU/Mem/Load of running puma instances from the CLI
477
+
478
+ ## Contributing
193
479
 
480
+ Find details for contributing in the [contribution guide](CONTRIBUTING.md).
194
481
 
195
482
  ## License
196
483
 
197
- Puma is copyright 2013 Evan Phoenix and contributors. It is licensed under the BSD license. See the include LICENSE file for details.
484
+ Puma is copyright Evan Phoenix and contributors, licensed under the BSD 3-Clause license. See the included LICENSE file for details.