From d4a29675bb648c1c26c1bb240de0692551b989ba Mon Sep 17 00:00:00 2001 From: Jacob Herrington Date: Sat, 25 Sep 2021 18:18:28 -0500 Subject: [PATCH] Clean up and format markdown documentation (#2714) * Revise and format architecture docs [ci skip] * Fix typo and format compile options doc [ci skip] * Revise and format deployment doc [ci skip] * Revise and format plugins doc [ci skip] * Fix mechanical typos [ci skip] * Revise restart.md [ci skip] * Revise signals.md [ci skip] * Revise stats.md [ci skip] * Revise and format systemd.md [ci skip] --- docs/architecture.md | 65 +++++++++++++++----- docs/compile_options.md | 6 +- docs/deployment.md | 105 ++++++++++++++++---------------- docs/plugins.md | 30 +++++----- docs/rails_dev_mode.md | 5 +- docs/restart.md | 12 ++-- docs/signals.md | 20 +++---- docs/stats.md | 16 ++--- docs/systemd.md | 130 +++++++++++++++++++--------------------- 9 files changed, 210 insertions(+), 179 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 22dd8ad728..e4fb571090 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -4,38 +4,71 @@ ![https://bit.ly/2iJuFky](images/puma-general-arch.png) -Puma is a threaded Ruby HTTP application server, processing requests across a TCP or UNIX socket. +Puma is a threaded Ruby HTTP application server processing requests across a TCP +and/or UNIX socket. -Puma processes (there can be one or many) accept connections from the socket via a thread (in the [`Reactor`](../lib/puma/reactor.rb) class). The connection, once fully buffered and read, moves in to the `todo` list, where it will be picked up by a free/waiting thread in the threadpool (the [`ThreadPool`](../lib/puma/thread_pool.rb) class). +Puma processes (there can be one or many) accept connections from the socket via +a thread (in the [`Reactor`](../lib/puma/reactor.rb) class). The connection, +once fully buffered and read, moves into the `todo` list, where an available +thread will pick it up (in the [`ThreadPool`](../lib/puma/thread_pool.rb) +class). -Puma works in two main modes: cluster and single. In single mode, only one Puma process is booted. In cluster mode, a `master` process is booted, which prepares (and may boot) the application, and then uses the `fork()` system call to create 1 or more `child` processes. These `child` processes all listen to the same socket. The `master` process does not listen to the socket or process requests - its purpose is mostly to manage and listen for UNIX signals and possibly kill or boot `child` processes. +Puma works in two main modes: cluster and single. In single mode, only one Puma +process boots. In cluster mode, a `master` process is booted, which prepares +(and may boot) the application and then uses the `fork()` system call to create +one or more `child` processes. These `child` processes all listen to the same +socket. The `master` process does not listen to the socket or process requests - +its purpose is primarily to manage and listen for UNIX signals and possibly kill +or boot `child` processes. -We sometimes call `child` processes (or Puma processes in `single` mode) _workers_, and we sometimes call the threads created by Puma's [`ThreadPool`](../lib/puma/thread_pool.rb) _worker threads_. +We sometimes call `child` processes (or Puma processes in `single` mode) +_workers_, and we sometimes call the threads created by Puma's +[`ThreadPool`](../lib/puma/thread_pool.rb) _worker threads_. ## How Requests Work ![https://bit.ly/2zwzhEK](images/puma-connection-flow.png) * Upon startup, Puma listens on a TCP or UNIX socket. - * The backlog of this socket is configured (with a default of 1024). This determines the size of the queue for unaccepted connections. Generally, this setting is unimportant and will never be hit in production use. If the backlog is full, the connection will be refused by the operating system. - * This socket backlog is distinct from the `backlog` of work as reported by `Puma.stats` or the control server. The backlog as reported by Puma is the number of connections in the process' `todo` set waiting for a thread from the [`ThreadPool`](../lib/puma/thread_pool.rb). -* By default, a single, separate thread (created by the [`Reactor`](../lib/puma/reactor.rb) class) is used to read and buffer requests from the socket. - * When at least one worker thread is available for work, the reactor thread listens to the socket and accepts a request, if one is waiting. + * The backlog of this socket is configured (with a default of 1024). The + backlog determines the size of the queue for unaccepted connections. + Generally, you'll never hit the backlog cap in production. If the backlog is + full, the operating system refuses new connections. + * This socket backlog is distinct from the `backlog` of work as reported by + `Puma.stats` or the control server. The backlog that `Puma.stats` refers to + represents the number of connections in the process' `todo` set waiting for + a thread from the [`ThreadPool`](../lib/puma/thread_pool.rb). +* By default, a single, separate thread (created by the + [`Reactor`](../lib/puma/reactor.rb) class) reads and buffers requests from the + socket. + * When at least one worker thread is available for work, the reactor thread + listens to the socket and accepts a request (if one is waiting). * The reactor thread waits for the entire HTTP request to be received. - * The time spent waiting for the HTTP request body to be received is exposed to the Rack app as `env['puma.request_body_wait']` (milliseconds). - * Once fully buffered and received, the connection is pushed into the "todo" set. + * Puma exposes the time spent waiting for the HTTP request body to be + received to the Rack app as `env['puma.request_body_wait']` + (milliseconds). + * Once fully buffered and received, the connection is pushed into the "todo" + set. * Worker threads pop work off the "todo" set for processing. - * The worker thread processes the request via `call`ing the configured Rack application. The Rack application generates the HTTP response. - * The worker thread writes the response to the connection. Note that while Puma buffers requests via a separate thread, it does not use a separate thread for responses. - * Once done, the thread become available to process another connection in the "todo" set. + * The worker thread processes the request via `call`ing the configured Rack + application. The Rack application generates the HTTP response. + * The worker thread writes the response to the connection. While Puma buffers + requests via a separate thread, it does not use a separate thread for + responses. + * Once done, the thread becomes available to process another connection in the + "todo" set. ### `queue_requests` ![https://bit.ly/2zxCJ1Z](images/puma-connection-flow-no-reactor.png) -The `queue_requests` option is `true` by default, enabling the separate reactor thread used to buffer requests as described above. +The `queue_requests` option is `true` by default, enabling the separate reactor +thread used to buffer requests as described above. -If set to `false`, this buffer will not be used for connections while waiting for the request to arrive. +If set to `false`, this buffer will not be used for connections while waiting +for the request to arrive. -In this mode, when a connection is accepted, it is added to the "todo" queue immediately, and a worker will synchronously do any waiting necessary to read the HTTP request from the socket. +In this mode, when a connection is accepted, it is added to the "todo" queue +immediately, and a worker will synchronously do any waiting necessary to read +the HTTP request from the socket. diff --git a/docs/compile_options.md b/docs/compile_options.md index bb5d6350f4..178f05a3c2 100644 --- a/docs/compile_options.md +++ b/docs/compile_options.md @@ -1,10 +1,12 @@ # Compile Options -There are some `cflags` provided to change Puma's default configuration for its C extension. +There are some `cflags` provided to change Puma's default configuration for its +C extension. ## Query String, `PUMA_QUERY_STRING_MAX_LENGTH` -By default, the max length of `QUERY_STRING` is `1024 * 10`. But you may want to adjust it to allow accept larger queries in GET requests. +By default, the max length of `QUERY_STRING` is `1024 * 10`. But you may want to +adjust it to accept longer queries in GET requests. For manual install, pass the `PUMA_QUERY_STRING_MAX_LENGTH` option like this: diff --git a/docs/deployment.md b/docs/deployment.md index bcf9487310..2364aa66d1 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -1,35 +1,32 @@ # Deployment engineering for Puma -Puma is software that is expected to be run in a deployed environment eventually. -You can certainly use it as your dev server only, but most people look to use -it in their production deployments as well. +Puma expects to be run in a deployed environment eventually. You can use it as +your development server, but most people use it in their production deployments. -To that end, this is meant to serve as a foundation of wisdom how to do that -in a way that increases happiness and decreases downtime. +To that end, this document serves as a foundation of wisdom regarding deploying +Puma to production while increasing happiness and decreasing downtime. ## Specifying Puma -Most people want to do this by putting `gem "puma"` into their Gemfile, so we'll -go ahead and assume that. Go add it now... we'll wait. +Most people will specify Puma by including `gem "puma"` in a Gemfile, so we'll +assume this is how you're using Puma. -Welcome back! +## Single vs. Cluster mode -## Single vs Cluster mode +Initially, Puma was conceived as a thread-only web server, but support for +processes was added in version 2. -Puma was originally conceived as a thread-only web server, but grew the ability to -also use processes in version 2. +To run `puma` in single mode (i.e., as a development environment), set the +number of workers to 0; anything higher will run in cluster mode. -To run `puma` in single mode (e.g. for a development environment) you will need to -set the number of workers to 0, anything above will run in cluster mode. - -Here are some rules of thumb for cluster mode: +Here are some tips for cluster mode: ### MRI -* Use cluster mode and set the number of workers to 1.5x the number of cpu cores - in the machine, minimum 2. -* Set the number of threads to desired concurrent requests / number of workers. - Puma defaults to 5 and that's a decent number. +* Use cluster mode and set the number of workers to 1.5x the number of CPU cores + in the machine, starting from a minimum of 2. +* Set the number of threads to desired concurrent requests/number of workers. + Puma defaults to 5, and that's a decent number. #### Migrating from Unicorn @@ -37,7 +34,7 @@ Here are some rules of thumb for cluster mode: * Set workers to half the number of unicorn workers you're using * Set threads to 2 * Enjoy 50% memory savings -* As you grow more confident in the thread safety of your app, you can tune the +* As you grow more confident in the thread-safety of your app, you can tune the workers down and the threads up. #### Ubuntu / Systemd (Systemctl) Installation @@ -48,54 +45,58 @@ See [systemd.md](systemd.md) **How do you know if you've got enough (or too many workers)?** -A good question. Due to MRI's GIL, only one thread can be executing Ruby code at a time. -But since so many apps are waiting on IO from DBs, etc., they can utilize threads -to make better use of the process. +A good question. Due to MRI's GIL, only one thread can be executing Ruby code at +a time. But since so many apps are waiting on IO from DBs, etc., they can +utilize threads to use the process more efficiently. -The rule of thumb is you never want processes that are pegged all the time. This -means that there is more work to do than the process can get through. On the other -hand, if you have processes that sit around doing nothing, then they're just eating -up resources. +Generally, you never want processes that are pegged all the time. That can mean +there is more work to do than the process can get through. On the other hand, if +you have processes that sit around doing nothing, then they're just eating up +resources. -Watch your CPU utilization over time and aim for about 70% on average. This means -you've got capacity still but aren't starving threads. +Watch your CPU utilization over time and aim for about 70% on average. 70% +utilization means you've got capacity still but aren't starving threads. **Measuring utilization** -Using a timestamp header from an upstream proxy server (eg. nginx or haproxy), it's -possible to get an indication of how long requests have been waiting for a Puma -thread to become available. +Using a timestamp header from an upstream proxy server (e.g., `nginx` or +`haproxy`) makes it possible to indicate how long requests have been waiting for +a Puma thread to become available. * Have your upstream proxy set a header with the time it received the request: * nginx: `proxy_set_header X-Request-Start "${msec}";` - * haproxy >= 1.9: `http-request set-header X-Request-Start t=%[date()]%[date_us()]` + * haproxy >= 1.9: `http-request set-header X-Request-Start + t=%[date()]%[date_us()]` * haproxy < 1.9: `http-request set-header X-Request-Start t=%[date()]` -* In your Rack middleware, determine the amount of time elapsed since `X-Request-Start`. -* To improve accuracy, you will want to subtract time spent waiting for slow clients: - * `env['puma.request_body_wait']` contains the number of milliseconds Puma spent - waiting for the client to send the request body. - * haproxy: `%Th` (TLS handshake time) and `%Ti` (idle time before request) can - can also be added as headers. +* In your Rack middleware, determine the amount of time elapsed since + `X-Request-Start`. +* To improve accuracy, you will want to subtract time spent waiting for slow + clients: + * `env['puma.request_body_wait']` contains the number of milliseconds Puma + spent waiting for the client to send the request body. + * haproxy: `%Th` (TLS handshake time) and `%Ti` (idle time before request) + can can also be added as headers. ## Should I daemonize? -Daemonization was removed in Puma 5.0. For alternatives, continue reading. +The Puma 5.0 release removed daemonization. For older versions and alternatives, +continue reading. -I prefer to not daemonize my servers and use something like `runit` or `systemd` to -monitor them as child processes. This gives them fast response to crashes and +I prefer not to daemonize my servers and use something like `runit` or `systemd` +to monitor them as child processes. This gives them fast response to crashes and makes it easy to figure out what is going on. Additionally, unlike `unicorn`, -puma does not require daemonization to do zero-downtime restarts. +Puma does not require daemonization to do zero-downtime restarts. -I see people using daemonization because they start puma directly via capistrano -task and thus want it to live on past the `cap deploy`. To these people I say: -You need to be using a process monitor. Nothing is making sure puma stays up in -this scenario! You're just waiting for something weird to happen, puma to die, -and to get paged at 3am. Do yourself a favor, at least the process monitoring -your OS comes with, be it `sysvinit` or `systemd`. Or branch out -and use `runit` or hell, even `monit`. +I see people using daemonization because they start puma directly via Capistrano +task and thus want it to live on past the `cap deploy`. To these people, I say: +You need to be using a process monitor. Nothing is making sure Puma stays up in +this scenario! You're just waiting for something weird to happen, Puma to die, +and to get paged at 3 AM. Do yourself a favor, at least the process monitoring +your OS comes with, be it `sysvinit` or `systemd`. Or branch out and use `runit` +or hell, even `monit`. ## Restarting You probably will want to deploy some new code at some point, and you'd like -puma to start running that new code. There are a few options for restarting -puma, described separately in our [restart documentation](restart.md). +Puma to start running that new code. There are a few options for restarting +Puma, described separately in our [restart documentation](restart.md). diff --git a/docs/plugins.md b/docs/plugins.md index 985a6817f5..c7500c10d9 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -3,22 +3,22 @@ Puma 3.0 added support for plugins that can augment configuration and service operations. -2 canonical plugins to look to aid in development of further plugins: +There are two canonical plugins to aid in the development of new plugins: * [tmp\_restart](https://github.com/puma/puma/blob/master/lib/puma/plugin/tmp_restart.rb): Restarts the server if the file `tmp/restart.txt` is touched * [heroku](https://github.com/puma/puma-heroku/blob/master/lib/puma/plugin/heroku.rb): - Packages up the default configuration used by puma on Heroku (being sunset with the release of Puma 5.0) + Packages up the default configuration used by Puma on Heroku (being sunset + with the release of Puma 5.0) -Plugins are activated in a puma configuration file (such as `config/puma.rb'`) +Plugins are activated in a Puma configuration file (such as `config/puma.rb'`) by adding `plugin "name"`, such as `plugin "heroku"`. -Plugins are activated based simply on path requirements so, activating the -`heroku` plugin will simply be doing `require "puma/plugin/heroku"`. This -allows gems to provide multiple plugins (as well as unrelated gems to provide -puma plugins). +Plugins are activated based on path requirements so, activating the `heroku` +plugin is much like `require "puma/plugin/heroku"`. This allows gems to provide +multiple plugins (as well as unrelated gems to provide Puma plugins). -The `tmp_restart` plugin is bundled with puma, so it can always be used. +The `tmp_restart` plugin comes with Puma, so it is always available. To use the `heroku` plugin, add `puma-heroku` to your Gemfile or install it. @@ -26,13 +26,13 @@ To use the `heroku` plugin, add `puma-heroku` to your Gemfile or install it. ## Server-wide hooks -Plugins can use a couple of hooks at server level: `start` and `config`. +Plugins can use a couple of hooks at the server level: `start` and `config`. -`start` runs when the server has started and allows the plugin to start other -functionality to augment puma. +`start` runs when the server has started and allows the plugin to initiate other +functionality to augment Puma. -`config` runs when the server is being configured and is passed a `Puma::DSL` -object that can be used to add additional configuration. +`config` runs when the server is being configured and receives a `Puma::DSL` +object that is useful for additional configuration. -Any public methods in [`Puma::Plugin`](../lib/puma/plugin.rb) are the public API that any plugin may -use. +Public methods in [`Puma::Plugin`](../lib/puma/plugin.rb) are treated as a +public API for plugins. diff --git a/docs/rails_dev_mode.md b/docs/rails_dev_mode.md index 3261b70d8c..add3cee849 100644 --- a/docs/rails_dev_mode.md +++ b/docs/rails_dev_mode.md @@ -2,16 +2,15 @@ ## "Loopback requests" -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 is completed. 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. +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. 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. ### Solutions - #### 1. Bypass Rails' load interlock with `.permit_concurrent_loads` -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. +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. ###### Example diff --git a/docs/restart.md b/docs/restart.md index de9e84f2d0..cf95a60dc4 100644 --- a/docs/restart.md +++ b/docs/restart.md @@ -1,8 +1,8 @@ -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). +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). ## Hot restart -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. +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. 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. @@ -16,14 +16,14 @@ Any of the following will cause a Puma server to perform a hot restart: ### Supported configurations -* Works in cluster mode and in single mode +* Works in cluster mode and single mode * Supported on all platforms ### Client experience -* 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. +* 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. * 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. -* On Windows and on JRuby: Clients who connect just before a restart may experience "connection reset" errors. +* On Windows and JRuby: Clients who connect just before a restart may experience "connection reset" errors. ### Additional notes @@ -32,7 +32,7 @@ Any of the following will cause a Puma server to perform a hot restart: ## Phased restart -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. +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. ### How-to diff --git a/docs/signals.md b/docs/signals.md index 123ab99387..cbe74fc3fa 100644 --- a/docs/signals.md +++ b/docs/signals.md @@ -1,8 +1,8 @@ -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. +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. ## Sending Signals -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: +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: ```sh $ echo "foo" >> my.log @@ -10,7 +10,7 @@ $ irb > pid = Process.spawn 'tail -f my.log' ``` -From here we can see that the tail process is running by using the `ps` command: +From here, we can see that the tail process is running by using the `ps` command: ```sh $ ps aux | grep tail @@ -27,7 +27,7 @@ Process.detach(pid) # https://ruby-doc.org/core-2.1.1/Process.html#method-c-deta Process.kill("TERM", pid) ``` -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`. +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`. ## Puma Signals @@ -35,13 +35,13 @@ Puma cluster responds to these signals: - `TTIN` increment the worker count by 1 - `TTOU` decrement the worker count by 1 -- `TERM` send `TERM` to worker. Worker will attempt to finish then exit. -- `USR2` restart workers. This also reloads puma configuration file, if there is one. -- `USR1` restart workers in phases, a rolling restart. This will not reload configuration file. -- `HUP ` reopen log files defined in stdout_redirect configuration parameter. If there is no stdout_redirect option provided it will behave like `INT` -- `INT ` equivalent of sending Ctrl-C to cluster. Will attempt to finish then exit. +- `TERM` send `TERM` to worker. The worker will attempt to finish then exit. +- `USR2` restart workers. This also reloads the Puma configuration file, if there is one. +- `USR1` restart workers in phases, a rolling restart. This will not reload the configuration file. +- `HUP ` reopen log files defined in stdout_redirect configuration parameter. If there is no stdout_redirect option provided, it will behave like `INT` +- `INT ` equivalent of sending Ctrl-C to cluster. Puma will attempt to finish then exit. - `CHLD` -- `URG ` refork workers in phases from worker 0, if `fork_workers` option is enabled. +- `URG ` refork workers in phases from worker 0 if `fork_workers` option is enabled. ## Callbacks order in case of different signals diff --git a/docs/stats.md b/docs/stats.md index 313823f746..e3d9846955 100644 --- a/docs/stats.md +++ b/docs/stats.md @@ -1,4 +1,4 @@ -## accessing stats +## Accessing stats Stats can be accessed in two ways: @@ -47,18 +47,18 @@ end ## Explanation of stats -`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: +`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: -* started_at: when puma was started +* started_at: when Puma was started ### single mode and individual workers in cluster mode -When Puma is run in single mode, these stats are available at the top level. When Puma is run in cluster mode, these stats are available within the `worker_status` array in a hash labeled `last_status`, in an array of hashes, one hash for each worker. +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. * backlog: requests that are waiting for an available thread to be available. if this is above 0, you need more capacity [always true?] * running: how many threads are running -* 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. -* max_threads: the maximum number of threads puma is configured to spool up per worker +* 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. +* max_threads: the maximum number of threads Puma is configured to spool per worker * requests_count: the number of requests this worker has served since starting @@ -72,9 +72,9 @@ When Puma is run in single mode, these stats are available at the top level. Whe ### worker status -* started_at: when the worker was started +* started_at: when the worker started * pid: the process id of the worker process -* index: each worker gets a number. if puma is configured to have 3 workers, then this will be 0, 1, or 2 +* index: each worker gets a number. if Puma is configured to have 3 workers, then this will be 0, 1, or 2 * booted: if it's done booting [?] * last_checkin: Last time the worker responded to the master process' heartbeat check. * 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. diff --git a/docs/systemd.md b/docs/systemd.md index ca4b99be90..6dd6401e37 100644 --- a/docs/systemd.md +++ b/docs/systemd.md @@ -1,19 +1,18 @@ # systemd -[systemd](https://www.freedesktop.org/wiki/Software/systemd/) is a -commonly available init system (PID 1) on many Linux distributions. It -offers process monitoring (including automatic restarts) and other -useful features for running Puma in production. +[systemd](https://www.freedesktop.org/wiki/Software/systemd/) is a commonly +available init system (PID 1) on many Linux distributions. It offers process +monitoring (including automatic restarts) and other useful features for running +Puma in production. ## Service Configuration -Below is a sample puma.service configuration file for systemd, which -can be copied or symlinked to `/etc/systemd/system/puma.service`, or if -desired, using an application or instance specific name. +Below is a sample puma.service configuration file for systemd, which can be +copied or symlinked to `/etc/systemd/system/puma.service`, or if desired, using +an application or instance-specific name. -Note that this uses the systemd preferred "simple" type where the -start command remains running in the foreground (does not fork and -exit). +Note that this uses the systemd preferred "simple" type where the start command +remains running in the foreground (does not fork and exit). ~~~~ ini [Unit] @@ -37,8 +36,8 @@ WatchdogSec=10 # Preferably configure a non-privileged user # User= -# The path to the your application code root directory. -# Also replace the "" place holders below with this path. +# The path to your application code root directory. +# Also replace the "" placeholders below with this path. # Example /home/username/myapp WorkingDirectory= @@ -64,33 +63,31 @@ Restart=always WantedBy=multi-user.target ~~~~ -See [systemd.exec](https://www.freedesktop.org/software/systemd/man/systemd.exec.html) +See +[systemd.exec](https://www.freedesktop.org/software/systemd/man/systemd.exec.html) for additional details. ## Socket Activation -systemd and puma also support socket activation, where systemd opens -the listening socket(s) in advance and provides them to the puma -master process on startup. Among other advantages, this keeps -listening sockets open across puma restarts and achieves graceful -restarts, including when upgraded puma, and is compatible with both -clustered mode and application preload. - -**Note:** Any wrapper scripts which `exec`, or other indirections in -`ExecStart`, may result in activated socket file descriptors being closed -before they reach the puma master process. For example, if using `bundle exec`, -pass the `--keep-file-descriptors` flag. `bundle exec` can be avoided by using a -`puma` executable generated by `bundle binstubs puma`. This is tracked in -[#1499]. - -**Note:** Socket activation doesn't currently work on JRuby. This is -tracked in [#1367]. - -To use socket activation, configure one or more `ListenStream` sockets -in a companion `*.socket` unit file. Also uncomment the associated -`Requires` directive for the socket unit in the service file (see -above.) Here is a sample puma.socket, matching the ports used in the -above puma.service: +systemd and Puma also support socket activation, where systemd opens the +listening socket(s) in advance and provides them to the Puma master process on +startup. Among other advantages, this keeps listening sockets open across puma +restarts and achieves graceful restarts, including when upgraded Puma, and is +compatible with both clustered mode and application preload. + +**Note:** Any wrapper scripts which `exec`, or other indirections in `ExecStart` +may result in activated socket file descriptors being closed before reaching the +puma master process. For example, if using `bundle exec`, pass the +`--keep-file-descriptors` flag. `bundle exec` can be avoided by using a `puma` +executable generated by `bundle binstubs puma`. This is tracked in [#1499]. + +**Note:** Socket activation doesn't currently work on JRuby. This is tracked in +[#1367]. + +Configure one or more `ListenStream` sockets in a companion `*.socket` unit file +to use socket activation. Also, uncomment the associated `Requires` directive +for the socket unit in the service file (see above.) Here is a sample +puma.socket, matching the ports used in the above puma.service: ~~~~ ini [Unit] @@ -113,31 +110,32 @@ Backlog=1024 WantedBy=sockets.target ~~~~ -See [systemd.socket](https://www.freedesktop.org/software/systemd/man/systemd.socket.html) +See +[systemd.socket](https://www.freedesktop.org/software/systemd/man/systemd.socket.html) for additional configuration details. -Note that the above configurations will work with Puma in either -single process or cluster mode. +Note that the above configurations will work with Puma in either single process +or cluster mode. ### Sockets and symlinks -When using releases folders, you should set the socket path using the -shared folder path (ex. `/srv/projet/shared/tmp/puma.sock`), not the -release folder path (`/srv/projet/releases/1234/tmp/puma.sock`). +When using releases folders, you should set the socket path using the shared +folder path (ex. `/srv/projet/shared/tmp/puma.sock`), not the release folder +path (`/srv/projet/releases/1234/tmp/puma.sock`). Puma will detect the release path socket as different than the one provided by -systemd and attempt to bind it again, resulting in the exception - `There is already a server bound to:`. +systemd and attempt to bind it again, resulting in the exception `There is +already a server bound to:`. ### Binding -By default you need to configure puma to have binds matching with all +By default, you need to configure Puma to have binds matching with all ListenStream statements. Any mismatched systemd ListenStreams will be closed by -puma. +Puma. To automatically bind to all activated sockets, the option `--bind-to-activated-sockets` can be used. This matches the config DSL -`bind_to_activated_sockets` statement. This will cause puma to create a bind +`bind_to_activated_sockets` statement. This will cause Puma to create a bind automatically for any activated socket. When systemd socket activation is not enabled, this option does nothing. @@ -146,8 +144,8 @@ binds that's not socket activated. ## Usage -Without socket activation, use `systemctl` as root (e.g. via `sudo`) as -with other system services: +Without socket activation, use `systemctl` as root (i.e., via `sudo`) as with +other system services: ~~~~ sh # After installing or making changes to puma.service @@ -156,35 +154,35 @@ systemctl daemon-reload # Enable so it starts on boot systemctl enable puma.service -# Initial start up. +# Initial startup. systemctl start puma.service # Check status systemctl status puma.service -# A normal restart. Warning: listeners sockets will be closed +# A normal restart. Warning: listener's sockets will be closed # while a new puma process initializes. systemctl restart puma.service ~~~~ -With socket activation, several but not all of these commands should -be run for both socket and service: +With socket activation, several but not all of these commands should be run for +both socket and service: ~~~~ sh # After installing or making changes to either puma.socket or # puma.service. systemctl daemon-reload -# Enable both socket and service so they start on boot. Alternatively -# you could leave puma.service disabled and systemd will start it on -# first use (with startup lag on first request) +# Enable both socket and service, so they start on boot. Alternatively +# you could leave puma.service disabled, and systemd will start it on +# the first use (with startup lag on the first request) systemctl enable puma.socket puma.service -# Initial start up. The Requires directive (see above) ensures the +# Initial startup. The Requires directive (see above) ensures the # socket is started before the service. systemctl start puma.socket puma.service -# Check status of both socket and service. +# Check the status of both socket and service. systemctl status puma.socket puma.service # A "hot" restart, with systemd keeping puma.socket listening and @@ -197,8 +195,8 @@ systemctl restart puma.service systemctl restart puma.socket puma.service ~~~~ -Here is sample output from `systemctl status` with both service and -socket running: +Here is sample output from `systemctl status` with both service and socket +running: ~~~~ ● puma.socket - Puma HTTP Server Accept Sockets @@ -231,14 +229,12 @@ Apr 07 08:40:19 hx puma[28320]: Use Ctrl-C to stop ### capistrano3-puma -By default, -[capistrano3-puma](https://github.com/seuros/capistrano-puma) uses -`pumactl` for deployment restarts, outside of systemd. To learn the -exact commands that this tool would use for `ExecStart` and -`ExecStop`, use the following `cap` commands in dry-run mode, and -update from the above forking service configuration accordingly. Note -also that the configured `User` should likely be the same as the -capistrano3-puma `:puma_user` option. +By default, [capistrano3-puma](https://github.com/seuros/capistrano-puma) uses +`pumactl` for deployment restarts outside of systemd. To learn the exact +commands that this tool would use for `ExecStart` and `ExecStop`, use the +following `cap` commands in dry-run mode, and update from the above forking +service configuration accordingly. Note also that the configured `User` should +likely be the same as the capistrano3-puma `:puma_user` option. ~~~~ sh stage=production # or different stage, as needed