puma 5.0.4 → 5.5.1

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.

Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +250 -48
  3. data/README.md +90 -24
  4. data/docs/architecture.md +57 -20
  5. data/docs/compile_options.md +21 -0
  6. data/docs/deployment.md +53 -67
  7. data/docs/fork_worker.md +2 -0
  8. data/docs/jungle/rc.d/README.md +1 -1
  9. data/docs/kubernetes.md +66 -0
  10. data/docs/plugins.md +15 -15
  11. data/docs/rails_dev_mode.md +28 -0
  12. data/docs/restart.md +7 -7
  13. data/docs/signals.md +10 -10
  14. data/docs/stats.md +142 -0
  15. data/docs/systemd.md +85 -66
  16. data/ext/puma_http11/extconf.rb +36 -6
  17. data/ext/puma_http11/http11_parser.c +64 -59
  18. data/ext/puma_http11/http11_parser.h +1 -1
  19. data/ext/puma_http11/http11_parser.java.rl +1 -1
  20. data/ext/puma_http11/http11_parser.rl +1 -1
  21. data/ext/puma_http11/http11_parser_common.rl +1 -1
  22. data/ext/puma_http11/mini_ssl.c +177 -84
  23. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +39 -41
  24. data/ext/puma_http11/puma_http11.c +8 -2
  25. data/lib/puma/app/status.rb +4 -7
  26. data/lib/puma/binder.rb +121 -46
  27. data/lib/puma/cli.rb +9 -0
  28. data/lib/puma/client.rb +58 -19
  29. data/lib/puma/cluster/worker.rb +19 -16
  30. data/lib/puma/cluster/worker_handle.rb +9 -2
  31. data/lib/puma/cluster.rb +46 -22
  32. data/lib/puma/configuration.rb +18 -2
  33. data/lib/puma/const.rb +14 -4
  34. data/lib/puma/control_cli.rb +76 -71
  35. data/lib/puma/detect.rb +14 -10
  36. data/lib/puma/dsl.rb +143 -26
  37. data/lib/puma/error_logger.rb +12 -5
  38. data/lib/puma/events.rb +18 -3
  39. data/lib/puma/json_serialization.rb +96 -0
  40. data/lib/puma/launcher.rb +54 -6
  41. data/lib/puma/minissl/context_builder.rb +6 -0
  42. data/lib/puma/minissl.rb +54 -38
  43. data/lib/puma/null_io.rb +12 -0
  44. data/lib/puma/plugin.rb +1 -1
  45. data/lib/puma/queue_close.rb +7 -7
  46. data/lib/puma/rack/builder.rb +1 -1
  47. data/lib/puma/reactor.rb +19 -12
  48. data/lib/puma/request.rb +45 -16
  49. data/lib/puma/runner.rb +38 -13
  50. data/lib/puma/server.rb +62 -123
  51. data/lib/puma/state_file.rb +5 -3
  52. data/lib/puma/systemd.rb +46 -0
  53. data/lib/puma/thread_pool.rb +10 -7
  54. data/lib/puma/util.rb +8 -1
  55. data/lib/puma.rb +36 -10
  56. data/lib/rack/handler/puma.rb +1 -0
  57. metadata +15 -9
data/docs/plugins.md CHANGED
@@ -3,22 +3,22 @@
3
3
  Puma 3.0 added support for plugins that can augment configuration and service
4
4
  operations.
5
5
 
6
- 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:
7
7
 
8
8
  * [tmp\_restart](https://github.com/puma/puma/blob/master/lib/puma/plugin/tmp_restart.rb):
9
9
  Restarts the server if the file `tmp/restart.txt` is touched
10
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
11
+ Packages up the default configuration used by Puma on Heroku (being sunset
12
+ with the release of Puma 5.0)
12
13
 
13
- Plugins are activated in a puma configuration file (such as `config/puma.rb'`)
14
+ Plugins are activated in a Puma configuration file (such as `config/puma.rb'`)
14
15
  by adding `plugin "name"`, such as `plugin "heroku"`.
15
16
 
16
- Plugins are activated based simply on path requirements so, activating the
17
- `heroku` plugin will simply be doing `require "puma/plugin/heroku"`. This
18
- allows gems to provide multiple plugins (as well as unrelated gems to provide
19
- 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).
20
20
 
21
- 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.
22
22
 
23
23
  To use the `heroku` plugin, add `puma-heroku` to your Gemfile or install it.
24
24
 
@@ -26,13 +26,13 @@ To use the `heroku` plugin, add `puma-heroku` to your Gemfile or install it.
26
26
 
27
27
  ## Server-wide hooks
28
28
 
29
- Plugins can use a couple of hooks at server level: `start` and `config`.
29
+ Plugins can use a couple of hooks at the server level: `start` and `config`.
30
30
 
31
- `start` runs when the server has started and allows the plugin to start other
32
- functionality to augment puma.
31
+ `start` runs when the server has started and allows the plugin to initiate other
32
+ functionality to augment Puma.
33
33
 
34
- `config` runs when the server is being configured and is passed a `Puma::DSL`
35
- object that can be used to add additional configuration.
34
+ `config` runs when the server is being configured and receives a `Puma::DSL`
35
+ object that is useful for additional configuration.
36
36
 
37
- Any public methods in `Puma::Plugin` are the public API that any plugin may
38
- use.
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,8 +1,8 @@
1
- Puma provides three distinct kinds of restart operations, each for different use cases. Hot restarts and phased restarts are described here. The third kind of restart operation is called "refork" and is described in the documentation for [`fork_worker`](fork_worker.md).
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
- 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 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.
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.
6
6
 
7
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.
8
8
 
@@ -16,14 +16,14 @@ Any of the following will cause a Puma server to perform a hot restart:
16
16
 
17
17
  ### Supported configurations
18
18
 
19
- * Works in cluster mode and in single mode
19
+ * Works in cluster mode and single mode
20
20
  * Supported on all platforms
21
21
 
22
22
  ### Client experience
23
23
 
24
- * All platforms: for clients with an in-flight request, those clients will be served responses before the connection is closed gracefully. Puma gracefully disconnects any idle HTTP persistent connections before restarting.
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
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 on JRuby: Clients who connect just before a restart may experience "connection reset" errors.
26
+ * On Windows and JRuby: Clients who connect just before a restart may experience "connection reset" errors.
27
27
 
28
28
  ### Additional notes
29
29
 
@@ -32,7 +32,7 @@ Any of the following will cause a Puma server to perform a hot restart:
32
32
 
33
33
  ## Phased restart
34
34
 
35
- Phased restarts replace all running workers in a Puma cluster. This is a useful way to gracefully upgrade the application that Puma is serving. 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 have been replaced. The master process is not restarted.
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.
36
36
 
37
37
  ### How-to
38
38
 
@@ -45,7 +45,7 @@ Any of the following will cause a Puma server to perform a phased restart:
45
45
  ### Supported configurations
46
46
 
47
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 (it is disabled by default).
48
+ * To support upgrading the application that Puma is serving, ensure `prune_bundler` is enabled and that `preload_app!` is disabled
49
49
  * Supported on all platforms where cluster mode is supported
50
50
 
51
51
  ### Client experience
data/docs/signals.md CHANGED
@@ -1,8 +1,8 @@
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.
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](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:
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,7 +10,7 @@ $ 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
@@ -27,7 +27,7 @@ Process.detach(pid) # https://ruby-doc.org/core-2.1.1/Process.html#method-c-deta
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,13 +35,13 @@ 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.
44
+ - `URG ` refork workers in phases from worker 0 if `fork_workers` option is enabled.
45
45
 
46
46
  ## Callbacks order in case of different signals
47
47
 
data/docs/stats.md ADDED
@@ -0,0 +1,142 @@
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 above 0, you need more capacity [always true?]
59
+ * running: how many threads are running
60
+ * pool_capacity: the number of requests that the server is capable of taking right now. For example, if the number is 5, then it means there are 5 threads sitting idle ready to take a request. If one request comes in, then the value would be 4 until it finishes processing. If the minimum threads allowed is zero, this number will still have a maximum value of the maximum threads allowed.
61
+ * max_threads: the maximum number of threads Puma is configured to spool per worker
62
+ * requests_count: the number of requests this worker has served since starting
63
+
64
+
65
+ ### cluster mode
66
+
67
+ * phase: which phase of restart the process is in, during [phased restart](https://github.com/puma/puma/blob/master/docs/restart.md)
68
+ * workers: ??
69
+ * booted_workers: how many workers currently running?
70
+ * old_workers: ??
71
+ * worker_status: array of hashes of info for each worker (see below)
72
+
73
+ ### worker status
74
+
75
+ * started_at: when the worker started
76
+ * pid: the process id of the worker process
77
+ * index: each worker gets a number. if Puma is configured to have 3 workers, then this will be 0, 1, or 2
78
+ * booted: if it's done booting [?]
79
+ * last_checkin: Last time the worker responded to the master process' heartbeat check.
80
+ * 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.
81
+
82
+
83
+ ## Examples
84
+
85
+ Here are two example stats hashes produced by `Puma.stats`:
86
+
87
+ ### single
88
+
89
+ ```json
90
+ {
91
+ "started_at": "2021-01-14T07:12:35Z",
92
+ "backlog": 0,
93
+ "running": 5,
94
+ "pool_capacity": 5,
95
+ "max_threads": 5,
96
+ "requests_count": 3
97
+ }
98
+ ```
99
+
100
+ ### cluster
101
+
102
+ ```json
103
+ {
104
+ "started_at": "2021-01-14T07:09:17Z",
105
+ "workers": 2,
106
+ "phase": 0,
107
+ "booted_workers": 2,
108
+ "old_workers": 0,
109
+ "worker_status": [
110
+ {
111
+ "started_at": "2021-01-14T07:09:24Z",
112
+ "pid": 64136,
113
+ "index": 0,
114
+ "phase": 0,
115
+ "booted": true,
116
+ "last_checkin": "2021-01-14T07:11:09Z",
117
+ "last_status": {
118
+ "backlog": 0,
119
+ "running": 5,
120
+ "pool_capacity": 5,
121
+ "max_threads": 5,
122
+ "requests_count": 2
123
+ }
124
+ },
125
+ {
126
+ "started_at": "2021-01-14T07:09:24Z",
127
+ "pid": 64137,
128
+ "index": 1,
129
+ "phase": 0,
130
+ "booted": true,
131
+ "last_checkin": "2021-01-14T07:11:09Z",
132
+ "last_status": {
133
+ "backlog": 0,
134
+ "running": 5,
135
+ "pool_capacity": 5,
136
+ "max_threads": 5,
137
+ "requests_count": 1
138
+ }
139
+ }
140
+ ]
141
+ }
142
+ ```
data/docs/systemd.md CHANGED
@@ -1,19 +1,18 @@
1
1
  # systemd
2
2
 
3
- [systemd](https://www.freedesktop.org/wiki/Software/systemd/) is a
4
- commonly available init system (PID 1) on many Linux distributions. It
5
- offers process monitoring (including automatic restarts) and other
6
- useful features for running Puma in production.
3
+ [systemd](https://www.freedesktop.org/wiki/Software/systemd/) is a commonly
4
+ available init system (PID 1) on many Linux distributions. It offers process
5
+ monitoring (including automatic restarts) and other useful features for running
6
+ Puma in production.
7
7
 
8
8
  ## Service Configuration
9
9
 
10
- Below is a sample puma.service configuration file for systemd, which
11
- can be copied or symlinked to /etc/systemd/system/puma.service, or if
12
- desired, using an application or instance specific name.
10
+ Below is a sample puma.service configuration file for systemd, which can be
11
+ copied or symlinked to `/etc/systemd/system/puma.service`, or if desired, using
12
+ an application or instance-specific name.
13
13
 
14
- Note that this uses the systemd preferred "simple" type where the
15
- start command remains running in the foreground (does not fork and
16
- exit).
14
+ Note that this uses the systemd preferred "simple" type where the start command
15
+ remains running in the foreground (does not fork and exit).
17
16
 
18
17
  ~~~~ ini
19
18
  [Unit]
@@ -24,14 +23,21 @@ After=network.target
24
23
  # Requires=puma.socket
25
24
 
26
25
  [Service]
27
- # Foreground process (do not use --daemon in ExecStart or config.rb)
28
- Type=simple
26
+ # Puma supports systemd's `Type=notify` and watchdog service
27
+ # monitoring, if the [sd_notify](https://github.com/agis/ruby-sdnotify) gem is installed,
28
+ # as of Puma 5.1 or later.
29
+ # On earlier versions of Puma or JRuby, change this to `Type=simple` and remove
30
+ # the `WatchdogSec` line.
31
+ Type=notify
32
+
33
+ # If your Puma process locks up, systemd's watchdog will restart it within seconds.
34
+ WatchdogSec=10
29
35
 
30
36
  # Preferably configure a non-privileged user
31
37
  # User=
32
38
 
33
- # The path to the your application code root directory.
34
- # Also replace the "<YOUR_APP_PATH>" place holders below with this path.
39
+ # The path to your application code root directory.
40
+ # Also replace the "<YOUR_APP_PATH>" placeholders below with this path.
35
41
  # Example /home/username/myapp
36
42
  WorkingDirectory=<YOUR_APP_PATH>
37
43
 
@@ -57,33 +63,31 @@ Restart=always
57
63
  WantedBy=multi-user.target
58
64
  ~~~~
59
65
 
60
- See [systemd.exec](https://www.freedesktop.org/software/systemd/man/systemd.exec.html)
66
+ See
67
+ [systemd.exec](https://www.freedesktop.org/software/systemd/man/systemd.exec.html)
61
68
  for additional details.
62
69
 
63
70
  ## Socket Activation
64
71
 
65
- systemd and puma also support socket activation, where systemd opens
66
- the listening socket(s) in advance and provides them to the puma
67
- master process on startup. Among other advantages, this keeps
68
- listening sockets open across puma restarts and achieves graceful
69
- restarts, including when upgraded puma, and is compatible with both
70
- clustered mode and application preload.
71
-
72
- **Note:** Any wrapper scripts which `exec`, or other indirections in
73
- `ExecStart`, may result in activated socket file descriptors being closed
74
- before they reach the puma master process. For example, if using `bundle exec`,
75
- pass the `--keep-file-descriptors` flag. `bundle exec` can be avoided by using a
76
- `puma` executable generated by `bundle binstubs puma`. This is tracked in
77
- [#1499].
78
-
79
- **Note:** Socket activation doesn't currently work on JRuby. This is
80
- tracked in [#1367].
81
-
82
- To use socket activation, configure one or more `ListenStream` sockets
83
- in a companion `*.socket` unit file. Also uncomment the associated
84
- `Requires` directive for the socket unit in the service file (see
85
- above.) Here is a sample puma.socket, matching the ports used in the
86
- above puma.service:
72
+ systemd and Puma also support socket activation, where systemd opens the
73
+ listening socket(s) in advance and provides them to the Puma master process on
74
+ startup. Among other advantages, this keeps listening sockets open across puma
75
+ restarts and achieves graceful restarts, including when upgraded Puma, and is
76
+ compatible with both clustered mode and application preload.
77
+
78
+ **Note:** Any wrapper scripts which `exec`, or other indirections in `ExecStart`
79
+ may result in activated socket file descriptors being closed before reaching the
80
+ puma master process. For example, if using `bundle exec`, pass the
81
+ `--keep-file-descriptors` flag. `bundle exec` can be avoided by using a `puma`
82
+ executable generated by `bundle binstubs puma`. This is tracked in [#1499].
83
+
84
+ **Note:** Socket activation doesn't currently work on JRuby. This is tracked in
85
+ [#1367].
86
+
87
+ Configure one or more `ListenStream` sockets in a companion `*.socket` unit file
88
+ to use socket activation. Also, uncomment the associated `Requires` directive
89
+ for the socket unit in the service file (see above.) Here is a sample
90
+ puma.socket, matching the ports used in the above puma.service:
87
91
 
88
92
  ~~~~ ini
89
93
  [Unit]
@@ -106,26 +110,42 @@ Backlog=1024
106
110
  WantedBy=sockets.target
107
111
  ~~~~
108
112
 
109
- See [systemd.socket](https://www.freedesktop.org/software/systemd/man/systemd.socket.html)
113
+ See
114
+ [systemd.socket](https://www.freedesktop.org/software/systemd/man/systemd.socket.html)
110
115
  for additional configuration details.
111
116
 
112
- Note that the above configurations will work with Puma in either
113
- single process or cluster mode.
117
+ Note that the above configurations will work with Puma in either single process
118
+ or cluster mode.
114
119
 
115
120
  ### Sockets and symlinks
116
121
 
117
- When using releases folders, you should set the socket path using the
118
- shared folder path (ex. `/srv/projet/shared/tmp/puma.sock`), not the
119
- release folder path (`/srv/projet/releases/1234/tmp/puma.sock`).
122
+ When using releases folders, you should set the socket path using the shared
123
+ folder path (ex. `/srv/projet/shared/tmp/puma.sock`), not the release folder
124
+ path (`/srv/projet/releases/1234/tmp/puma.sock`).
120
125
 
121
126
  Puma will detect the release path socket as different than the one provided by
122
- systemd and attempt to bind it again, resulting in the exception
123
- `There is already a server bound to:`.
127
+ systemd and attempt to bind it again, resulting in the exception `There is
128
+ already a server bound to:`.
129
+
130
+ ### Binding
131
+
132
+ By default, you need to configure Puma to have binds matching with all
133
+ ListenStream statements. Any mismatched systemd ListenStreams will be closed by
134
+ Puma.
135
+
136
+ To automatically bind to all activated sockets, the option
137
+ `--bind-to-activated-sockets` can be used. This matches the config DSL
138
+ `bind_to_activated_sockets` statement. This will cause Puma to create a bind
139
+ automatically for any activated socket. When systemd socket activation is not
140
+ enabled, this option does nothing.
141
+
142
+ This also accepts an optional argument `only` (DSL: `'only'`) to discard any
143
+ binds that's not socket activated.
124
144
 
125
145
  ## Usage
126
146
 
127
- Without socket activation, use `systemctl` as root (e.g. via `sudo`) as
128
- with other system services:
147
+ Without socket activation, use `systemctl` as root (i.e., via `sudo`) as with
148
+ other system services:
129
149
 
130
150
  ~~~~ sh
131
151
  # After installing or making changes to puma.service
@@ -134,35 +154,35 @@ systemctl daemon-reload
134
154
  # Enable so it starts on boot
135
155
  systemctl enable puma.service
136
156
 
137
- # Initial start up.
157
+ # Initial startup.
138
158
  systemctl start puma.service
139
159
 
140
160
  # Check status
141
161
  systemctl status puma.service
142
162
 
143
- # A normal restart. Warning: listeners sockets will be closed
163
+ # A normal restart. Warning: listener's sockets will be closed
144
164
  # while a new puma process initializes.
145
165
  systemctl restart puma.service
146
166
  ~~~~
147
167
 
148
- With socket activation, several but not all of these commands should
149
- be run for both socket and service:
168
+ With socket activation, several but not all of these commands should be run for
169
+ both socket and service:
150
170
 
151
171
  ~~~~ sh
152
172
  # After installing or making changes to either puma.socket or
153
173
  # puma.service.
154
174
  systemctl daemon-reload
155
175
 
156
- # Enable both socket and service so they start on boot. Alternatively
157
- # you could leave puma.service disabled and systemd will start it on
158
- # first use (with startup lag on first request)
176
+ # Enable both socket and service, so they start on boot. Alternatively
177
+ # you could leave puma.service disabled, and systemd will start it on
178
+ # the first use (with startup lag on the first request)
159
179
  systemctl enable puma.socket puma.service
160
180
 
161
- # Initial start up. The Requires directive (see above) ensures the
181
+ # Initial startup. The Requires directive (see above) ensures the
162
182
  # socket is started before the service.
163
183
  systemctl start puma.socket puma.service
164
184
 
165
- # Check status of both socket and service.
185
+ # Check the status of both socket and service.
166
186
  systemctl status puma.socket puma.service
167
187
 
168
188
  # A "hot" restart, with systemd keeping puma.socket listening and
@@ -175,8 +195,8 @@ systemctl restart puma.service
175
195
  systemctl restart puma.socket puma.service
176
196
  ~~~~
177
197
 
178
- Here is sample output from `systemctl status` with both service and
179
- socket running:
198
+ Here is sample output from `systemctl status` with both service and socket
199
+ running:
180
200
 
181
201
  ~~~~
182
202
  ● puma.socket - Puma HTTP Server Accept Sockets
@@ -209,14 +229,12 @@ Apr 07 08:40:19 hx puma[28320]: Use Ctrl-C to stop
209
229
 
210
230
  ### capistrano3-puma
211
231
 
212
- By default,
213
- [capistrano3-puma](https://github.com/seuros/capistrano-puma) uses
214
- `pumactl` for deployment restarts, outside of systemd. To learn the
215
- exact commands that this tool would use for `ExecStart` and
216
- `ExecStop`, use the following `cap` commands in dry-run mode, and
217
- update from the above forking service configuration accordingly. Note
218
- also that the configured `User` should likely be the same as the
219
- capistrano3-puma `:puma_user` option.
232
+ By default, [capistrano3-puma](https://github.com/seuros/capistrano-puma) uses
233
+ `pumactl` for deployment restarts outside of systemd. To learn the exact
234
+ commands that this tool would use for `ExecStart` and `ExecStop`, use the
235
+ following `cap` commands in dry-run mode, and update from the above forking
236
+ service configuration accordingly. Note also that the configured `User` should
237
+ likely be the same as the capistrano3-puma `:puma_user` option.
220
238
 
221
239
  ~~~~ sh
222
240
  stage=production # or different stage, as needed
@@ -226,3 +244,4 @@ cap $stage puma:stop --dry-run
226
244
 
227
245
  [Restart]: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Restart=
228
246
  [#1367]: https://github.com/puma/puma/issues/1367
247
+ [#1499]: https://github.com/puma/puma/issues/1499
@@ -11,18 +11,48 @@ end
11
11
  unless ENV["DISABLE_SSL"]
12
12
  dir_config("openssl")
13
13
 
14
- if %w'crypto libeay32'.find {|crypto| have_library(crypto, 'BIO_read')} and
14
+ found_ssl = if pkg_config 'openssl'
15
+ puts 'using OpenSSL pkgconfig (openssl.pc)'
16
+ true
17
+ elsif %w'crypto libeay32'.find {|crypto| have_library(crypto, 'BIO_read')} &&
15
18
  %w'ssl ssleay32'.find {|ssl| have_library(ssl, 'SSL_CTX_new')}
19
+ true
20
+ else
21
+ puts '** Puma will be compiled without SSL support'
22
+ false
23
+ end
16
24
 
25
+ if found_ssl
17
26
  have_header "openssl/bio.h"
18
27
 
19
28
  # below is yes for 1.0.2 & later
20
- have_func "DTLS_method" , "openssl/ssl.h"
29
+ have_func "DTLS_method" , "openssl/ssl.h"
30
+
31
+ # below are yes for 1.1.0 & later
32
+ have_func "TLS_server_method" , "openssl/ssl.h"
33
+ have_func "SSL_CTX_set_min_proto_version(NULL, 0)", "openssl/ssl.h"
34
+
35
+ have_func "X509_STORE_up_ref"
36
+ have_func("SSL_CTX_set_ecdh_auto(NULL, 0)", "openssl/ssl.h")
37
+
38
+ # Random.bytes available in Ruby 2.5 and later, Random::DEFAULT deprecated in 3.0
39
+ if Random.respond_to?(:bytes)
40
+ $defs.push("-DHAVE_RANDOM_BYTES")
41
+ puts "checking for Random.bytes... yes"
42
+ else
43
+ puts "checking for Random.bytes... no"
44
+ end
45
+ end
46
+ end
21
47
 
22
- # below are yes for 1.1.0 & later, may need to check func rather than macro
23
- # with versions after 1.1.1
24
- have_func "TLS_server_method" , "openssl/ssl.h"
25
- have_macro "SSL_CTX_set_min_proto_version", "openssl/ssl.h"
48
+ if ENV["MAKE_WARNINGS_INTO_ERRORS"]
49
+ # Make all warnings into errors
50
+ # Except `implicit-fallthrough` since most failures comes from ragel state machine generated code
51
+ if respond_to? :append_cflags
52
+ append_cflags config_string 'WERRORFLAG'
53
+ append_cflags '-Wno-implicit-fallthrough'
54
+ else
55
+ $CFLAGS += ' ' << (config_string 'WERRORFLAG') << ' -Wno-implicit-fallthrough'
26
56
  end
27
57
  end
28
58