ed-precompiled_puma 7.0.4
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.
- checksums.yaml +7 -0
- data/History.md +3172 -0
- data/LICENSE +29 -0
- data/README.md +477 -0
- data/bin/puma +10 -0
- data/bin/puma-wild +25 -0
- data/bin/pumactl +12 -0
- data/docs/architecture.md +74 -0
- data/docs/compile_options.md +55 -0
- data/docs/deployment.md +102 -0
- data/docs/fork_worker.md +41 -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/java_options.md +54 -0
- data/docs/jungle/README.md +9 -0
- data/docs/jungle/rc.d/README.md +74 -0
- data/docs/jungle/rc.d/puma +61 -0
- data/docs/jungle/rc.d/puma.conf +10 -0
- data/docs/kubernetes.md +80 -0
- data/docs/nginx.md +80 -0
- data/docs/plugins.md +42 -0
- data/docs/rails_dev_mode.md +28 -0
- data/docs/restart.md +65 -0
- data/docs/signals.md +98 -0
- data/docs/stats.md +148 -0
- data/docs/systemd.md +253 -0
- 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 +17 -0
- data/ext/puma_http11/ext_help.h +15 -0
- data/ext/puma_http11/extconf.rb +65 -0
- data/ext/puma_http11/http11_parser.c +1057 -0
- data/ext/puma_http11/http11_parser.h +65 -0
- data/ext/puma_http11/http11_parser.java.rl +145 -0
- data/ext/puma_http11/http11_parser.rl +149 -0
- data/ext/puma_http11/http11_parser_common.rl +54 -0
- data/ext/puma_http11/mini_ssl.c +852 -0
- data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
- data/ext/puma_http11/org/jruby/puma/Http11.java +257 -0
- data/ext/puma_http11/org/jruby/puma/Http11Parser.java +455 -0
- data/ext/puma_http11/org/jruby/puma/MiniSSL.java +509 -0
- data/ext/puma_http11/puma_http11.c +507 -0
- data/lib/puma/app/status.rb +96 -0
- data/lib/puma/binder.rb +511 -0
- data/lib/puma/cli.rb +245 -0
- data/lib/puma/client.rb +720 -0
- data/lib/puma/cluster/worker.rb +182 -0
- data/lib/puma/cluster/worker_handle.rb +127 -0
- data/lib/puma/cluster.rb +635 -0
- data/lib/puma/cluster_accept_loop_delay.rb +91 -0
- data/lib/puma/commonlogger.rb +115 -0
- data/lib/puma/configuration.rb +452 -0
- data/lib/puma/const.rb +307 -0
- data/lib/puma/control_cli.rb +320 -0
- data/lib/puma/detect.rb +47 -0
- data/lib/puma/dsl.rb +1480 -0
- data/lib/puma/error_logger.rb +115 -0
- data/lib/puma/events.rb +72 -0
- data/lib/puma/io_buffer.rb +50 -0
- data/lib/puma/jruby_restart.rb +11 -0
- data/lib/puma/json_serialization.rb +96 -0
- data/lib/puma/launcher/bundle_pruner.rb +104 -0
- data/lib/puma/launcher.rb +496 -0
- data/lib/puma/log_writer.rb +147 -0
- data/lib/puma/minissl/context_builder.rb +96 -0
- data/lib/puma/minissl.rb +463 -0
- data/lib/puma/null_io.rb +101 -0
- data/lib/puma/plugin/systemd.rb +90 -0
- data/lib/puma/plugin/tmp_restart.rb +36 -0
- data/lib/puma/plugin.rb +111 -0
- data/lib/puma/rack/builder.rb +297 -0
- data/lib/puma/rack/urlmap.rb +93 -0
- data/lib/puma/rack_default.rb +24 -0
- data/lib/puma/reactor.rb +140 -0
- data/lib/puma/request.rb +701 -0
- data/lib/puma/runner.rb +211 -0
- data/lib/puma/sd_notify.rb +146 -0
- data/lib/puma/server.rb +734 -0
- data/lib/puma/single.rb +72 -0
- data/lib/puma/state_file.rb +69 -0
- data/lib/puma/thread_pool.rb +402 -0
- data/lib/puma/util.rb +134 -0
- data/lib/puma.rb +93 -0
- data/lib/rack/handler/puma.rb +144 -0
- data/tools/Dockerfile +18 -0
- data/tools/trickletest.rb +44 -0
- metadata +152 -0
data/docs/deployment.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Deployment engineering for Puma
|
2
|
+
|
3
|
+
Puma expects to be run in a deployed environment eventually. You can use it as
|
4
|
+
your development server, but most people use it in their production deployments.
|
5
|
+
|
6
|
+
To that end, this document serves as a foundation of wisdom regarding deploying
|
7
|
+
Puma to production while increasing happiness and decreasing downtime.
|
8
|
+
|
9
|
+
## Specifying Puma
|
10
|
+
|
11
|
+
Most people will specify Puma by including `gem "puma"` in a Gemfile, so we'll
|
12
|
+
assume this is how you're using Puma.
|
13
|
+
|
14
|
+
## Single vs. Cluster mode
|
15
|
+
|
16
|
+
Initially, Puma was conceived as a thread-only web server, but support for
|
17
|
+
processes was added in version 2.
|
18
|
+
|
19
|
+
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.
|
21
|
+
|
22
|
+
Here are some tips for cluster mode:
|
23
|
+
|
24
|
+
### MRI
|
25
|
+
|
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.
|
30
|
+
|
31
|
+
#### Migrating from Unicorn
|
32
|
+
|
33
|
+
* If you're migrating from unicorn though, here are some settings to start with:
|
34
|
+
* Set workers to half the number of unicorn workers you're using
|
35
|
+
* Set threads to 2
|
36
|
+
* Enjoy 50% memory savings
|
37
|
+
* As you grow more confident in the thread-safety of your app, you can tune the
|
38
|
+
workers down and the threads up.
|
39
|
+
|
40
|
+
#### Ubuntu / Systemd (Systemctl) Installation
|
41
|
+
|
42
|
+
See [systemd.md](systemd.md)
|
43
|
+
|
44
|
+
#### Worker utilization
|
45
|
+
|
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**
|
61
|
+
|
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.
|
65
|
+
|
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.
|
79
|
+
|
80
|
+
## Should I daemonize?
|
81
|
+
|
82
|
+
The Puma 5.0 release removed daemonization. For older versions and alternatives,
|
83
|
+
continue reading.
|
84
|
+
|
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
|
87
|
+
makes it easy to figure out what is going on. Additionally, unlike `unicorn`,
|
88
|
+
Puma does not require daemonization to do zero-downtime restarts.
|
89
|
+
|
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`.
|
97
|
+
|
98
|
+
## Restarting
|
99
|
+
|
100
|
+
You probably will want to deploy some new code at some point, and you'd like
|
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,41 @@
|
|
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
|
+
### Usage Considerations
|
26
|
+
|
27
|
+
- `fork_worker` introduces new `before_refork` and `after_refork` configuration hooks. Note the following:
|
28
|
+
- When initially forking the parent process to the worker 0 child, `before_fork` will trigger on the parent process and `before_worker_boot` will trigger on the worker 0 child as normal.
|
29
|
+
- When forking the worker 0 child to grandchild workers, `before_refork` and `after_refork` will trigger on the worker 0 child, and `before_worker_boot` will trigger on each grandchild worker.
|
30
|
+
- For clarity, `before_fork` does not trigger on worker 0, and `after_refork` does not trigger on the grandchild.
|
31
|
+
- As a general migration guide:
|
32
|
+
- Copy any logic within your existing `before_fork` hook to the `before_refork` hook.
|
33
|
+
- Consider to copy logic from your `before_worker_boot` hook to the `after_refork` hook, if it is needed to reset the state of worker 0 after it forks.
|
34
|
+
|
35
|
+
### Limitations
|
36
|
+
|
37
|
+
- 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.
|
38
|
+
|
39
|
+
- 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.
|
40
|
+
|
41
|
+
- 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
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# Java Options
|
2
|
+
|
3
|
+
`System Properties` or `Environment Variables` can be used to change Puma's
|
4
|
+
default configuration for its Java extension. The provided values are evaluated
|
5
|
+
during initialization, and changes while running the app have no effect.
|
6
|
+
Moreover, default values may be used in case of invalid inputs.
|
7
|
+
|
8
|
+
## Supported Options
|
9
|
+
|
10
|
+
| ENV Name | Default Value | Validation |
|
11
|
+
|------------------------------|:-------------:|:------------------------:|
|
12
|
+
| PUMA_QUERY_STRING_MAX_LENGTH | 1024 * 10 | Positive natural number |
|
13
|
+
| PUMA_REQUEST_PATH_MAX_LENGTH | 8192 | Positive natural number |
|
14
|
+
| PUMA_REQUEST_URI_MAX_LENGTH | 1024 * 12 | Positive natural number |
|
15
|
+
| PUMA_SKIP_SIGUSR2 | nil | n/a |
|
16
|
+
|
17
|
+
## Examples
|
18
|
+
|
19
|
+
### Invalid inputs
|
20
|
+
|
21
|
+
An empty string will be handled as missing, and the default value will be used instead.
|
22
|
+
Puma will print an error message for other invalid values.
|
23
|
+
|
24
|
+
```
|
25
|
+
foo@bar:~/puma$ PUMA_QUERY_STRING_MAX_LENGTH=abc PUMA_REQUEST_PATH_MAX_LENGTH='' PUMA_REQUEST_URI_MAX_LENGTH=0 bundle exec bin/puma test/rackup/hello.ru
|
26
|
+
|
27
|
+
The value 0 for PUMA_REQUEST_URI_MAX_LENGTH is invalid. Using default value 12288 instead.
|
28
|
+
The value abc for PUMA_QUERY_STRING_MAX_LENGTH is invalid. Using default value 10240 instead.
|
29
|
+
Puma starting in single mode...
|
30
|
+
```
|
31
|
+
|
32
|
+
### Valid inputs
|
33
|
+
|
34
|
+
```
|
35
|
+
foo@bar:~/puma$ PUMA_REQUEST_PATH_MAX_LENGTH=9 bundle exec bin/puma test/rackup/hello.ru
|
36
|
+
|
37
|
+
Puma starting in single mode...
|
38
|
+
```
|
39
|
+
```
|
40
|
+
foo@bar:~ export path=/123456789 # 10 chars
|
41
|
+
foo@bar:~ curl "http://localhost:9292${path}"
|
42
|
+
|
43
|
+
Puma caught this error: HTTP element REQUEST_PATH is longer than the 9 allowed length. (Puma::HttpParserError)
|
44
|
+
|
45
|
+
foo@bar:~ export path=/12345678 # 9 chars
|
46
|
+
foo@bar:~ curl "http://localhost:9292${path}"
|
47
|
+
Hello World
|
48
|
+
```
|
49
|
+
|
50
|
+
### Java Flight Recorder Compatibility
|
51
|
+
|
52
|
+
Unfortunately Java Flight Recorder uses `SIGUSR2` internally. If you wish to
|
53
|
+
use JFR, turn off Puma's trapping of `SIGUSR2` by setting the environment variable
|
54
|
+
`PUMA_SKIP_SIGUSR2` to any value.
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# Puma as a service using rc.d
|
2
|
+
|
3
|
+
Manage multiple Puma servers as services on one box using FreeBSD's rc.d service.
|
4
|
+
|
5
|
+
## Dependencies
|
6
|
+
|
7
|
+
* `jq` - a command-line json parser is needed to parse the json in the config file
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
# Copy the puma script to the rc.d directory (make sure everyone has read/execute perms)
|
12
|
+
sudo cp puma /usr/local/etc/rc.d/
|
13
|
+
|
14
|
+
# Create an empty configuration file
|
15
|
+
sudo touch /usr/local/etc/puma.conf
|
16
|
+
|
17
|
+
# Enable the puma service
|
18
|
+
sudo echo 'puma_enable="YES"' >> /etc/rc.conf
|
19
|
+
|
20
|
+
## Managing the jungle
|
21
|
+
|
22
|
+
Puma apps are referenced in /usr/local/etc/puma.conf by default.
|
23
|
+
|
24
|
+
Start the jungle running:
|
25
|
+
|
26
|
+
`service puma start`
|
27
|
+
|
28
|
+
This script will run at boot time.
|
29
|
+
|
30
|
+
|
31
|
+
You can also stop the jungle (stops ALL puma instances) by running:
|
32
|
+
|
33
|
+
`service puma stop`
|
34
|
+
|
35
|
+
|
36
|
+
To restart the jungle:
|
37
|
+
|
38
|
+
`service puma restart`
|
39
|
+
|
40
|
+
## Conventions
|
41
|
+
|
42
|
+
* The script expects:
|
43
|
+
* a config file to exist under `config/puma.rb` in your app. E.g.: `/home/apps/my-app/config/puma.rb`.
|
44
|
+
|
45
|
+
You can always change those defaults by editing the scripts.
|
46
|
+
|
47
|
+
## Here's what a minimal app's config file should have
|
48
|
+
|
49
|
+
```
|
50
|
+
{
|
51
|
+
"servers" : [
|
52
|
+
{
|
53
|
+
"dir": "/path/to/rails/project",
|
54
|
+
"user": "deploy-user",
|
55
|
+
"ruby_version": "ruby.version",
|
56
|
+
"ruby_env": "rbenv"
|
57
|
+
}
|
58
|
+
]
|
59
|
+
}
|
60
|
+
```
|
61
|
+
|
62
|
+
## Before starting...
|
63
|
+
|
64
|
+
You need to customise `puma.conf` to:
|
65
|
+
|
66
|
+
* Set the right user your app should be running on unless you want root to execute it!
|
67
|
+
* Set the directory of the app
|
68
|
+
* Set the ruby version to execute
|
69
|
+
* Set the ruby environment (currently set to rbenv, since that is the only ruby environment currently supported)
|
70
|
+
* Add additional server instances following the scheme in the example
|
71
|
+
|
72
|
+
## Notes:
|
73
|
+
|
74
|
+
Only rbenv is currently supported.
|
@@ -0,0 +1,61 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
#
|
3
|
+
|
4
|
+
# PROVIDE: puma
|
5
|
+
|
6
|
+
. /etc/rc.subr
|
7
|
+
|
8
|
+
name="puma"
|
9
|
+
start_cmd="puma_start"
|
10
|
+
stop_cmd="puma_stop"
|
11
|
+
restart_cmd="puma_restart"
|
12
|
+
rcvar=puma_enable
|
13
|
+
required_files=/usr/local/etc/puma.conf
|
14
|
+
|
15
|
+
puma_start()
|
16
|
+
{
|
17
|
+
server_count=$(/usr/local/bin/jq ".servers[] .ruby_env" /usr/local/etc/puma.conf | wc -l)
|
18
|
+
i=0
|
19
|
+
while [ "$i" -lt "$server_count" ]; do
|
20
|
+
rb_env=$(/usr/local/bin/jq -r ".servers[$i].ruby_env" /usr/local/etc/puma.conf)
|
21
|
+
dir=$(/usr/local/bin/jq -r ".servers[$i].dir" /usr/local/etc/puma.conf)
|
22
|
+
user=$(/usr/local/bin/jq -r ".servers[$i].user" /usr/local/etc/puma.conf)
|
23
|
+
rb_ver=$(/usr/local/bin/jq -r ".servers[$i].ruby_version" /usr/local/etc/puma.conf)
|
24
|
+
case $rb_env in
|
25
|
+
"rbenv")
|
26
|
+
cd $dir && rbenv shell $rb_ver && /usr/sbin/daemon -u $user bundle exec puma -C $dir/config/puma.rb
|
27
|
+
;;
|
28
|
+
*)
|
29
|
+
;;
|
30
|
+
esac
|
31
|
+
i=$(( i + 1 ))
|
32
|
+
done
|
33
|
+
}
|
34
|
+
|
35
|
+
puma_stop()
|
36
|
+
{
|
37
|
+
pkill ruby
|
38
|
+
}
|
39
|
+
|
40
|
+
puma_restart()
|
41
|
+
{
|
42
|
+
server_count=$(/usr/local/bin/jq ".servers[] .ruby_env" /usr/local/etc/puma.conf | wc -l)
|
43
|
+
i=0
|
44
|
+
while [ "$i" -lt "$server_count" ]; do
|
45
|
+
rb_env=$(/usr/local/bin/jq -r ".servers[$i].ruby_env" /usr/local/etc/puma.conf)
|
46
|
+
dir=$(/usr/local/bin/jq -r ".servers[$i].dir" /usr/local/etc/puma.conf)
|
47
|
+
user=$(/usr/local/bin/jq -r ".servers[$i].user" /usr/local/etc/puma.conf)
|
48
|
+
rb_ver=$(/usr/local/bin/jq -r ".servers[$i].ruby_version" /usr/local/etc/puma.conf)
|
49
|
+
case $rb_env in
|
50
|
+
"rbenv")
|
51
|
+
cd $dir && rbenv shell $rb_ver && /usr/sbin/daemon -u $user bundle exec puma -C $dir/config/puma.rb
|
52
|
+
;;
|
53
|
+
*)
|
54
|
+
;;
|
55
|
+
esac
|
56
|
+
i=$(( i + 1 ))
|
57
|
+
done
|
58
|
+
}
|
59
|
+
|
60
|
+
load_rc_config $name
|
61
|
+
run_rc_command "$1"
|
data/docs/kubernetes.md
ADDED
@@ -0,0 +1,80 @@
|
|
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
|
+
```
|
14
|
+
FROM ruby:3.4.5-alpine # can be updated to newer ruby versions
|
15
|
+
RUN apk update && apk add build-base # and any other packages you need
|
16
|
+
|
17
|
+
# Only rebuild gem bundle if Gemfile changes
|
18
|
+
COPY Gemfile Gemfile.lock ./
|
19
|
+
RUN bundle install
|
20
|
+
|
21
|
+
# Copy over the rest of the files
|
22
|
+
COPY . .
|
23
|
+
|
24
|
+
# Open up port and start the service
|
25
|
+
EXPOSE 9292
|
26
|
+
CMD bundle exec rackup -o 0.0.0.0
|
27
|
+
```
|
28
|
+
|
29
|
+
A sample `deployment.yaml`:
|
30
|
+
|
31
|
+
```
|
32
|
+
---
|
33
|
+
apiVersion: apps/v1
|
34
|
+
kind: Deployment
|
35
|
+
metadata:
|
36
|
+
name: my-awesome-puma-app
|
37
|
+
spec:
|
38
|
+
selector:
|
39
|
+
matchLabels:
|
40
|
+
app: my-awesome-puma-app
|
41
|
+
template:
|
42
|
+
metadata:
|
43
|
+
labels:
|
44
|
+
app: my-awesome-puma-app
|
45
|
+
service: my-awesome-puma-app
|
46
|
+
spec:
|
47
|
+
containers:
|
48
|
+
- name: my-awesome-puma-app
|
49
|
+
image: <your image here>
|
50
|
+
ports:
|
51
|
+
- containerPort: 9292
|
52
|
+
```
|
53
|
+
|
54
|
+
## Graceful shutdown and pod termination
|
55
|
+
|
56
|
+
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):
|
57
|
+
|
58
|
+
1. The replication controller determines a pod should be shut down.
|
59
|
+
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.
|
60
|
+
3. The pods pre-stop hook get called. The default for this is to send `SIGTERM` to the process inside the pod.
|
61
|
+
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.
|
62
|
+
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.
|
63
|
+
|
64
|
+
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.
|
65
|
+
|
66
|
+
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.
|
67
|
+
|
68
|
+
More discussions and links to relevant articles can be found in https://github.com/puma/puma/issues/2343.
|
69
|
+
|
70
|
+
## Workers Per Pod, and Other Config Issues
|
71
|
+
|
72
|
+
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.
|
73
|
+
|
74
|
+
* 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).
|
75
|
+
* 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.
|
76
|
+
* 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.
|
77
|
+
* 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.
|
78
|
+
* 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.
|
79
|
+
* 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).
|
80
|
+
|
data/docs/nginx.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# Nginx configuration example file
|
2
|
+
|
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
|
+
|
5
|
+
```nginx
|
6
|
+
upstream myapp {
|
7
|
+
server unix:///myapp/tmp/puma.sock;
|
8
|
+
}
|
9
|
+
|
10
|
+
server {
|
11
|
+
listen 80;
|
12
|
+
server_name myapp.com;
|
13
|
+
|
14
|
+
# ~2 seconds is often enough for most folks to parse HTML/CSS and
|
15
|
+
# retrieve needed images/icons/frames, connections are cheap in
|
16
|
+
# nginx so increasing this is generally safe...
|
17
|
+
keepalive_timeout 5;
|
18
|
+
|
19
|
+
# path for static files
|
20
|
+
root /myapp/public;
|
21
|
+
access_log /myapp/log/nginx.access.log;
|
22
|
+
error_log /myapp/log/nginx.error.log info;
|
23
|
+
|
24
|
+
# this rewrites all the requests to the maintenance.html
|
25
|
+
# page if it exists in the doc root. This is for capistrano's
|
26
|
+
# disable web task
|
27
|
+
if (-f $document_root/maintenance.html) {
|
28
|
+
rewrite ^(.*)$ /maintenance.html last;
|
29
|
+
break;
|
30
|
+
}
|
31
|
+
|
32
|
+
location / {
|
33
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
34
|
+
proxy_set_header Host $host;
|
35
|
+
|
36
|
+
# If the file exists as a static file serve it directly without
|
37
|
+
# running all the other rewrite tests on it
|
38
|
+
if (-f $request_filename) {
|
39
|
+
break;
|
40
|
+
}
|
41
|
+
|
42
|
+
# check for index.html for directory index
|
43
|
+
# if it's there on the filesystem then rewrite
|
44
|
+
# the url to add /index.html to the end of it
|
45
|
+
# and then break to send it to the next config rules.
|
46
|
+
if (-f $request_filename/index.html) {
|
47
|
+
rewrite (.*) $1/index.html break;
|
48
|
+
}
|
49
|
+
|
50
|
+
# this is the meat of the rack page caching config
|
51
|
+
# it adds .html to the end of the url and then checks
|
52
|
+
# the filesystem for that file. If it exists, then we
|
53
|
+
# rewrite the url to have explicit .html on the end
|
54
|
+
# and then send it on its way to the next config rule.
|
55
|
+
# if there is no file on the fs then it sets all the
|
56
|
+
# necessary headers and proxies to our upstream pumas
|
57
|
+
if (-f $request_filename.html) {
|
58
|
+
rewrite (.*) $1.html break;
|
59
|
+
}
|
60
|
+
|
61
|
+
if (!-f $request_filename) {
|
62
|
+
proxy_pass http://myapp;
|
63
|
+
break;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
# Now this supposedly should work as it gets the filenames with querystrings that Rails provides.
|
68
|
+
# BUT there's a chance it could break the ajax calls.
|
69
|
+
location ~* \.(ico|css|gif|jpe?g|png|js)(\?[0-9]+)?$ {
|
70
|
+
expires max;
|
71
|
+
break;
|
72
|
+
}
|
73
|
+
|
74
|
+
# Error pages
|
75
|
+
# error_page 500 502 503 504 /500.html;
|
76
|
+
location = /500.html {
|
77
|
+
root /myapp/current/public;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
```
|
data/docs/plugins.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
## Plugins
|
2
|
+
|
3
|
+
Puma 3.0 added support for plugins that can augment configuration and service
|
4
|
+
operations.
|
5
|
+
|
6
|
+
There are two canonical plugins to aid in the development of new plugins:
|
7
|
+
|
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)
|
13
|
+
|
14
|
+
Plugins are activated in a Puma configuration file (such as `config/puma.rb'`)
|
15
|
+
by adding `plugin "name"`, such as `plugin "heroku"`.
|
16
|
+
|
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).
|
20
|
+
|
21
|
+
The `tmp_restart` plugin comes with Puma, so it is always available.
|
22
|
+
|
23
|
+
To use the `heroku` plugin, add `puma-heroku` to your Gemfile or install it.
|
24
|
+
|
25
|
+
### API
|
26
|
+
|
27
|
+
## Server-wide hooks
|
28
|
+
|
29
|
+
Plugins can use a couple of hooks at the server level: `start` and `config`.
|
30
|
+
|
31
|
+
`start` runs when the server has started and allows the plugin to initiate other
|
32
|
+
functionality to augment Puma.
|
33
|
+
|
34
|
+
`config` runs when the server is being configured and receives a `Puma::DSL`
|
35
|
+
object that is useful for additional configuration.
|
36
|
+
|
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.
|