puma 3.11.1 → 6.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. checksums.yaml +5 -5
  2. data/History.md +2092 -422
  3. data/LICENSE +23 -20
  4. data/README.md +301 -69
  5. data/bin/puma-wild +3 -9
  6. data/docs/architecture.md +59 -21
  7. data/docs/compile_options.md +55 -0
  8. data/docs/deployment.md +69 -58
  9. data/docs/fork_worker.md +41 -0
  10. data/docs/java_options.md +54 -0
  11. data/docs/jungle/README.md +9 -0
  12. data/docs/jungle/rc.d/README.md +74 -0
  13. data/docs/jungle/rc.d/puma +61 -0
  14. data/docs/jungle/rc.d/puma.conf +10 -0
  15. data/docs/kubernetes.md +78 -0
  16. data/docs/nginx.md +2 -2
  17. data/docs/plugins.md +26 -12
  18. data/docs/rails_dev_mode.md +28 -0
  19. data/docs/restart.md +48 -22
  20. data/docs/signals.md +13 -11
  21. data/docs/stats.md +147 -0
  22. data/docs/systemd.md +108 -117
  23. data/docs/testing_benchmarks_local_files.md +150 -0
  24. data/docs/testing_test_rackup_ci_files.md +36 -0
  25. data/ext/puma_http11/PumaHttp11Service.java +2 -2
  26. data/ext/puma_http11/ext_help.h +1 -1
  27. data/ext/puma_http11/extconf.rb +68 -3
  28. data/ext/puma_http11/http11_parser.c +106 -118
  29. data/ext/puma_http11/http11_parser.h +2 -2
  30. data/ext/puma_http11/http11_parser.java.rl +22 -38
  31. data/ext/puma_http11/http11_parser.rl +6 -4
  32. data/ext/puma_http11/http11_parser_common.rl +6 -6
  33. data/ext/puma_http11/mini_ssl.c +474 -94
  34. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  35. data/ext/puma_http11/org/jruby/puma/Http11.java +136 -121
  36. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +84 -99
  37. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +251 -88
  38. data/ext/puma_http11/puma_http11.c +53 -58
  39. data/lib/puma/app/status.rb +71 -49
  40. data/lib/puma/binder.rb +257 -151
  41. data/lib/puma/cli.rb +61 -38
  42. data/lib/puma/client.rb +464 -224
  43. data/lib/puma/cluster/worker.rb +183 -0
  44. data/lib/puma/cluster/worker_handle.rb +96 -0
  45. data/lib/puma/cluster.rb +343 -239
  46. data/lib/puma/commonlogger.rb +23 -14
  47. data/lib/puma/configuration.rb +144 -96
  48. data/lib/puma/const.rb +194 -115
  49. data/lib/puma/control_cli.rb +135 -81
  50. data/lib/puma/detect.rb +34 -2
  51. data/lib/puma/dsl.rb +1092 -153
  52. data/lib/puma/error_logger.rb +113 -0
  53. data/lib/puma/events.rb +17 -111
  54. data/lib/puma/io_buffer.rb +44 -5
  55. data/lib/puma/jruby_restart.rb +2 -73
  56. data/lib/puma/json_serialization.rb +96 -0
  57. data/lib/puma/launcher/bundle_pruner.rb +104 -0
  58. data/lib/puma/launcher.rb +205 -138
  59. data/lib/puma/log_writer.rb +147 -0
  60. data/lib/puma/minissl/context_builder.rb +96 -0
  61. data/lib/puma/minissl.rb +279 -70
  62. data/lib/puma/null_io.rb +61 -2
  63. data/lib/puma/plugin/systemd.rb +90 -0
  64. data/lib/puma/plugin/tmp_restart.rb +3 -1
  65. data/lib/puma/plugin.rb +9 -13
  66. data/lib/puma/rack/builder.rb +10 -11
  67. data/lib/puma/rack/urlmap.rb +3 -1
  68. data/lib/puma/rack_default.rb +21 -4
  69. data/lib/puma/reactor.rb +97 -185
  70. data/lib/puma/request.rb +688 -0
  71. data/lib/puma/runner.rb +114 -69
  72. data/lib/puma/sd_notify.rb +146 -0
  73. data/lib/puma/server.rb +409 -704
  74. data/lib/puma/single.rb +29 -72
  75. data/lib/puma/state_file.rb +48 -9
  76. data/lib/puma/thread_pool.rb +234 -93
  77. data/lib/puma/util.rb +23 -10
  78. data/lib/puma.rb +68 -5
  79. data/lib/rack/handler/puma.rb +119 -86
  80. data/tools/Dockerfile +16 -0
  81. data/tools/trickletest.rb +0 -1
  82. metadata +55 -33
  83. data/ext/puma_http11/io_buffer.c +0 -155
  84. data/lib/puma/accept_nonblock.rb +0 -23
  85. data/lib/puma/compat.rb +0 -14
  86. data/lib/puma/convenient.rb +0 -23
  87. data/lib/puma/daemon_ext.rb +0 -31
  88. data/lib/puma/delegation.rb +0 -11
  89. data/lib/puma/java_io_buffer.rb +0 -45
  90. data/lib/puma/rack/backports/uri/common_193.rb +0 -33
  91. data/lib/puma/tcp_logger.rb +0 -39
  92. data/tools/jungle/README.md +0 -13
  93. data/tools/jungle/init.d/README.md +0 -59
  94. data/tools/jungle/init.d/puma +0 -421
  95. data/tools/jungle/init.d/run-puma +0 -18
  96. data/tools/jungle/upstart/README.md +0 -61
  97. data/tools/jungle/upstart/puma-manager.conf +0 -31
  98. data/tools/jungle/upstart/puma.conf +0 -69
@@ -0,0 +1,78 @@
1
+ # Kubernetes
2
+
3
+ ## Running Puma in Kubernetes
4
+
5
+ In general running Puma in Kubernetes works as-is, no special configuration is needed beyond what you would write anyway to get a new Kubernetes Deployment going. There is one known interaction between the way Kubernetes handles pod termination and how Puma handles `SIGINT`, where some request might be sent to Puma after it has already entered graceful shutdown mode and is no longer accepting requests. This can lead to dropped requests during rolling deploys. A workaround for this is listed at the end of this article.
6
+
7
+ ## Basic setup
8
+
9
+ Assuming you already have a running cluster and docker image repository, you can run a simple Puma app with the following example Dockerfile and Deployment specification. These are meant as examples only and are deliberately very minimal to the point of skipping many options that are recommended for running in production, like healthchecks and envvar configuration with ConfigMaps. In general you should check the [Kubernetes documentation](https://kubernetes.io/docs/home/) and [Docker documentation](https://docs.docker.com/) for a more comprehensive overview of the available options.
10
+
11
+ A basic Dockerfile example:
12
+ ```
13
+ FROM ruby:2.5.1-alpine # can be updated to newer ruby versions
14
+ RUN apk update && apk add build-base # and any other packages you need
15
+
16
+ # Only rebuild gem bundle if Gemfile changes
17
+ COPY Gemfile Gemfile.lock ./
18
+ RUN bundle install
19
+
20
+ # Copy over the rest of the files
21
+ COPY . .
22
+
23
+ # Open up port and start the service
24
+ EXPOSE 9292
25
+ CMD bundle exec rackup -o 0.0.0.0
26
+ ```
27
+
28
+ A sample `deployment.yaml`:
29
+ ```
30
+ ---
31
+ apiVersion: apps/v1
32
+ kind: Deployment
33
+ metadata:
34
+ name: my-awesome-puma-app
35
+ spec:
36
+ selector:
37
+ matchLabels:
38
+ app: my-awesome-puma-app
39
+ template:
40
+ metadata:
41
+ labels:
42
+ app: my-awesome-puma-app
43
+ service: my-awesome-puma-app
44
+ spec:
45
+ containers:
46
+ - name: my-awesome-puma-app
47
+ image: <your image here>
48
+ ports:
49
+ - containerPort: 9292
50
+ ```
51
+
52
+ ## Graceful shutdown and pod termination
53
+
54
+ For some high-throughput systems, it is possible that some HTTP requests will return responses with response codes in the 5XX range during a rolling deploy to a new version. This is caused by [the way that Kubernetes terminates a pod during rolling deploys](https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-terminating-with-grace):
55
+
56
+ 1. The replication controller determines a pod should be shut down.
57
+ 2. The Pod is set to the “Terminating” State and removed from the endpoints list of all Services, so that it receives no more requests.
58
+ 3. The pods pre-stop hook get called. The default for this is to send `SIGTERM` to the process inside the pod.
59
+ 4. The pod has up to `terminationGracePeriodSeconds` (default: 30 seconds) to gracefully shut down. Puma will do this (after it receives SIGTERM) by closing down the socket that accepts new requests and finishing any requests already running before exiting the Puma process.
60
+ 5. If the pod is still running after `terminationGracePeriodSeconds` has elapsed, the pod receives `SIGKILL` to make sure the process inside it stops. After that, the container exits and all other Kubernetes objects associated with it are cleaned up.
61
+
62
+ There is a subtle race condition between step 2 and 3: The replication controller does not synchronously remove the pod from the Services AND THEN call the pre-stop hook of the pod, but rather it asynchronously sends "remove this pod from your endpoints" requests to the Services and then immediately proceeds to invoke the pods' pre-stop hook. If the Service controller (typically something like nginx or haproxy) receives this request handles this request "too" late (due to internal lag or network latency between the replication and Service controllers) then it is possible that the Service controller will send one or more requests to a Puma process which has already shut down its listening socket. These requests will then fail with 5XX error codes.
63
+
64
+ The way Kubernetes works this way, rather than handling step 2 synchronously, is due to the CAP theorem: in a distributed system there is no way to guarantee that any message will arrive promptly. In particular, waiting for all Service controllers to report back might get stuck for an indefinite time if one of them has already been terminated or if there has been a net split. A way to work around this is to add a sleep to the pre-stop hook of the same time as the `terminationGracePeriodSeconds` time. This will allow the Puma process to keep serving new requests during the entire grace period, although it will no longer receive new requests after all Service controllers have propagated the removal of the pod from their endpoint lists. Then, after `terminationGracePeriodSeconds`, the pod receives `SIGKILL` and closes down. If your process can't handle SIGKILL properly, for example because it needs to release locks in different services, you can also sleep for a shorter period (and/or increase `terminationGracePeriodSeconds`) as long as the time slept is longer than the time that your Service controllers take to propagate the pod removal. The downside of this workaround is that all pods will take at minimum the amount of time slept to shut down and this will increase the time required for your rolling deploy.
65
+
66
+ More discussions and links to relevant articles can be found in https://github.com/puma/puma/issues/2343.
67
+
68
+ ## Workers Per Pod, and Other Config Issues
69
+
70
+ With containerization, you will have to make a decision about how "big" to make each pod. Should you run 2 pods with 50 workers each? 25 pods, each with 4 workers? 100 pods, with each Puma running in single mode? Each scenario represents the same total amount of capacity (100 Puma processes that can respond to requests), but there are tradeoffs to make.
71
+
72
+ * Worker counts should be somewhere between 4 and 32 in most cases. You want more than 4 in order to minimize time spent in request queueing for a free Puma worker, but probably less than ~32 because otherwise autoscaling is working in too large of an increment or they probably won't fit very well into your nodes. In any queueing system, queue time is proportional to 1/n, where n is the number of things pulling from the queue. Each pod will have its own request queue (i.e., the socket backlog). If you have 4 pods with 1 worker each (4 request queues), wait times are, proportionally, about 4 times higher than if you had 1 pod with 4 workers (1 request queue).
73
+ * Unless you have a very I/O-heavy application (50%+ time spent waiting on IO), use the default thread count (5 for MRI). Using higher numbers of threads with low I/O wait (<50%) will lead to additional request queueing time (latency!) and additional memory usage.
74
+ * More processes per pod reduces memory usage per process, because of copy-on-write memory and because the cost of the single master process is "amortized" over more child processes.
75
+ * Don't run less than 4 processes per pod if you can. Low numbers of processes per pod will lead to high request queueing, which means you will have to run more pods.
76
+ * If multithreaded, allocate 1 CPU 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.
77
+ * 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).
78
+
data/docs/nginx.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This is a very common setup using an upstream. It was adapted from some Capistrano recipe I found on the Internet a while ago.
4
4
 
5
- ```
5
+ ```nginx
6
6
  upstream myapp {
7
7
  server unix:///myapp/tmp/puma.sock;
8
8
  }
@@ -31,7 +31,7 @@ server {
31
31
 
32
32
  location / {
33
33
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
34
- proxy_set_header Host $http_host;
34
+ proxy_set_header Host $host;
35
35
 
36
36
  # If the file exists as a static file serve it directly without
37
37
  # running all the other rewrite tests on it
data/docs/plugins.md CHANGED
@@ -1,28 +1,42 @@
1
1
  ## Plugins
2
2
 
3
- Puma 3.0 added support for plugins that can augment configuration and service operations.
3
+ Puma 3.0 added support for plugins that can augment configuration and service
4
+ operations.
4
5
 
5
- 2 canonical plugins to look to aid in development of further plugins:
6
+ There are two canonical plugins to aid in the development of new plugins:
6
7
 
7
- * [tmp\_restart](https://github.com/puma/puma/blob/master/lib/puma/plugin/tmp_restart.rb): Restarts the server if the file `tmp/restart.txt` is touched
8
- * [heroku](https://github.com/puma/puma-heroku/blob/master/lib/puma/plugin/heroku.rb): Packages up the default configuration used by puma on Heroku
8
+ * [tmp\_restart](https://github.com/puma/puma/blob/master/lib/puma/plugin/tmp_restart.rb):
9
+ Restarts the server if the file `tmp/restart.txt` is touched
10
+ * [heroku](https://github.com/puma/puma-heroku/blob/master/lib/puma/plugin/heroku.rb):
11
+ Packages up the default configuration used by Puma on Heroku (being sunset
12
+ with the release of Puma 5.0)
9
13
 
10
- Plugins are activated in a puma configuration file (such as `config/puma.rb'`) by adding `plugin "name"`, such as `plugin "heroku"`.
14
+ Plugins are activated in a Puma configuration file (such as `config/puma.rb'`)
15
+ by adding `plugin "name"`, such as `plugin "heroku"`.
11
16
 
12
- Plugins are activated based simply on path requirements so, activating the `heroku` plugin will simply be doing `require "puma/plugin/heroku"`. This allows gems to provide multiple plugins (as well as unrelated gems to provide puma plugins).
17
+ Plugins are activated based on path requirements so, activating the `heroku`
18
+ plugin is much like `require "puma/plugin/heroku"`. This allows gems to provide
19
+ multiple plugins (as well as unrelated gems to provide Puma plugins).
13
20
 
14
- The `tmp_restart` plugin is bundled with puma, so it can always be used.
21
+ The `tmp_restart` plugin comes with Puma, so it is always available.
15
22
 
16
23
  To use the `heroku` plugin, add `puma-heroku` to your Gemfile or install it.
17
24
 
18
25
  ### API
19
26
 
20
- At present, there are 2 hooks that plugins can use: `start` and `config`.
27
+ ## Server-wide hooks
21
28
 
22
- `start` runs when the server has started and allows the plugin to start other functionality to augment puma.
29
+ Plugins can use a couple of hooks at the server level: `start` and `config`.
23
30
 
24
- `config` runs when the server is being configured and is passed a `Puma::DSL` object that can be used to add additional configuration.
31
+ `start` runs when the server has started and allows the plugin to initiate other
32
+ functionality to augment Puma.
25
33
 
26
- Any public methods in `Puma::Plugin` are the public API that any plugin may use.
34
+ `config` runs when the server is being configured and receives a `Puma::DSL`
35
+ object that is useful for additional configuration.
27
36
 
28
- In the future, more hooks and APIs will be added.
37
+ Public methods in [`Puma::Plugin`](../lib/puma/plugin.rb) are treated as a
38
+ public API for plugins.
39
+
40
+ ## Binder hooks
41
+
42
+ There's `Puma::Binder#before_parse` method that allows to add proc to run before the body of `Puma::Binder#parse`. Example of usage can be found in [that repository](https://github.com/anchordotdev/puma-acme/blob/v0.1.3/lib/puma/acme/plugin.rb#L97-L118) (`before_parse_hook` could be renamed `before_parse`, making monkey patching of [binder.rb](https://github.com/anchordotdev/puma-acme/blob/v0.1.3/lib/puma/acme/binder.rb) is unnecessary).
@@ -0,0 +1,28 @@
1
+ # Running Puma in Rails Development Mode
2
+
3
+ ## "Loopback requests"
4
+
5
+ Be cautious of "loopback requests," where a Rails application executes a request to a server that, in turn, results in another request back to the same Rails application before the first request completes. Having a loopback request will trigger [Rails' load interlock](https://guides.rubyonrails.org/threading_and_code_execution.html#load-interlock) mechanism. The load interlock mechanism prevents a thread from using Rails autoloading mechanism to load constants while the application code is still running inside another thread.
6
+
7
+ This issue only occurs in the development environment as Rails' load interlock is not used in production environments. Although we're not sure, we believe this issue may not occur with the new `zeitwerk` code loader.
8
+
9
+ ### Solutions
10
+
11
+ #### 1. Bypass Rails' load interlock with `.permit_concurrent_loads`
12
+
13
+ Wrap the first request inside a block that will allow concurrent loads: [`ActiveSupport::Dependencies.interlock.permit_concurrent_loads`](https://guides.rubyonrails.org/threading_and_code_execution.html#permit-concurrent-loads). Anything wrapped inside the `.permit_concurrent_loads` block will bypass the load interlock mechanism, allowing new threads to access the Rails environment and boot properly.
14
+
15
+ ###### Example
16
+
17
+ ```ruby
18
+ response = ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
19
+ # Your HTTP request code here. For example:
20
+ Faraday.post url, data: 'foo'
21
+ end
22
+
23
+ do_something_with response
24
+ ```
25
+
26
+ #### 2. Use multiple processes on Puma
27
+
28
+ Alternatively, you may also enable multiple (single-threaded) workers on Puma. By doing so, you are sidestepping the problem by creating multiple processes rather than new threads. However, this workaround is not ideal because debugging tools such as [byebug](https://github.com/deivid-rodriguez/byebug/issues/487) and [pry](https://github.com/pry/pry/issues/2153), work poorly with any multi-process web server.
data/docs/restart.md CHANGED
@@ -1,39 +1,65 @@
1
- # Restarts
1
+ Puma provides three distinct kinds of restart operations, each for different use cases. This document describes "hot restarts" and "phased restarts." The third kind of restart operation is called "refork" and is described in the documentation for [`fork_worker`](fork_worker.md).
2
2
 
3
- To perform a restart, there are 3 builtin mechanisms:
3
+ ## Hot restart
4
4
 
5
- * Send the `puma` process the `SIGUSR2` signal
6
- * Send the `puma` process the `SIGUSR1` signal (rolling restart, cluster mode only)
7
- * Use the status server and issue `/restart`
5
+ To perform a "hot" restart, Puma performs an `exec` operation to start the process up again, so no memory is shared between the old process and the new process. As a result, it is safe to issue a restart at any place where you would manually stop Puma and start it again. In particular, it is safe to upgrade Puma itself using a hot restart.
8
6
 
9
- 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.
7
+ If the new process is unable to load, it will simply exit. You should therefore run Puma under a process monitor when using it in production.
10
8
 
11
- If the new process is unable to load, it will simply exit. You should therefore run Puma under a process monitor (see below) when using it in production.
9
+ ### How-to
12
10
 
13
- ### Normal vs Hot vs Phased Restart
11
+ Any of the following will cause a Puma server to perform a hot restart:
14
12
 
15
- A hot restart means that no requests will be lost while deploying your new code, since the server socket is kept open between restarts.
13
+ * Send the `puma` process the `SIGUSR2` signal
14
+ * Issue a `GET` request to the Puma status/control server with the path `/restart`
15
+ * Issue `pumactl restart` (this uses the control server method if available, otherwise sends the `SIGUSR2` signal to the process)
16
16
 
17
- But beware, hot restart does not mean that the incoming requests won’t hang for multiple seconds while your new code has not fully deployed. If you need a zero downtime and zero hanging requests deploy, you must use phased restart.
17
+ ### Supported configurations
18
18
 
19
- When you run pumactl phased-restart, Puma kills workers one-by-one, meaning that at least another worker is still available to serve requests, which lead to zero hanging requests (yay!).
19
+ * Works in cluster mode and single mode
20
+ * Supported on all platforms
20
21
 
21
- But again beware, upgrading an application sometimes involves upgrading the database schema. With phased restart, there may be a moment during the deployment where processes belonging to the previous version and processes belonging to the new version both exist at the same time. Any database schema upgrades you perform must therefore be backwards-compatible with the old application version.
22
+ ### Client experience
22
23
 
23
- If you perform a lot of database migrations, you probably should not use phased restart and use a normal/hot restart instead (pumactl restart). That way, no code is shared while deploying (in that case, preload_app might help for quicker deployment, see below).
24
+ * All platforms: clients with an in-flight request are served responses before the connection is closed gracefully. Puma gracefully disconnects any idle HTTP persistent connections before restarting.
25
+ * On MRI or TruffleRuby on Linux and BSD: Clients who connect just before the server restarts may experience increased latency while the server stops and starts again, but their connections will not be closed prematurely.
26
+ * On Windows and JRuby: Clients who connect just before a restart may experience "connection reset" errors.
24
27
 
25
- ### Release Directory
28
+ ### Additional notes
26
29
 
27
- If your symlink releases into a common working directory (i.e., `/current` from Capistrano), Puma won't pick up your new changes when running phased restarts without additional configuration. You should set your working directory within Puma's config to specify the directory it should use. This is a change from earlier versions of Puma (< 2.15) that would infer the directory for you.
30
+ * The newly started Puma process changes its current working directory to the directory specified by the `directory` option. If `directory` is set to symlink, this is automatically re-evaluated, so this mechanism can be used to upgrade the application.
31
+ * Only one version of the application is running at a time.
32
+ * `on_restart` is invoked just before the server shuts down. This can be used to clean up resources (like long-lived database connections) gracefully. Since Ruby 2.0, it is not typically necessary to explicitly close file descriptors on restart. This is because any file descriptor opened by Ruby will have the `FD_CLOEXEC` flag set, meaning that file descriptors are closed on `exec`. `on_restart` is useful, though, if your application needs to perform any more graceful protocol-specific shutdown procedures before closing connections.
28
33
 
29
- ```ruby
30
- # config/puma.rb
34
+ ## Phased restart
31
35
 
32
- directory '/var/www/current'
33
- ```
36
+ Phased restarts replace all running workers in a Puma cluster. This is a useful way to upgrade the application that Puma is serving gracefully. A phased restart works by first killing an old worker, then starting a new worker, waiting until the new worker has successfully started before proceeding to the next worker. This process continues until all workers are replaced. The master process is not restarted.
34
37
 
35
- ### Cleanup Code
38
+ ### How-to
36
39
 
37
- 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` called `on_restart`. The block passed to `on_restart` will be called, unsurprisingly, just before Puma restarts itself.
40
+ Any of the following will cause a Puma server to perform a phased restart:
38
41
 
39
- 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 restarted many times.
42
+ * Send the `puma` process the `SIGUSR1` signal
43
+ * Issue a `GET` request to the Puma status/control server with the path `/phased-restart`
44
+ * Issue `pumactl phased-restart` (this uses the control server method if available, otherwise sends the `SIGUSR1` signal to the process)
45
+
46
+ ### Supported configurations
47
+
48
+ * Works in cluster mode only
49
+ * To support upgrading the application that Puma is serving, ensure `prune_bundler` is enabled and that `preload_app!` is disabled
50
+ * Supported on all platforms where cluster mode is supported
51
+
52
+ ### Client experience
53
+
54
+ * In-flight requests are always served responses before the connection is closed gracefully
55
+ * Idle persistent connections are gracefully disconnected
56
+ * New connections are not lost, and clients will not experience any increase in latency (as long as the number of configured workers is greater than one)
57
+
58
+ ### Additional notes
59
+
60
+ * When a phased restart begins, the Puma master process changes its current working directory to the directory specified by the `directory` option. If `directory` is set to symlink, this is automatically re-evaluated, so this mechanism can be used to upgrade the application.
61
+ * On a single server, it's possible that two versions of the application are running concurrently during a phased restart.
62
+ * `on_restart` is not invoked
63
+ * Phased restarts can be slow for Puma clusters with many workers. Hot restarts often complete more quickly, but at the cost of increased latency during the restart.
64
+ * Phased restarts cannot be used to upgrade any gems loaded by the Puma master process, including `puma` itself, anything in `extra_runtime_dependencies`, or dependencies thereof. Upgrading other gems is safe.
65
+ * If you remove the gems from old releases as part of your deployment strategy, there are additional considerations. Do not put any gems into `extra_runtime_dependencies` that have native extensions or have dependencies that have native extensions (one common example is `puma_worker_killer` and its dependency on `ffi`). Workers will fail on boot during a phased restart. The underlying issue is recorded in [an issue on the rubygems project](https://github.com/rubygems/rubygems/issues/4004). Hot restarts are your only option here if you need these dependencies.
data/docs/signals.md CHANGED
@@ -1,8 +1,8 @@
1
- The [unix signal](http://en.wikipedia.org/wiki/Unix_signal) is a method of sending messages between [processes](http://en.wikipedia.org/wiki/Process_(computing)). When a signal is sent, the operating system interrupts the target process's normal flow of execution. There are standard signals that are used to stop a process but there are also custom signals that can be used for other purposes. This document is an attempt to list all supported signals that Puma will respond to. In general, signals need only be sent to the master process of a cluster.
1
+ The [unix signal](https://en.wikipedia.org/wiki/Unix_signal) is a method of sending messages between [processes](https://en.wikipedia.org/wiki/Process_(computing)). When a signal is sent, the operating system interrupts the target process's normal flow of execution. There are standard signals that are used to stop a process, but there are also custom signals that can be used for other purposes. This document is an attempt to list all supported signals that Puma will respond to. In general, signals need only be sent to the master process of a cluster.
2
2
 
3
3
  ## Sending Signals
4
4
 
5
- If you are new to signals it can be useful to see how they can be used. When a process is created in a *nix like operating system it will have a [PID - or process identifier](http://en.wikipedia.org/wiki/Process_identifier) that can be used to send signals to the process. For demonstration we will create an infinitely running process by tailing a file:
5
+ If you are new to signals, it can be helpful to see how they are used. When a process starts in a *nix-like operating system, it will have a [PID - or process identifier](https://en.wikipedia.org/wiki/Process_identifier) that can be used to send signals to the process. For demonstration, we will create an infinitely running process by tailing a file:
6
6
 
7
7
  ```sh
8
8
  $ echo "foo" >> my.log
@@ -10,24 +10,24 @@ $ irb
10
10
  > pid = Process.spawn 'tail -f my.log'
11
11
  ```
12
12
 
13
- From here we can see that the tail process is running by using the `ps` command:
13
+ From here, we can see that the tail process is running by using the `ps` command:
14
14
 
15
15
  ```sh
16
16
  $ ps aux | grep tail
17
17
  schneems 87152 0.0 0.0 2432772 492 s032 S+ 12:46PM 0:00.00 tail -f my.log
18
18
  ```
19
19
 
20
- You can send a signal in Ruby using the [Process module](http://www.ruby-doc.org/core-2.1.1/Process.html#kill-method):
20
+ You can send a signal in Ruby using the [Process module](https://ruby-doc.org/3.2.2/Process.html#method-c-kill):
21
21
 
22
22
  ```
23
23
  $ irb
24
24
  > puts pid
25
25
  => 87152
26
- Process.detach(pid) # http://ruby-doc.org/core-2.1.1/Process.html#method-c-detach
26
+ Process.detach(pid) # https://ruby-doc.org/3.2.2/Process.html#method-c-detach
27
27
  Process.kill("TERM", pid)
28
28
  ```
29
29
 
30
- Now you will see via `ps` that there is no more `tail` process. Sometimes when referring to signals the `SIG` prefix will be used for instance `SIGTERM` is equivalent to sending `TERM` via `Process.kill`.
30
+ Now you will see via `ps` that there is no more `tail` process. Sometimes when referring to signals, the `SIG` prefix will be used. For example, `SIGTERM` is equivalent to sending `TERM` via `Process.kill`.
31
31
 
32
32
  ## Puma Signals
33
33
 
@@ -35,12 +35,14 @@ Puma cluster responds to these signals:
35
35
 
36
36
  - `TTIN` increment the worker count by 1
37
37
  - `TTOU` decrement the worker count by 1
38
- - `TERM` send `TERM` to worker. Worker will attempt to finish then exit.
39
- - `USR2` restart workers. This also reloads puma configuration file, if there is one.
40
- - `USR1` restart workers in phases, a rolling restart. This will not reload configuration file.
41
- - `HUP` reopen log files defined in stdout_redirect configuration parameter. If there is no stdout_redirect option provided it will behave like `INT`
42
- - `INT` equivalent of sending Ctrl-C to cluster. Will attempt to finish then exit.
38
+ - `TERM` send `TERM` to worker. The worker will attempt to finish then exit.
39
+ - `USR2` restart workers. This also reloads the Puma configuration file, if there is one.
40
+ - `USR1` restart workers in phases, a rolling restart. This will not reload the configuration file.
41
+ - `HUP ` reopen log files defined in stdout_redirect configuration parameter. If there is no stdout_redirect option provided, it will behave like `INT`
42
+ - `INT ` equivalent of sending Ctrl-C to cluster. Puma will attempt to finish then exit.
43
43
  - `CHLD`
44
+ - `URG ` refork workers in phases from worker 0 if `fork_workers` option is enabled.
45
+ - `INFO` print backtraces of all puma threads
44
46
 
45
47
  ## Callbacks order in case of different signals
46
48
 
data/docs/stats.md ADDED
@@ -0,0 +1,147 @@
1
+ ## Accessing stats
2
+
3
+ Stats can be accessed in two ways:
4
+
5
+ ### control server
6
+
7
+ `$ pumactl stats` or `GET /stats`
8
+
9
+ [Read more about `pumactl` and the control server in the README.](https://github.com/puma/puma#controlstatus-server).
10
+
11
+ ### Puma.stats
12
+
13
+ `Puma.stats` produces a JSON string. `Puma.stats_hash` produces a ruby hash.
14
+
15
+ #### in single mode
16
+
17
+ Invoke `Puma.stats` anywhere in runtime, e.g. in a rails initializer:
18
+
19
+ ```ruby
20
+ # config/initializers/puma_stats.rb
21
+
22
+ Thread.new do
23
+ loop do
24
+ sleep 30
25
+ puts Puma.stats
26
+ end
27
+ end
28
+ ```
29
+
30
+ #### in cluster mode
31
+
32
+ Invoke `Puma.stats` from the master process
33
+
34
+ ```ruby
35
+ # config/puma.rb
36
+
37
+ before_fork do
38
+ Thread.new do
39
+ loop do
40
+ puts Puma.stats
41
+ sleep 30
42
+ end
43
+ end
44
+ end
45
+ ```
46
+
47
+
48
+ ## Explanation of stats
49
+
50
+ `Puma.stats` returns different information and a different structure depending on if Puma is in single vs. cluster mode. There is one top-level attribute that is common to both modes:
51
+
52
+ * started_at: when Puma was started
53
+
54
+ ### single mode and individual workers in cluster mode
55
+
56
+ When Puma runs in single mode, these stats are available at the top level. When Puma runs in cluster mode, these stats are available within the `worker_status` array in a hash labeled `last_status`, in an array of hashes where one hash represents each worker.
57
+
58
+ * backlog: requests that are waiting for an available thread to be available. if this is frequently above 0, you need more capacity.
59
+ * running: how many threads are spawned. A spawned thread may be busy processing a request or waiting for a new request. If `min_threads` and `max_threads` are set to the same number,
60
+ this will be a never-changing number (other than rare cases when a thread dies, etc).
61
+ * busy_threads: `running` - `how many threads are waiting to receive work` + `how many requests are waiting for a thread to pick them up`.
62
+ this is a "wholistic" stat reflecting the overall current state of work to be done and the capacity to do it.
63
+ * pool_capacity: `how many threads are waiting to receive work` + `max_threads` - `running`. In a typical configuration where `min_threads`
64
+ and `max_threads` are configured to the same number, this is simply `how many threads are waiting to receive work`. This number exists only as a stat
65
+ and is not used for any internal decisions, unlike `busy_theads`, which is usually a more useful stat.
66
+ * max_threads: the maximum number of threads Puma is configured to spool per worker
67
+ * requests_count: the number of requests this worker has served since starting
68
+
69
+
70
+ ### cluster mode
71
+
72
+ * phase: which phase of restart the process is in, during [phased restart](https://github.com/puma/puma/blob/master/docs/restart.md)
73
+ * workers: ??
74
+ * booted_workers: how many workers currently running?
75
+ * old_workers: ??
76
+ * worker_status: array of hashes of info for each worker (see below)
77
+
78
+ ### worker status
79
+
80
+ * started_at: when the worker started
81
+ * pid: the process id of the worker process
82
+ * index: each worker gets a number. if Puma is configured to have 3 workers, then this will be 0, 1, or 2
83
+ * booted: if it's done booting [?]
84
+ * last_checkin: Last time the worker responded to the master process' heartbeat check.
85
+ * last_status: a hash of info about the worker's state handling requests. See the explanation for this in "single mode and individual workers in cluster mode" section above.
86
+
87
+
88
+ ## Examples
89
+
90
+ Here are two example stats hashes produced by `Puma.stats`:
91
+
92
+ ### single
93
+
94
+ ```json
95
+ {
96
+ "started_at": "2021-01-14T07:12:35Z",
97
+ "backlog": 0,
98
+ "running": 5,
99
+ "pool_capacity": 5,
100
+ "max_threads": 5,
101
+ "requests_count": 3
102
+ }
103
+ ```
104
+
105
+ ### cluster
106
+
107
+ ```json
108
+ {
109
+ "started_at": "2021-01-14T07:09:17Z",
110
+ "workers": 2,
111
+ "phase": 0,
112
+ "booted_workers": 2,
113
+ "old_workers": 0,
114
+ "worker_status": [
115
+ {
116
+ "started_at": "2021-01-14T07:09:24Z",
117
+ "pid": 64136,
118
+ "index": 0,
119
+ "phase": 0,
120
+ "booted": true,
121
+ "last_checkin": "2021-01-14T07:11:09Z",
122
+ "last_status": {
123
+ "backlog": 0,
124
+ "running": 5,
125
+ "pool_capacity": 5,
126
+ "max_threads": 5,
127
+ "requests_count": 2
128
+ }
129
+ },
130
+ {
131
+ "started_at": "2021-01-14T07:09:24Z",
132
+ "pid": 64137,
133
+ "index": 1,
134
+ "phase": 0,
135
+ "booted": true,
136
+ "last_checkin": "2021-01-14T07:11:09Z",
137
+ "last_status": {
138
+ "backlog": 0,
139
+ "running": 5,
140
+ "pool_capacity": 5,
141
+ "max_threads": 5,
142
+ "requests_count": 1
143
+ }
144
+ }
145
+ ]
146
+ }
147
+ ```