puma 2.1.1 → 7.2.1

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 (125) hide show
  1. checksums.yaml +7 -0
  2. data/History.md +3273 -0
  3. data/LICENSE +23 -20
  4. data/README.md +388 -100
  5. data/bin/puma-wild +25 -0
  6. data/bin/pumactl +1 -1
  7. data/docs/architecture.md +74 -0
  8. data/docs/compile_options.md +55 -0
  9. data/docs/deployment.md +137 -0
  10. data/docs/fork_worker.md +41 -0
  11. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  12. data/docs/images/puma-connection-flow.png +0 -0
  13. data/docs/images/puma-general-arch.png +0 -0
  14. data/docs/java_options.md +54 -0
  15. data/docs/jungle/README.md +9 -0
  16. data/docs/jungle/rc.d/README.md +74 -0
  17. data/docs/jungle/rc.d/puma +61 -0
  18. data/docs/jungle/rc.d/puma.conf +10 -0
  19. data/docs/kubernetes.md +73 -0
  20. data/docs/nginx.md +5 -5
  21. data/docs/plugins.md +42 -0
  22. data/docs/rails_dev_mode.md +28 -0
  23. data/docs/restart.md +65 -0
  24. data/docs/signals.md +98 -0
  25. data/docs/stats.md +148 -0
  26. data/docs/systemd.md +253 -0
  27. data/docs/testing_benchmarks_local_files.md +150 -0
  28. data/docs/testing_test_rackup_ci_files.md +36 -0
  29. data/ext/puma_http11/PumaHttp11Service.java +2 -2
  30. data/ext/puma_http11/extconf.rb +59 -2
  31. data/ext/puma_http11/http11_parser.c +319 -487
  32. data/ext/puma_http11/http11_parser.h +15 -13
  33. data/ext/puma_http11/http11_parser.java.rl +27 -43
  34. data/ext/puma_http11/http11_parser.rl +27 -24
  35. data/ext/puma_http11/http11_parser_common.rl +8 -8
  36. data/ext/puma_http11/mini_ssl.c +696 -38
  37. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  38. data/ext/puma_http11/org/jruby/puma/Http11.java +145 -113
  39. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +139 -172
  40. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +413 -193
  41. data/ext/puma_http11/puma_http11.c +183 -175
  42. data/lib/puma/app/status.rb +77 -29
  43. data/lib/puma/binder.rb +349 -119
  44. data/lib/puma/cli.rb +164 -769
  45. data/lib/puma/client.rb +591 -132
  46. data/lib/puma/cluster/worker.rb +183 -0
  47. data/lib/puma/cluster/worker_handle.rb +127 -0
  48. data/lib/puma/cluster.rb +634 -0
  49. data/lib/puma/cluster_accept_loop_delay.rb +91 -0
  50. data/lib/puma/commonlogger.rb +115 -0
  51. data/lib/puma/configuration.rb +370 -202
  52. data/lib/puma/const.rb +266 -115
  53. data/lib/puma/control_cli.rb +219 -120
  54. data/lib/puma/detect.rb +45 -2
  55. data/lib/puma/dsl.rb +1496 -0
  56. data/lib/puma/error_logger.rb +115 -0
  57. data/lib/puma/events.rb +44 -60
  58. data/lib/puma/io_buffer.rb +48 -5
  59. data/lib/puma/jruby_restart.rb +2 -51
  60. data/lib/puma/json_serialization.rb +96 -0
  61. data/lib/puma/launcher/bundle_pruner.rb +104 -0
  62. data/lib/puma/launcher.rb +502 -0
  63. data/lib/puma/log_writer.rb +147 -0
  64. data/lib/puma/minissl/context_builder.rb +96 -0
  65. data/lib/puma/minissl.rb +355 -37
  66. data/lib/puma/null_io.rb +79 -12
  67. data/lib/puma/plugin/systemd.rb +90 -0
  68. data/lib/puma/plugin/tmp_restart.rb +36 -0
  69. data/lib/puma/plugin.rb +111 -0
  70. data/lib/puma/rack/builder.rb +297 -0
  71. data/lib/puma/rack/urlmap.rb +93 -0
  72. data/lib/puma/rack_default.rb +21 -4
  73. data/lib/puma/reactor.rb +102 -133
  74. data/lib/puma/request.rb +703 -0
  75. data/lib/puma/runner.rb +211 -0
  76. data/lib/puma/sd_notify.rb +146 -0
  77. data/lib/puma/server.rb +544 -436
  78. data/lib/puma/single.rb +72 -0
  79. data/lib/puma/state_file.rb +69 -0
  80. data/lib/puma/thread_pool.rb +282 -56
  81. data/lib/puma/util.rb +125 -0
  82. data/lib/puma.rb +80 -6
  83. data/lib/rack/handler/puma.rb +125 -47
  84. data/tools/Dockerfile +26 -0
  85. data/tools/trickletest.rb +44 -0
  86. metadata +77 -148
  87. data/COPYING +0 -55
  88. data/Gemfile +0 -10
  89. data/History.txt +0 -291
  90. data/Manifest.txt +0 -60
  91. data/Rakefile +0 -121
  92. data/TODO +0 -5
  93. data/docs/config.md +0 -0
  94. data/ext/puma_http11/ext_help.h +0 -15
  95. data/ext/puma_http11/io_buffer.c +0 -154
  96. data/lib/puma/accept_nonblock.rb +0 -23
  97. data/lib/puma/capistrano.rb +0 -34
  98. data/lib/puma/compat.rb +0 -11
  99. data/lib/puma/daemon_ext.rb +0 -20
  100. data/lib/puma/delegation.rb +0 -11
  101. data/lib/puma/java_io_buffer.rb +0 -45
  102. data/lib/puma/rack_patch.rb +0 -25
  103. data/puma.gemspec +0 -45
  104. data/test/test_app_status.rb +0 -88
  105. data/test/test_cli.rb +0 -171
  106. data/test/test_config.rb +0 -16
  107. data/test/test_http10.rb +0 -27
  108. data/test/test_http11.rb +0 -126
  109. data/test/test_integration.rb +0 -154
  110. data/test/test_iobuffer.rb +0 -38
  111. data/test/test_minissl.rb +0 -25
  112. data/test/test_null_io.rb +0 -31
  113. data/test/test_persistent.rb +0 -238
  114. data/test/test_puma_server.rb +0 -207
  115. data/test/test_rack_handler.rb +0 -10
  116. data/test/test_rack_server.rb +0 -141
  117. data/test/test_thread_pool.rb +0 -146
  118. data/test/test_unix_socket.rb +0 -39
  119. data/test/test_ws.rb +0 -89
  120. data/tools/jungle/init.d/README.md +0 -54
  121. data/tools/jungle/init.d/puma +0 -332
  122. data/tools/jungle/init.d/run-puma +0 -3
  123. data/tools/jungle/upstart/README.md +0 -61
  124. data/tools/jungle/upstart/puma-manager.conf +0 -31
  125. 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,485 @@
1
- # Puma: A Ruby Web Server Built For Concurrency
1
+ <p align="center">
2
+ <img src="https://puma.io/images/logos/puma-logo-large.png">
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)
8
+ [![StackOverflow](https://img.shields.io/badge/stackoverflow-Puma-blue.svg)]( https://stackoverflow.com/questions/tagged/puma )
6
9
 
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.
10
+ Puma is a **simple, fast, multi-threaded, and highly parallel HTTP 1.1 server for Ruby/Rack applications**.
8
11
 
9
- ## Built For Speed &amp; Concurrency
12
+ ## Built For Speed &amp; Parallelism
10
13
 
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.
14
+ Puma is a server for [Rack](https://github.com/rack/rack)-powered HTTP applications written in Ruby. It is:
15
+ * **Multi-threaded**. Each request is served in a separate thread. This helps you serve more requests per second with less memory use.
16
+ * **Multi-process**. "Pre-forks" in cluster mode, using less memory per-process thanks to copy-on-write memory.
17
+ * **Standalone**. With SSL support, zero-downtime rolling restarts and a built-in request bufferer, you can deploy Puma without any reverse proxy.
18
+ * **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
19
 
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!
20
+ Originally designed as a server for [Rubinius](https://github.com/rubinius/rubinius), Puma also works well with Ruby (MRI) and JRuby.
14
21
 
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).
22
+ 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
23
 
19
24
  ## Quick Start
20
25
 
21
- The easiest way to get started with Puma is to install it via RubyGems. You can do this easily:
26
+ ```
27
+ $ gem install puma
28
+ $ puma
29
+ ```
30
+
31
+ Without arguments, puma will look for a rackup (.ru) file in
32
+ working directory called `config.ru`.
33
+
34
+ ## SSL Connection Support
22
35
 
23
- $ gem install puma
36
+ Puma will install/compile with support for ssl sockets, assuming OpenSSL
37
+ development files are installed on the system.
24
38
 
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:
39
+ If the system does not have OpenSSL development files installed, Puma will
40
+ install/compile, but it will not allow ssl connections.
26
41
 
27
- $ puma app.ru
42
+ ## Frameworks
28
43
 
29
- ## Advanced Setup
44
+ ### Rails
45
+
46
+ Puma is the default server for Rails, included in the generated Gemfile.
47
+
48
+ Start your server with the `rails` command:
49
+
50
+ ```
51
+ $ rails server
52
+ ```
53
+
54
+ Many configuration options and Puma features are not available when using `rails server`. It is recommended that you use Puma's executable instead:
55
+
56
+ ```
57
+ $ bundle exec puma
58
+ ```
30
59
 
31
60
  ### Sinatra
32
61
 
33
62
  You can run your Sinatra application with Puma from the command line like this:
34
63
 
35
- $ ruby app.rb -s Puma
64
+ ```
65
+ $ ruby app.rb -s Puma
66
+ ```
36
67
 
37
- Or you can configure your application to always use Puma:
68
+ 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:
69
+
70
+ ```ruby
71
+ # config.ru
72
+ require './app'
73
+ run Sinatra::Application
74
+ ```
38
75
 
39
- require 'sinatra'
40
- configure { set :server, :puma }
76
+ You can then start your application using:
41
77
 
42
- If you use Bundler, make sure you add Puma to your Gemfile (see below).
78
+ ```
79
+ $ bundle exec puma
80
+ ```
43
81
 
44
- ### Rails
82
+ ## Configuration
45
83
 
46
- First, make sure Puma is in your Gemfile:
84
+ 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
85
 
48
- gem 'puma'
86
+ You can also find several configuration examples as part of the
87
+ [test](https://github.com/puma/puma/tree/main/test/config) suite.
49
88
 
50
- Then start your server with the `rails` command:
89
+ For debugging purposes, you can set the environment variable `PUMA_LOG_CONFIG` with a value
90
+ and the loaded configuration will be printed as part of the boot process.
51
91
 
52
- $ rails s Puma
92
+ ### Thread Pool
53
93
 
54
- ### Rackup
94
+ 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
95
 
56
- You can pass it as an option to `rackup`:
96
+ ```
97
+ $ puma -t 8:32
98
+ ```
57
99
 
58
- $ rackup -s Puma
100
+ 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
101
 
60
- Alternatively, you can modify your `config.ru` to choose Puma by default, by adding the following as the first line:
102
+ 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
103
 
62
- #\ -s puma
104
+ ### Cluster mode
63
105
 
64
- ## Configuration
106
+ 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
107
 
66
- Puma provides numerous options for controlling the operation of the server. Consult `puma -h` (or `puma --help`) for a full list.
108
+ ```
109
+ $ puma -t 8:32 -w 3
110
+ ```
67
111
 
68
- ### Thread Pool
112
+ Or with the `WEB_CONCURRENCY` environment variable:
69
113
 
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:
114
+ ```
115
+ $ WEB_CONCURRENCY=3 puma -t 8:32
116
+ ```
71
117
 
72
- $ puma -t 8:32
118
+ 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
119
 
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).
120
+ ```ruby
121
+ # config/puma.rb
122
+ workers :auto
123
+ ```
75
124
 
76
- ### Clustered mode
125
+ See [`workers :auto` gotchas](lib/puma/dsl.rb).
77
126
 
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:
127
+ 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
128
 
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.
129
+ 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
130
 
85
- Additionally, you can specify a block in your configuration that will be run on boot of each worker:
131
+ For an in-depth discussion of the tradeoffs of thread and process count settings, [see our docs](docs/deployment.md).
86
132
 
87
- # config/puma.rb
88
- on_worker_boot do
89
- # configuration here
90
- end
133
+ 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
134
 
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.
135
+ 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
136
 
97
- Be sure to specify the location of your configuration file:
137
+ ```
138
+ $ puma -w 3 --preload
139
+ ```
98
140
 
99
- $ puma -t 8:32 -w 3 -C config/puma.rb
141
+ Or, if you're using a configuration file, you can use the `preload_app!` method:
142
+
143
+ ```ruby
144
+ # config/puma.rb
145
+ workers 3
146
+ preload_app!
147
+ ```
148
+
149
+ 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.
150
+
151
+ #### Cluster mode hooks
152
+
153
+ When using clustered mode, Puma's configuration DSL provides `before_fork`, `before_worker_boot`, and `after_worker_shutdown`
154
+ hooks to run code when the master process forks, the child workers are booted, and after each child worker exits respectively.
155
+
156
+ It is recommended to use these hooks with `preload_app!`, otherwise constants loaded by your
157
+ application (such as `Rails`) will not be available inside the hooks.
158
+
159
+ ```ruby
160
+ # config/puma.rb
161
+ before_fork do
162
+ # Add code to run inside the Puma master process before it forks a worker child.
163
+ end
164
+
165
+ before_worker_boot do
166
+ # Add code to run inside the Puma worker process after forking.
167
+ end
168
+
169
+ after_worker_shutdown do |worker_handle|
170
+ # Add code to run inside the Puma master process after a worker exits. `worker.process_status` can be used to get the
171
+ # `Process::Status` of the exited worker.
172
+ end
173
+ ```
174
+
175
+ In addition, there is an `before_refork` and `after_refork` hooks which are used only in [`fork_worker` mode](docs/fork_worker.md),
176
+ when the worker 0 child process forks a grandchild worker:
177
+
178
+ ```ruby
179
+ before_refork do
180
+ # Used only when fork_worker mode is enabled. Add code to run inside the Puma worker 0
181
+ # child process before it forks a grandchild worker.
182
+ end
183
+ ```
184
+
185
+ ```ruby
186
+ after_refork do
187
+ # Used only when fork_worker mode is enabled. Add code to run inside the Puma worker 0
188
+ # child process after it forks a grandchild worker.
189
+ end
190
+ ```
191
+
192
+ Importantly, note the following considerations when Ruby forks a child process:
193
+
194
+ 1. File descriptors such as network sockets **are** copied from the parent to the forked
195
+ child process. Dual-use of the same sockets by parent and child will result in I/O conflicts
196
+ such as `SocketError`, `Errno::EPIPE`, and `EOFError`.
197
+ 2. Background Ruby threads, including threads used by various third-party gems for connection
198
+ monitoring, etc., are **not** copied to the child process. Often this does not cause
199
+ immediate problems until a third-party connection goes down, at which point there will
200
+ be no supervisor to reconnect it.
201
+
202
+ Therefore, we recommend the following:
203
+
204
+ 1. If possible, do not establish any socket connections (HTTP, database connections, etc.)
205
+ inside Puma's master process when booting.
206
+ 2. If (1) is not possible, use `before_fork` and `before_refork` to disconnect the parent's socket
207
+ connections when forking, so that they are not accidentally copied to the child process.
208
+ 3. Use `before_worker_boot` to restart any background threads on the forked child.
209
+ 4. Use `after_refork` to restart any background threads on the parent.
210
+
211
+ #### Master process lifecycle hooks
212
+
213
+ Puma's configuration DSL provides master process lifecycle hooks `after_booted`, `before_restart`, and `after_stopped`
214
+ which may be used to specify code blocks to run on each event:
215
+
216
+ ```ruby
217
+ # config/puma.rb
218
+ after_booted do
219
+ # Add code to run in the Puma master process after it boots,
220
+ # and also after a phased restart completes.
221
+ end
222
+
223
+ before_restart do
224
+ # Add code to run in the Puma master process when it receives
225
+ # a restart command but before it restarts.
226
+ end
227
+
228
+ after_stopped do
229
+ # Add code to run in the Puma master process when it receives
230
+ # a stop command but before it shuts down.
231
+ end
232
+ ```
233
+
234
+ ### Error handling
235
+
236
+ If Puma encounters an error outside of the context of your application, it will respond with a 400/500 and a simple
237
+ textual error message (see `Puma::Server#lowlevel_error` or [server.rb](https://github.com/puma/puma/blob/main/lib/puma/server.rb)).
238
+ You can specify custom behavior for this scenario. For example, you can report the error to your third-party
239
+ error-tracking service (in this example, [rollbar](https://rollbar.com)):
240
+
241
+ ```ruby
242
+ lowlevel_error_handler do |e, env, status|
243
+ if status == 400
244
+ 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"
245
+ else
246
+ 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"
247
+ Rollbar.critical(e)
248
+ end
249
+
250
+ [status, {}, [message]]
251
+ end
252
+ ```
100
253
 
101
254
  ### Binding TCP / Sockets
102
255
 
103
- In contrast to many other server configs which require multiple flags, Puma simply uses one URI parameter with the `-b` (or `--bind`) flag:
256
+ Bind Puma to a socket with the `-b` (or `--bind`) flag:
104
257
 
105
- $ puma -b tcp://127.0.0.1:9292
258
+ ```
259
+ $ puma -b tcp://127.0.0.1:9292
260
+ ```
106
261
 
107
- Want to use UNIX Sockets instead of TCP (which can provide a 5-10% performance boost)? No problem!
262
+ To use a UNIX Socket instead of TCP:
108
263
 
109
- $ puma -b unix:///var/run/puma.sock
264
+ ```
265
+ $ puma -b unix:///var/run/puma.sock
266
+ ```
110
267
 
111
268
  If you need to change the permissions of the UNIX socket, just add a umask parameter:
112
269
 
113
- $ puma -b 'unix:///var/run/puma.sock?umask=0777'
270
+ ```
271
+ $ puma -b 'unix:///var/run/puma.sock?umask=0111'
272
+ ```
273
+
274
+ Need a bit of security? Use SSL sockets:
114
275
 
115
- Need a bit of security? Use SSL sockets!
276
+ ```
277
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'
278
+ ```
279
+ #### Self-signed SSL certificates (via the [`localhost`] gem, for development use):
116
280
 
117
- $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'
281
+ 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
282
 
119
- ### Control/Status Server
283
+ Puma automatically configures SSL when the [`localhost`] gem is loaded in a `development` environment:
120
284
 
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:
285
+ Add the gem to your Gemfile:
286
+ ```ruby
287
+ group(:development) do
288
+ gem 'localhost'
289
+ end
290
+ ```
122
291
 
123
- $ puma --control tcp://127.0.0.1:9293 --control-token foo
292
+ And require it implicitly using bundler:
293
+ ```ruby
294
+ require "bundler"
295
+ Bundler.require(:default, ENV["RACK_ENV"].to_sym)
296
+ ```
124
297
 
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.
298
+ 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:
299
+ ```ruby
300
+ require 'localhost'
301
+ # configuration methods (from Puma::DSL) as needed
302
+ ```
126
303
 
127
- ### Configuration file
304
+ Additionally, Puma must be listening to an SSL socket:
128
305
 
129
- You can also provide a configuration file which puma will use:
306
+ ```shell
307
+ $ puma -b 'ssl://localhost:9292' -C config/use_local_host.rb
130
308
 
131
- $ puma --config /path/to/config
309
+ # The following options allow you to reach Puma over HTTP as well:
310
+ $ puma -b ssl://localhost:9292 -b tcp://localhost:9393 -C config/use_local_host.rb
311
+ ```
132
312
 
133
- or
313
+ [`localhost`]: https://github.com/socketry/localhost
134
314
 
135
- $ puma -C /path/to/config
315
+ #### Controlling SSL Cipher Suites
136
316
 
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#L138) to see all available options.
317
+ To use or avoid specific SSL ciphers for TLSv1.2 and below, use `ssl_cipher_filter` or `ssl_cipher_list` options.
138
318
 
139
- ## Restart
319
+ ##### Ruby:
320
+
321
+ ```
322
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&ssl_cipher_filter=!aNULL:AES+SHA'
323
+ ```
324
+
325
+ ##### JRuby:
326
+
327
+ ```
328
+ $ 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'
329
+ ```
330
+
331
+ To configure the available TLSv1.3 ciphersuites, use `ssl_ciphersuites` option (not available for JRuby).
140
332
 
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.
333
+ ##### Ruby:
142
334
 
143
- To perform a restart, there are 2 builtin mechanism:
335
+ ```
336
+ $ 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'
337
+ ```
144
338
 
145
- * Send the puma process the `SIGUSR2` signal
146
- * Use the status server and issue `/restart`
339
+ See https://www.openssl.org/docs/man1.1.1/man1/ciphers.html for cipher filter format and full list of cipher suites.
147
340
 
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.
341
+ Disable TLS v1 with the `no_tlsv1` option:
149
342
 
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.
343
+ ```
344
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&no_tlsv1=true'
345
+ ```
151
346
 
152
- ### Cleanup Code
347
+ #### Controlling OpenSSL Verification Flags
153
348
 
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.
349
+ To enable verification flags offered by OpenSSL, use `verification_flags` (not available for JRuby):
155
350
 
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.
351
+ ```
352
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&verification_flags=PARTIAL_CHAIN'
353
+ ```
157
354
 
158
- ## pumactl
355
+ You can also set multiple verification flags (by separating them with a comma):
159
356
 
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:
357
+ ```
358
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&verification_flags=PARTIAL_CHAIN,CRL_CHECK'
359
+ ```
161
360
 
162
- $ pumactl -S some/path command
361
+ 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`
362
+ (see https://www.openssl.org/docs/manmaster/man3/X509_VERIFY_PARAM_set_hostflags.html#VERIFICATION-FLAGS).
163
363
 
164
- or
364
+ #### Controlling OpenSSL Password Decryption
165
365
 
166
- $ pumactl -C url -T token command
366
+ To enable runtime decryption of an encrypted SSL key (not available for JRuby), use `key_password_command`:
167
367
 
168
- will cause the server to perform a restart. `pumactl` is a simple CLI frontend to the control/status app described above.
368
+ ```
369
+ $ puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert&key_password_command=/path/to/command.sh'
370
+ ```
169
371
 
170
- Allowed commands: status, restart, halt, stop
372
+ `key_password_command` must:
171
373
 
172
- ## Managing multiple Pumas / init.d / upstart scripts
374
+ 1. Be executable by Puma.
375
+ 2. Print the decryption password to stdout.
173
376
 
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.
377
+ For example:
175
378
 
176
- ## Capistrano deployment
379
+ ```shell
380
+ #!/bin/sh
177
381
 
178
- Puma has included Capistrano [deploy script](https://github.com/puma/puma/blob/master/lib/puma/capistrano.rb), you just need require that:
382
+ echo "this is my password"
383
+ ```
179
384
 
180
- config/deploy.rb
385
+ `key_password_command` can be used with `key` or `key_pem`. If the key
386
+ is not encrypted, the executable will not be called.
387
+
388
+ ### Control/Status Server
389
+
390
+ Puma has a built-in status and control app that can be used to query and control Puma.
181
391
 
182
- ```ruby
183
- require 'puma/capistrano'
184
392
  ```
393
+ $ puma --control-url tcp://127.0.0.1:9293 --control-token foo
394
+ ```
395
+
396
+ 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
397
 
186
- and then
398
+ You can also interact with the control server via `pumactl`. This command will restart Puma:
187
399
 
188
- ```bash
189
- $ bundle exec cap puma:start
190
- $ bundle exec cap puma:restart
191
- $ bundle exec cap puma:stop
192
400
  ```
401
+ $ pumactl --control-url 'tcp://127.0.0.1:9293' --control-token foo restart
402
+ ```
403
+
404
+ To see a list of `pumactl` options, use `pumactl --help`.
405
+
406
+ ### Configuration File
407
+
408
+ You can also provide a configuration file with the `-C` (or `--config`) flag:
409
+
410
+ ```
411
+ $ puma -C /path/to/config
412
+ ```
413
+
414
+ 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`.
415
+
416
+ If you want to prevent Puma from looking for a configuration file in those locations, include the `--no-config` flag:
417
+
418
+ ```
419
+ $ puma --no-config
420
+
421
+ # or
422
+
423
+ $ puma -C "-"
424
+ ```
425
+
426
+ 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).
427
+
428
+ Check out `Puma::DSL` or [dsl.rb](https://github.com/puma/puma/blob/main/lib/puma/dsl.rb) to see all available options.
429
+
430
+ ## Restart
431
+
432
+ 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.
433
+
434
+ For more, see the [Restart documentation](docs/restart.md).
435
+
436
+ ## Signals
437
+
438
+ Puma responds to several signals. A detailed guide to using UNIX signals with Puma can be found in the [Signals documentation](docs/signals.md).
439
+
440
+ ## Platform Constraints
441
+
442
+ Some platforms do not support all Puma features.
443
+
444
+ * **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).
445
+ * **Windows**: Cluster mode is not supported due to a lack of fork(2).
446
+ * **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.
447
+
448
+ ## Deployment
449
+
450
+ * Puma has support for Capistrano with an [external gem](https://github.com/seuros/capistrano-puma).
451
+
452
+ * 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.
453
+
454
+
455
+ It is common to use process monitors with Puma. Modern process monitors like systemd or rc.d
456
+ provide continuous monitoring and restarts for increased reliability in production environments:
457
+
458
+ * [rc.d](docs/jungle/rc.d/README.md)
459
+ * [systemd](docs/systemd.md)
460
+
461
+ Community guides:
462
+
463
+ * [Deploying Puma on OpenBSD using relayd and httpd](https://gist.github.com/anon987654321/4532cf8d6c59c1f43ec8973faa031103)
464
+
465
+ ## Community Extensions
466
+
467
+ ### Plugins
468
+
469
+ * [puma-metrics](https://github.com/harmjanblok/puma-metrics) — export Puma metrics to Prometheus
470
+ * [puma-plugin-statsd](https://github.com/yob/puma-plugin-statsd) — send Puma metrics to statsd
471
+ * [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.
472
+ * [puma-plugin-telemetry](https://github.com/babbel/puma-plugin-telemetry) - telemetry plugin for Puma offering various targets to publish
473
+ * [puma-acme](https://github.com/anchordotdev/puma-acme) - automatic SSL/HTTPS certificate provisioning and setup
474
+
475
+ ### Monitoring
476
+
477
+ * [puma-status](https://github.com/ylecuyer/puma-status) — Monitor CPU/Mem/Load of running puma instances from the CLI
478
+
479
+ ## Contributing
193
480
 
481
+ Find details for contributing in the [contribution guide](CONTRIBUTING.md).
194
482
 
195
483
  ## License
196
484
 
197
- Puma is copyright 2013 Evan Phoenix and contributors. It is licensed under the BSD license. See the include LICENSE file for details.
485
+ Puma is copyright Evan Phoenix and contributors, licensed under the BSD 3-Clause license. See the included LICENSE file for details.