puma 3.12.6 → 6.3.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of puma might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/History.md +1806 -451
- data/LICENSE +23 -20
- data/README.md +217 -65
- data/bin/puma-wild +3 -9
- data/docs/architecture.md +59 -21
- data/docs/compile_options.md +55 -0
- data/docs/deployment.md +69 -58
- data/docs/fork_worker.md +31 -0
- data/docs/images/puma-connection-flow-no-reactor.png +0 -0
- data/docs/images/puma-connection-flow.png +0 -0
- data/docs/images/puma-general-arch.png +0 -0
- data/docs/jungle/README.md +9 -0
- data/{tools → docs}/jungle/rc.d/README.md +1 -1
- data/{tools → docs}/jungle/rc.d/puma +2 -2
- data/docs/kubernetes.md +66 -0
- data/docs/nginx.md +2 -2
- data/docs/plugins.md +22 -12
- data/docs/rails_dev_mode.md +28 -0
- data/docs/restart.md +47 -22
- data/docs/signals.md +13 -11
- data/docs/stats.md +142 -0
- data/docs/systemd.md +94 -120
- data/docs/testing_benchmarks_local_files.md +150 -0
- data/docs/testing_test_rackup_ci_files.md +36 -0
- data/ext/puma_http11/PumaHttp11Service.java +2 -2
- data/ext/puma_http11/ext_help.h +1 -1
- data/ext/puma_http11/extconf.rb +61 -3
- data/ext/puma_http11/http11_parser.c +103 -117
- data/ext/puma_http11/http11_parser.h +2 -2
- data/ext/puma_http11/http11_parser.java.rl +22 -38
- data/ext/puma_http11/http11_parser.rl +3 -3
- data/ext/puma_http11/http11_parser_common.rl +6 -6
- data/ext/puma_http11/mini_ssl.c +389 -99
- data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
- data/ext/puma_http11/org/jruby/puma/Http11.java +108 -116
- data/ext/puma_http11/org/jruby/puma/Http11Parser.java +84 -99
- data/ext/puma_http11/org/jruby/puma/MiniSSL.java +248 -92
- data/ext/puma_http11/puma_http11.c +49 -57
- data/lib/puma/app/status.rb +71 -49
- data/lib/puma/binder.rb +244 -150
- data/lib/puma/cli.rb +38 -34
- data/lib/puma/client.rb +388 -244
- data/lib/puma/cluster/worker.rb +180 -0
- data/lib/puma/cluster/worker_handle.rb +97 -0
- data/lib/puma/cluster.rb +261 -243
- data/lib/puma/commonlogger.rb +21 -14
- data/lib/puma/configuration.rb +116 -88
- data/lib/puma/const.rb +154 -104
- data/lib/puma/control_cli.rb +115 -70
- data/lib/puma/detect.rb +33 -2
- data/lib/puma/dsl.rb +764 -134
- data/lib/puma/error_logger.rb +113 -0
- data/lib/puma/events.rb +16 -112
- data/lib/puma/io_buffer.rb +42 -5
- data/lib/puma/jruby_restart.rb +2 -59
- data/lib/puma/json_serialization.rb +96 -0
- data/lib/puma/launcher/bundle_pruner.rb +104 -0
- data/lib/puma/launcher.rb +184 -133
- data/lib/puma/log_writer.rb +147 -0
- data/lib/puma/minissl/context_builder.rb +93 -0
- data/lib/puma/minissl.rb +263 -70
- data/lib/puma/null_io.rb +18 -1
- data/lib/puma/plugin/systemd.rb +90 -0
- data/lib/puma/plugin/tmp_restart.rb +3 -1
- data/lib/puma/plugin.rb +7 -13
- data/lib/puma/rack/builder.rb +9 -11
- data/lib/puma/rack/urlmap.rb +2 -0
- data/lib/puma/rack_default.rb +21 -4
- data/lib/puma/reactor.rb +93 -315
- data/lib/puma/request.rb +671 -0
- data/lib/puma/runner.rb +94 -69
- data/lib/puma/sd_notify.rb +149 -0
- data/lib/puma/server.rb +327 -772
- data/lib/puma/single.rb +20 -74
- data/lib/puma/state_file.rb +45 -8
- data/lib/puma/thread_pool.rb +146 -92
- data/lib/puma/util.rb +22 -10
- data/lib/puma.rb +60 -5
- data/lib/rack/handler/puma.rb +116 -90
- data/tools/Dockerfile +16 -0
- data/tools/trickletest.rb +0 -1
- metadata +54 -32
- data/ext/puma_http11/io_buffer.c +0 -155
- data/lib/puma/accept_nonblock.rb +0 -23
- data/lib/puma/compat.rb +0 -14
- data/lib/puma/convenient.rb +0 -25
- data/lib/puma/daemon_ext.rb +0 -33
- data/lib/puma/delegation.rb +0 -13
- data/lib/puma/java_io_buffer.rb +0 -47
- data/lib/puma/rack/backports/uri/common_193.rb +0 -33
- data/lib/puma/tcp_logger.rb +0 -41
- data/tools/jungle/README.md +0 -19
- data/tools/jungle/init.d/README.md +0 -61
- data/tools/jungle/init.d/puma +0 -421
- data/tools/jungle/init.d/run-puma +0 -18
- data/tools/jungle/upstart/README.md +0 -61
- data/tools/jungle/upstart/puma-manager.conf +0 -31
- data/tools/jungle/upstart/puma.conf +0 -69
- /data/{tools → docs}/jungle/rc.d/puma.conf +0 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
# Compile Options
|
2
|
+
|
3
|
+
There are some `cflags` provided to change Puma's default configuration for its
|
4
|
+
C extension.
|
5
|
+
|
6
|
+
## Query String, `PUMA_QUERY_STRING_MAX_LENGTH`
|
7
|
+
|
8
|
+
By default, the max length of `QUERY_STRING` is `1024 * 10`. But you may want to
|
9
|
+
adjust it to accept longer queries in GET requests.
|
10
|
+
|
11
|
+
For manual install, pass the `PUMA_QUERY_STRING_MAX_LENGTH` option like this:
|
12
|
+
|
13
|
+
```
|
14
|
+
gem install puma -- --with-cflags="-D PUMA_QUERY_STRING_MAX_LENGTH=64000"
|
15
|
+
```
|
16
|
+
|
17
|
+
For Bundler, use its configuration system:
|
18
|
+
|
19
|
+
```
|
20
|
+
bundle config build.puma "--with-cflags='-D PUMA_QUERY_STRING_MAX_LENGTH=64000'"
|
21
|
+
```
|
22
|
+
|
23
|
+
## Request Path, `PUMA_REQUEST_PATH_MAX_LENGTH`
|
24
|
+
|
25
|
+
By default, the max length of `REQUEST_PATH` is `8192`. But you may want to
|
26
|
+
adjust it to accept longer paths in requests.
|
27
|
+
|
28
|
+
For manual install, pass the `PUMA_REQUEST_PATH_MAX_LENGTH` option like this:
|
29
|
+
|
30
|
+
```
|
31
|
+
gem install puma -- --with-cflags="-D PUMA_REQUEST_PATH_MAX_LENGTH=64000"
|
32
|
+
```
|
33
|
+
|
34
|
+
For Bundler, use its configuration system:
|
35
|
+
|
36
|
+
```
|
37
|
+
bundle config build.puma "--with-cflags='-D PUMA_REQUEST_PATH_MAX_LENGTH=64000'"
|
38
|
+
```
|
39
|
+
|
40
|
+
## Request URI, `PUMA_REQUEST_URI_MAX_LENGTH`
|
41
|
+
|
42
|
+
By default, the max length of `REQUEST_URI` is `1024 * 12`. But you may want to
|
43
|
+
adjust it to accept longer URIs in requests.
|
44
|
+
|
45
|
+
For manual install, pass the `PUMA_REQUEST_URI_MAX_LENGTH` option like this:
|
46
|
+
|
47
|
+
```
|
48
|
+
gem install puma -- --with-cflags="-D PUMA_REQUEST_URI_MAX_LENGTH=64000"
|
49
|
+
```
|
50
|
+
|
51
|
+
For Bundler, use its configuration system:
|
52
|
+
|
53
|
+
```
|
54
|
+
bundle config build.puma "--with-cflags='-D PUMA_REQUEST_URI_MAX_LENGTH=64000'"
|
55
|
+
```
|
data/docs/deployment.md
CHANGED
@@ -1,33 +1,32 @@
|
|
1
|
-
# Deployment engineering for
|
1
|
+
# Deployment engineering for Puma
|
2
2
|
|
3
|
-
Puma
|
4
|
-
|
5
|
-
it in their production deployments as well.
|
3
|
+
Puma expects to be run in a deployed environment eventually. You can use it as
|
4
|
+
your development server, but most people use it in their production deployments.
|
6
5
|
|
7
|
-
To that end, this
|
8
|
-
|
6
|
+
To that end, this document serves as a foundation of wisdom regarding deploying
|
7
|
+
Puma to production while increasing happiness and decreasing downtime.
|
9
8
|
|
10
|
-
## Specifying
|
9
|
+
## Specifying Puma
|
11
10
|
|
12
|
-
Most people
|
13
|
-
|
11
|
+
Most people will specify Puma by including `gem "puma"` in a Gemfile, so we'll
|
12
|
+
assume this is how you're using Puma.
|
14
13
|
|
14
|
+
## Single vs. Cluster mode
|
15
15
|
|
16
|
-
|
16
|
+
Initially, Puma was conceived as a thread-only web server, but support for
|
17
|
+
processes was added in version 2.
|
17
18
|
|
18
|
-
|
19
|
+
To run `puma` in single mode (i.e., as a development environment), set the
|
20
|
+
number of workers to 0; anything higher will run in cluster mode.
|
19
21
|
|
20
|
-
|
21
|
-
also use processes in version 2.
|
22
|
-
|
23
|
-
Here are some rules of thumb:
|
22
|
+
Here are some tips for cluster mode:
|
24
23
|
|
25
24
|
### MRI
|
26
25
|
|
27
|
-
* Use cluster mode and set the number of workers to 1.5x the number of
|
28
|
-
in the machine, minimum 2.
|
29
|
-
* Set the number of threads to desired concurrent requests
|
30
|
-
Puma defaults to
|
26
|
+
* Use cluster mode and set the number of workers to 1.5x the number of CPU cores
|
27
|
+
in the machine, starting from a minimum of 2.
|
28
|
+
* Set the number of threads to desired concurrent requests/number of workers.
|
29
|
+
Puma defaults to 5, and that's a decent number.
|
31
30
|
|
32
31
|
#### Migrating from Unicorn
|
33
32
|
|
@@ -35,57 +34,69 @@ Here are some rules of thumb:
|
|
35
34
|
* Set workers to half the number of unicorn workers you're using
|
36
35
|
* Set threads to 2
|
37
36
|
* Enjoy 50% memory savings
|
38
|
-
* As you grow more confident in the thread
|
37
|
+
* As you grow more confident in the thread-safety of your app, you can tune the
|
39
38
|
workers down and the threads up.
|
40
39
|
|
40
|
+
#### Ubuntu / Systemd (Systemctl) Installation
|
41
|
+
|
42
|
+
See [systemd.md](systemd.md)
|
43
|
+
|
41
44
|
#### Worker utilization
|
42
45
|
|
43
|
-
**How do you know if you'
|
46
|
+
**How do you know if you've got enough (or too many workers)?**
|
47
|
+
|
48
|
+
A good question. Due to MRI's GIL, only one thread can be executing Ruby code at
|
49
|
+
a time. But since so many apps are waiting on IO from DBs, etc., they can
|
50
|
+
utilize threads to use the process more efficiently.
|
51
|
+
|
52
|
+
Generally, you never want processes that are pegged all the time. That can mean
|
53
|
+
there is more work to do than the process can get through. On the other hand, if
|
54
|
+
you have processes that sit around doing nothing, then they're just eating up
|
55
|
+
resources.
|
56
|
+
|
57
|
+
Watch your CPU utilization over time and aim for about 70% on average. 70%
|
58
|
+
utilization means you've got capacity still but aren't starving threads.
|
59
|
+
|
60
|
+
**Measuring utilization**
|
44
61
|
|
45
|
-
|
46
|
-
|
47
|
-
|
62
|
+
Using a timestamp header from an upstream proxy server (e.g., `nginx` or
|
63
|
+
`haproxy`) makes it possible to indicate how long requests have been waiting for
|
64
|
+
a Puma thread to become available.
|
48
65
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
66
|
+
* Have your upstream proxy set a header with the time it received the request:
|
67
|
+
* nginx: `proxy_set_header X-Request-Start "${msec}";`
|
68
|
+
* haproxy >= 1.9: `http-request set-header X-Request-Start
|
69
|
+
t=%[date()]%[date_us()]`
|
70
|
+
* haproxy < 1.9: `http-request set-header X-Request-Start t=%[date()]`
|
71
|
+
* In your Rack middleware, determine the amount of time elapsed since
|
72
|
+
`X-Request-Start`.
|
73
|
+
* To improve accuracy, you will want to subtract time spent waiting for slow
|
74
|
+
clients:
|
75
|
+
* `env['puma.request_body_wait']` contains the number of milliseconds Puma
|
76
|
+
spent waiting for the client to send the request body.
|
77
|
+
* haproxy: `%Th` (TLS handshake time) and `%Ti` (idle time before request)
|
78
|
+
can can also be added as headers.
|
53
79
|
|
54
|
-
|
55
|
-
you've got capacity still but aren't starving threads.
|
80
|
+
## Should I daemonize?
|
56
81
|
|
57
|
-
|
82
|
+
The Puma 5.0 release removed daemonization. For older versions and alternatives,
|
83
|
+
continue reading.
|
58
84
|
|
59
|
-
I prefer to
|
60
|
-
monitor them as child processes. This gives them fast response to crashes and
|
85
|
+
I prefer not to daemonize my servers and use something like `runit` or `systemd`
|
86
|
+
to monitor them as child processes. This gives them fast response to crashes and
|
61
87
|
makes it easy to figure out what is going on. Additionally, unlike `unicorn`,
|
62
|
-
|
88
|
+
Puma does not require daemonization to do zero-downtime restarts.
|
63
89
|
|
64
|
-
I see people using daemonization because they start puma directly via
|
65
|
-
task and thus want it to live on past the `cap deploy`. To
|
66
|
-
You need to be using a process monitor. Nothing is making sure
|
67
|
-
this scenario! You're just waiting for something weird to happen,
|
68
|
-
and to get paged at
|
69
|
-
your OS comes with, be it `sysvinit
|
70
|
-
|
90
|
+
I see people using daemonization because they start puma directly via Capistrano
|
91
|
+
task and thus want it to live on past the `cap deploy`. To these people, I say:
|
92
|
+
You need to be using a process monitor. Nothing is making sure Puma stays up in
|
93
|
+
this scenario! You're just waiting for something weird to happen, Puma to die,
|
94
|
+
and to get paged at 3 AM. Do yourself a favor, at least the process monitoring
|
95
|
+
your OS comes with, be it `sysvinit` or `systemd`. Or branch out and use `runit`
|
96
|
+
or hell, even `monit`.
|
71
97
|
|
72
98
|
## Restarting
|
73
99
|
|
74
100
|
You probably will want to deploy some new code at some point, and you'd like
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
1. Don't use `preload!`. This dirties the master process and means it will have
|
79
|
-
to shutdown all the workers and re-exec itself to get your new code. It is not compatible with phased-restart and `prune_bundler` as well.
|
80
|
-
|
81
|
-
1. Use `prune_bundler`. This makes it so that the cluster master will detach itself
|
82
|
-
from a Bundler context on start. This allows the cluster workers to load your app
|
83
|
-
and start a brand new Bundler context within the worker only. This means your
|
84
|
-
master remains pristine and can live on between new releases of your code.
|
85
|
-
|
86
|
-
1. Use phased-restart (`SIGUSR1` or `pumactl phased-restart`). This tells the master
|
87
|
-
to kill off one worker at a time and restart them in your new code. This minimizes
|
88
|
-
downtime and staggers the restart nicely. **WARNING** This means that both your
|
89
|
-
old code and your new code will be running concurrently. Most deployment solutions
|
90
|
-
already cause that, but it's worth warning you about it again. Be careful with your
|
91
|
-
migrations, etc!
|
101
|
+
Puma to start running that new code. There are a few options for restarting
|
102
|
+
Puma, described separately in our [restart documentation](restart.md).
|
data/docs/fork_worker.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Fork-Worker Cluster Mode [Experimental]
|
2
|
+
|
3
|
+
Puma 5 introduces an experimental new cluster-mode configuration option, `fork_worker` (`--fork-worker` from the CLI). This mode causes Puma to fork additional workers from worker 0, instead of directly from the master process:
|
4
|
+
|
5
|
+
```
|
6
|
+
10000 \_ puma 4.3.3 (tcp://0.0.0.0:9292) [puma]
|
7
|
+
10001 \_ puma: cluster worker 0: 10000 [puma]
|
8
|
+
10002 \_ puma: cluster worker 1: 10000 [puma]
|
9
|
+
10003 \_ puma: cluster worker 2: 10000 [puma]
|
10
|
+
10004 \_ puma: cluster worker 3: 10000 [puma]
|
11
|
+
```
|
12
|
+
|
13
|
+
The `fork_worker` option allows your application to be initialized only once for copy-on-write memory savings, and it has two additional advantages:
|
14
|
+
|
15
|
+
1. **Compatible with phased restart.** Because the master process itself doesn't preload the application, this mode works with phased restart (`SIGUSR1` or `pumactl phased-restart`). When worker 0 reloads as part of a phased restart, it initializes a new copy of your application first, then the other workers reload by forking from this new worker already containing the new preloaded application.
|
16
|
+
|
17
|
+
This allows a phased restart to complete as quickly as a hot restart (`SIGUSR2` or `pumactl restart`), while still minimizing downtime by staggering the restart across cluster workers.
|
18
|
+
|
19
|
+
2. **'Refork' for additional copy-on-write improvements in running applications.** Fork-worker mode introduces a new `refork` command that re-loads all nonzero workers by re-forking them from worker 0.
|
20
|
+
|
21
|
+
This command can potentially improve memory utilization in large or complex applications that don't fully pre-initialize on startup, because the re-forked workers can share copy-on-write memory with a worker that has been running for a while and serving requests.
|
22
|
+
|
23
|
+
You can trigger a refork by sending the cluster the `SIGURG` signal or running the `pumactl refork` command at any time. A refork will also automatically trigger once, after a certain number of requests have been processed by worker 0 (default 1000). To configure the number of requests before the auto-refork, pass a positive integer argument to `fork_worker` (e.g., `fork_worker 1000`), or `0` to disable.
|
24
|
+
|
25
|
+
### Limitations
|
26
|
+
|
27
|
+
- This mode is still very experimental so there may be bugs or edge-cases, particularly around expected behavior of existing hooks. Please open a [bug report](https://github.com/puma/puma/issues/new?template=bug_report.md) if you encounter any issues.
|
28
|
+
|
29
|
+
- In order to fork new workers cleanly, worker 0 shuts down its server and stops serving requests so there are no open file descriptors or other kinds of shared global state between processes, and to maximize copy-on-write efficiency across the newly-forked workers. This may temporarily reduce total capacity of the cluster during a phased restart / refork.
|
30
|
+
|
31
|
+
In a cluster with `n` workers, a normal phased restart stops and restarts workers one by one while the application is loaded in each process, so `n-1` workers are available serving requests during the restart. In a phased restart in fork-worker mode, the application is first loaded in worker 0 while `n-1` workers are available, then worker 0 remains stopped while the rest of the workers are reloaded one by one, leaving only `n-2` workers to be available for a brief period of time. Reloading the rest of the workers should be quick because the application is preloaded at that point, but there may be situations where it can take longer (slow clients, long-running application code, slow worker-fork hooks, etc).
|
Binary file
|
Binary file
|
Binary file
|
@@ -23,7 +23,7 @@ puma_start()
|
|
23
23
|
rb_ver=$(/usr/local/bin/jq -r ".servers[$i].ruby_version" /usr/local/etc/puma.conf)
|
24
24
|
case $rb_env in
|
25
25
|
"rbenv")
|
26
|
-
|
26
|
+
cd $dir && rbenv shell $rb_ver && /usr/sbin/daemon -u $user bundle exec puma -C $dir/config/puma.rb
|
27
27
|
;;
|
28
28
|
*)
|
29
29
|
;;
|
@@ -48,7 +48,7 @@ puma_restart()
|
|
48
48
|
rb_ver=$(/usr/local/bin/jq -r ".servers[$i].ruby_version" /usr/local/etc/puma.conf)
|
49
49
|
case $rb_env in
|
50
50
|
"rbenv")
|
51
|
-
|
51
|
+
cd $dir && rbenv shell $rb_ver && /usr/sbin/daemon -u $user bundle exec puma -C $dir/config/puma.rb
|
52
52
|
;;
|
53
53
|
*)
|
54
54
|
;;
|
data/docs/kubernetes.md
ADDED
@@ -0,0 +1,66 @@
|
|
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.
|
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 $
|
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,38 @@
|
|
1
1
|
## Plugins
|
2
2
|
|
3
|
-
Puma 3.0 added support for plugins that can augment configuration and service
|
3
|
+
Puma 3.0 added support for plugins that can augment configuration and service
|
4
|
+
operations.
|
4
5
|
|
5
|
-
|
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):
|
8
|
-
|
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
|
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
|
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
|
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
|
-
|
27
|
+
## Server-wide hooks
|
21
28
|
|
22
|
-
|
29
|
+
Plugins can use a couple of hooks at the server level: `start` and `config`.
|
23
30
|
|
24
|
-
`
|
31
|
+
`start` runs when the server has started and allows the plugin to initiate other
|
32
|
+
functionality to augment Puma.
|
25
33
|
|
26
|
-
|
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
|
-
|
37
|
+
Public methods in [`Puma::Plugin`](../lib/puma/plugin.rb) are treated as a
|
38
|
+
public API for plugins.
|
@@ -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,64 @@
|
|
1
|
-
|
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
|
-
|
3
|
+
## Hot restart
|
4
4
|
|
5
|
-
|
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
|
-
|
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
|
-
|
9
|
+
### How-to
|
12
10
|
|
13
|
-
|
11
|
+
Any of the following will cause a Puma server to perform a hot restart:
|
14
12
|
|
15
|
-
|
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
|
-
|
17
|
+
### Supported configurations
|
18
18
|
|
19
|
-
|
19
|
+
* Works in cluster mode and single mode
|
20
|
+
* Supported on all platforms
|
20
21
|
|
21
|
-
|
22
|
+
### Client experience
|
22
23
|
|
23
|
-
|
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
|
-
###
|
28
|
+
### Additional notes
|
26
29
|
|
27
|
-
|
30
|
+
* Only one version of the application is running at a time.
|
31
|
+
* `on_restart` is invoked just before the server shuts down. This can be used to clean up resources (like long-lived database connections) gracefully. Since Ruby 2.0, it is not typically necessary to explicitly close file descriptors on restart. This is because any file descriptor opened by Ruby will have the `FD_CLOEXEC` flag set, meaning that file descriptors are closed on `exec`. `on_restart` is useful, though, if your application needs to perform any more graceful protocol-specific shutdown procedures before closing connections.
|
28
32
|
|
29
|
-
|
30
|
-
# config/puma.rb
|
33
|
+
## Phased restart
|
31
34
|
|
32
|
-
|
33
|
-
```
|
35
|
+
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
36
|
|
35
|
-
###
|
37
|
+
### How-to
|
36
38
|
|
37
|
-
|
39
|
+
Any of the following will cause a Puma server to perform a phased restart:
|
38
40
|
|
39
|
-
|
41
|
+
* Send the `puma` process the `SIGUSR1` signal
|
42
|
+
* Issue a `GET` request to the Puma status/control server with the path `/phased-restart`
|
43
|
+
* Issue `pumactl phased-restart` (this uses the control server method if available, otherwise sends the `SIGUSR1` signal to the process)
|
44
|
+
|
45
|
+
### Supported configurations
|
46
|
+
|
47
|
+
* Works in cluster mode only
|
48
|
+
* To support upgrading the application that Puma is serving, ensure `prune_bundler` is enabled and that `preload_app!` is disabled
|
49
|
+
* Supported on all platforms where cluster mode is supported
|
50
|
+
|
51
|
+
### Client experience
|
52
|
+
|
53
|
+
* In-flight requests are always served responses before the connection is closed gracefully
|
54
|
+
* Idle persistent connections are gracefully disconnected
|
55
|
+
* 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)
|
56
|
+
|
57
|
+
### Additional notes
|
58
|
+
|
59
|
+
* When a phased restart begins, the Puma master process changes its current working directory to the directory specified by the `directory` option. If `directory` is set to symlink, this is automatically re-evaluated, so this mechanism can be used to upgrade the application.
|
60
|
+
* On a single server, it's possible that two versions of the application are running concurrently during a phased restart.
|
61
|
+
* `on_restart` is not invoked
|
62
|
+
* Phased restarts can be slow for Puma clusters with many workers. Hot restarts often complete more quickly, but at the cost of increased latency during the restart.
|
63
|
+
* Phased restarts cannot be used to upgrade any gems loaded by the Puma master process, including `puma` itself, anything in `extra_runtime_dependencies`, or dependencies thereof. Upgrading other gems is safe.
|
64
|
+
* 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](
|
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
|
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](
|
20
|
+
You can send a signal in Ruby using the [Process module](https://www.ruby-doc.org/core-2.1.1/Process.html#kill-method):
|
21
21
|
|
22
22
|
```
|
23
23
|
$ irb
|
24
24
|
> puts pid
|
25
25
|
=> 87152
|
26
|
-
Process.detach(pid) #
|
26
|
+
Process.detach(pid) # https://ruby-doc.org/core-2.1.1/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
|
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.
|
39
|
-
- `USR2` restart workers. This also reloads
|
40
|
-
- `USR1` restart workers in phases, a rolling restart. This will not reload configuration file.
|
41
|
-
- `HUP`
|
42
|
-
- `INT` equivalent of sending Ctrl-C to cluster.
|
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
|
|