puma 3.11.1 → 6.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. checksums.yaml +5 -5
  2. data/History.md +2092 -422
  3. data/LICENSE +23 -20
  4. data/README.md +301 -69
  5. data/bin/puma-wild +3 -9
  6. data/docs/architecture.md +59 -21
  7. data/docs/compile_options.md +55 -0
  8. data/docs/deployment.md +69 -58
  9. data/docs/fork_worker.md +41 -0
  10. data/docs/java_options.md +54 -0
  11. data/docs/jungle/README.md +9 -0
  12. data/docs/jungle/rc.d/README.md +74 -0
  13. data/docs/jungle/rc.d/puma +61 -0
  14. data/docs/jungle/rc.d/puma.conf +10 -0
  15. data/docs/kubernetes.md +78 -0
  16. data/docs/nginx.md +2 -2
  17. data/docs/plugins.md +26 -12
  18. data/docs/rails_dev_mode.md +28 -0
  19. data/docs/restart.md +48 -22
  20. data/docs/signals.md +13 -11
  21. data/docs/stats.md +147 -0
  22. data/docs/systemd.md +108 -117
  23. data/docs/testing_benchmarks_local_files.md +150 -0
  24. data/docs/testing_test_rackup_ci_files.md +36 -0
  25. data/ext/puma_http11/PumaHttp11Service.java +2 -2
  26. data/ext/puma_http11/ext_help.h +1 -1
  27. data/ext/puma_http11/extconf.rb +68 -3
  28. data/ext/puma_http11/http11_parser.c +106 -118
  29. data/ext/puma_http11/http11_parser.h +2 -2
  30. data/ext/puma_http11/http11_parser.java.rl +22 -38
  31. data/ext/puma_http11/http11_parser.rl +6 -4
  32. data/ext/puma_http11/http11_parser_common.rl +6 -6
  33. data/ext/puma_http11/mini_ssl.c +474 -94
  34. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  35. data/ext/puma_http11/org/jruby/puma/Http11.java +136 -121
  36. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +84 -99
  37. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +251 -88
  38. data/ext/puma_http11/puma_http11.c +53 -58
  39. data/lib/puma/app/status.rb +71 -49
  40. data/lib/puma/binder.rb +257 -151
  41. data/lib/puma/cli.rb +61 -38
  42. data/lib/puma/client.rb +464 -224
  43. data/lib/puma/cluster/worker.rb +183 -0
  44. data/lib/puma/cluster/worker_handle.rb +96 -0
  45. data/lib/puma/cluster.rb +343 -239
  46. data/lib/puma/commonlogger.rb +23 -14
  47. data/lib/puma/configuration.rb +144 -96
  48. data/lib/puma/const.rb +194 -115
  49. data/lib/puma/control_cli.rb +135 -81
  50. data/lib/puma/detect.rb +34 -2
  51. data/lib/puma/dsl.rb +1092 -153
  52. data/lib/puma/error_logger.rb +113 -0
  53. data/lib/puma/events.rb +17 -111
  54. data/lib/puma/io_buffer.rb +44 -5
  55. data/lib/puma/jruby_restart.rb +2 -73
  56. data/lib/puma/json_serialization.rb +96 -0
  57. data/lib/puma/launcher/bundle_pruner.rb +104 -0
  58. data/lib/puma/launcher.rb +205 -138
  59. data/lib/puma/log_writer.rb +147 -0
  60. data/lib/puma/minissl/context_builder.rb +96 -0
  61. data/lib/puma/minissl.rb +279 -70
  62. data/lib/puma/null_io.rb +61 -2
  63. data/lib/puma/plugin/systemd.rb +90 -0
  64. data/lib/puma/plugin/tmp_restart.rb +3 -1
  65. data/lib/puma/plugin.rb +9 -13
  66. data/lib/puma/rack/builder.rb +10 -11
  67. data/lib/puma/rack/urlmap.rb +3 -1
  68. data/lib/puma/rack_default.rb +21 -4
  69. data/lib/puma/reactor.rb +97 -185
  70. data/lib/puma/request.rb +688 -0
  71. data/lib/puma/runner.rb +114 -69
  72. data/lib/puma/sd_notify.rb +146 -0
  73. data/lib/puma/server.rb +409 -704
  74. data/lib/puma/single.rb +29 -72
  75. data/lib/puma/state_file.rb +48 -9
  76. data/lib/puma/thread_pool.rb +234 -93
  77. data/lib/puma/util.rb +23 -10
  78. data/lib/puma.rb +68 -5
  79. data/lib/rack/handler/puma.rb +119 -86
  80. data/tools/Dockerfile +16 -0
  81. data/tools/trickletest.rb +0 -1
  82. metadata +55 -33
  83. data/ext/puma_http11/io_buffer.c +0 -155
  84. data/lib/puma/accept_nonblock.rb +0 -23
  85. data/lib/puma/compat.rb +0 -14
  86. data/lib/puma/convenient.rb +0 -23
  87. data/lib/puma/daemon_ext.rb +0 -31
  88. data/lib/puma/delegation.rb +0 -11
  89. data/lib/puma/java_io_buffer.rb +0 -45
  90. data/lib/puma/rack/backports/uri/common_193.rb +0 -33
  91. data/lib/puma/tcp_logger.rb +0 -39
  92. data/tools/jungle/README.md +0 -13
  93. data/tools/jungle/init.d/README.md +0 -59
  94. data/tools/jungle/init.d/puma +0 -421
  95. data/tools/jungle/init.d/run-puma +0 -18
  96. data/tools/jungle/upstart/README.md +0 -61
  97. data/tools/jungle/upstart/puma-manager.conf +0 -31
  98. data/tools/jungle/upstart/puma.conf +0 -69
data/docs/architecture.md CHANGED
@@ -2,35 +2,73 @@
2
2
 
3
3
  ## Overview
4
4
 
5
- ![http://bit.ly/2iJuFky](images/puma-general-arch.png)
5
+ ![https://bit.ly/2iJuFky](images/puma-general-arch.png)
6
6
 
7
- Puma is a threaded web server, processing requests across a TCP or UNIX socket.
7
+ Puma is a threaded Ruby HTTP application server processing requests across a TCP
8
+ and/or UNIX socket.
8
9
 
9
- Workers accept connections from the socket and a thread in the worker's thread pool processes the client's request.
10
10
 
11
- Clustered mode is shown/discussed here. Single mode is analogous to having a single worker process.
11
+ Puma processes (there can be one or many) accept connections from the socket via
12
+ a thread (in the [`Reactor`](../lib/puma/reactor.rb) class). The connection,
13
+ once fully buffered and read, moves into the `todo` list, where an available
14
+ thread will pick it up (in the [`ThreadPool`](../lib/puma/thread_pool.rb)
15
+ class).
12
16
 
13
- ## Connection pipeline
17
+ Puma works in two main modes: cluster and single. In single mode, only one Puma
18
+ process boots. In cluster mode, a `master` process is booted, which prepares
19
+ (and may boot) the application and then uses the `fork()` system call to create
20
+ one or more `child` processes. These `child` processes all listen to the same
21
+ socket. The `master` process does not listen to the socket or process requests -
22
+ its purpose is primarily to manage and listen for UNIX signals and possibly kill
23
+ or boot `child` processes.
14
24
 
15
- ![http://bit.ly/2zwzhEK](images/puma-connection-flow.png)
25
+ We sometimes call `child` processes (or Puma processes in `single` mode)
26
+ _workers_, and we sometimes call the threads created by Puma's
27
+ [`ThreadPool`](../lib/puma/thread_pool.rb) _worker threads_.
28
+
29
+ ## How Requests Work
30
+
31
+ ![https://bit.ly/2zwzhEK](images/puma-connection-flow.png)
16
32
 
17
33
  * Upon startup, Puma listens on a TCP or UNIX socket.
18
- * The backlog of this socket is configured (with a default of 1024), determining how many established but unaccepted connections can exist concurrently.
19
- * This socket backlog is distinct from the "backlog" of work as reported by the control server stats. The latter is the number of connections in that worker's "todo" set waiting for a worker thread.
20
- * By default, a single, separate thread is used to receive HTTP requests across the socket.
21
- * When at least one worker thread is available for work, a connection is accepted and placed in this request buffer
22
- * This thread waits for entire HTTP requests to be received over the connection
23
- * Once received, the connection is pushed into the "todo" set
24
- * Worker threads pop work off the "todo" set for processing
25
- * The thread processes the request via the rack application (which generates the HTTP response)
26
- * The thread writes the response to the connection
27
- * Finally, the thread become available to process another connection in the "todo" set
34
+ * The backlog of this socket is configured with a default of 1024, but the
35
+ actual backlog value is capped by the `net.core.somaxconn` sysctl value.
36
+ The backlog determines the size of the queue for unaccepted connections. If
37
+ the backlog is full, the operating system is not accepting new connections.
38
+ * This socket backlog is distinct from the `backlog` of work as reported by
39
+ `Puma.stats` or the control server. The backlog that `Puma.stats` refers to
40
+ represents the number of connections in the process' `todo` set waiting for
41
+ a thread from the [`ThreadPool`](../lib/puma/thread_pool.rb).
42
+ * By default, a single, separate thread (created by the
43
+ [`Reactor`](../lib/puma/reactor.rb) class) reads and buffers requests from the
44
+ socket.
45
+ * When at least one worker thread is available for work, the reactor thread
46
+ listens to the socket and accepts a request (if one is waiting).
47
+ * The reactor thread waits for the entire HTTP request to be received.
48
+ * Puma exposes the time spent waiting for the HTTP request body to be
49
+ received to the Rack app as `env['puma.request_body_wait']`
50
+ (milliseconds).
51
+ * Once fully buffered and received, the connection is pushed into the "todo"
52
+ set.
53
+ * Worker threads pop work off the "todo" set for processing.
54
+ * The worker thread processes the request via `call`ing the configured Rack
55
+ application. The Rack application generates the HTTP response.
56
+ * The worker thread writes the response to the connection. While Puma buffers
57
+ requests via a separate thread, it does not use a separate thread for
58
+ responses.
59
+ * Once done, the thread becomes available to process another connection in the
60
+ "todo" set.
61
+
62
+ ### `queue_requests`
28
63
 
29
- ### Disabling `queue_requests`
64
+ ![https://bit.ly/2zxCJ1Z](images/puma-connection-flow-no-reactor.png)
30
65
 
31
- ![http://bit.ly/2zxCJ1Z](images/puma-connection-flow-no-reactor.png)
66
+ The `queue_requests` option is `true` by default, enabling the separate reactor
67
+ thread used to buffer requests as described above.
32
68
 
33
- The `queue_requests` option is `true` by default, enabling the separate thread used to buffer requests as described above.
69
+ If set to `false`, this buffer will not be used for connections while waiting
70
+ for the request to arrive.
34
71
 
35
- If set to `false`, this buffer will not be used for connections while waiting for the request to arrive.
36
- In this mode, when a connection is accepted, it is added to the "todo" queue immediately, and a worker will syncronously do any waiting necessarry to read the HTTP request from the socket.
72
+ In this mode, when a connection is accepted, it is added to the "todo" queue
73
+ immediately, and a worker will synchronously do any waiting necessary to read
74
+ the HTTP request from the socket.
@@ -0,0 +1,55 @@
1
+ # Compile Options
2
+
3
+ There are some `cflags` provided to change Puma's default configuration for its
4
+ C extension.
5
+
6
+ ## Query String, `PUMA_QUERY_STRING_MAX_LENGTH`
7
+
8
+ By default, the max length of `QUERY_STRING` is `1024 * 10`. But you may want to
9
+ adjust it to accept longer queries in GET requests.
10
+
11
+ For manual install, pass the `PUMA_QUERY_STRING_MAX_LENGTH` option like this:
12
+
13
+ ```
14
+ gem install puma -- --with-cflags="-D PUMA_QUERY_STRING_MAX_LENGTH=64000"
15
+ ```
16
+
17
+ For Bundler, use its configuration system:
18
+
19
+ ```
20
+ bundle config build.puma "--with-cflags='-D PUMA_QUERY_STRING_MAX_LENGTH=64000'"
21
+ ```
22
+
23
+ ## Request Path, `PUMA_REQUEST_PATH_MAX_LENGTH`
24
+
25
+ By default, the max length of `REQUEST_PATH` is `8192`. But you may want to
26
+ adjust it to accept longer paths in requests.
27
+
28
+ For manual install, pass the `PUMA_REQUEST_PATH_MAX_LENGTH` option like this:
29
+
30
+ ```
31
+ gem install puma -- --with-cflags="-D PUMA_REQUEST_PATH_MAX_LENGTH=64000"
32
+ ```
33
+
34
+ For Bundler, use its configuration system:
35
+
36
+ ```
37
+ bundle config build.puma "--with-cflags='-D PUMA_REQUEST_PATH_MAX_LENGTH=64000'"
38
+ ```
39
+
40
+ ## Request URI, `PUMA_REQUEST_URI_MAX_LENGTH`
41
+
42
+ By default, the max length of `REQUEST_URI` is `1024 * 12`. But you may want to
43
+ adjust it to accept longer URIs in requests.
44
+
45
+ For manual install, pass the `PUMA_REQUEST_URI_MAX_LENGTH` option like this:
46
+
47
+ ```
48
+ gem install puma -- --with-cflags="-D PUMA_REQUEST_URI_MAX_LENGTH=64000"
49
+ ```
50
+
51
+ For Bundler, use its configuration system:
52
+
53
+ ```
54
+ bundle config build.puma "--with-cflags='-D PUMA_REQUEST_URI_MAX_LENGTH=64000'"
55
+ ```
data/docs/deployment.md CHANGED
@@ -1,33 +1,32 @@
1
- # Deployment engineering for puma
1
+ # Deployment engineering for Puma
2
2
 
3
- Puma is software that is expected to be run in a deployed environment eventually.
4
- You can certainly use it as your dev server only, but most people look to use
5
- it in their production deployments as well.
3
+ Puma expects to be run in a deployed environment eventually. You can use it as
4
+ your development server, but most people use it in their production deployments.
6
5
 
7
- To that end, this is meant to serve as a foundation of wisdom how to do that
8
- in a way that increases happiness and decreases downtime.
6
+ To that end, this document serves as a foundation of wisdom regarding deploying
7
+ Puma to production while increasing happiness and decreasing downtime.
9
8
 
10
- ## Specifying puma
9
+ ## Specifying Puma
11
10
 
12
- Most people want to do this by putting `gem "puma"` into their Gemfile, so we'll
13
- go ahead and assume that. Go add it now... we'll wait.
11
+ Most people will specify Puma by including `gem "puma"` in a Gemfile, so we'll
12
+ assume this is how you're using Puma.
14
13
 
14
+ ## Single vs. Cluster mode
15
15
 
16
- Welcome back!
16
+ Initially, Puma was conceived as a thread-only web server, but support for
17
+ processes was added in version 2.
17
18
 
18
- ## Single vs Cluster mode
19
+ To run `puma` in single mode (i.e., as a development environment), set the
20
+ number of workers to 0; anything higher will run in cluster mode.
19
21
 
20
- Puma was originally conceived as a thread-only webserver, but grew the ability to
21
- also use processes in version 2.
22
-
23
- Here are some rules of thumb:
22
+ Here are some tips for cluster mode:
24
23
 
25
24
  ### MRI
26
25
 
27
- * Use cluster mode and set the number of workers to 1.5x the number of cpu cores
28
- in the machine, minimum 2.
29
- * Set the number of threads to desired concurrent requests / number of workers.
30
- Puma defaults to 16 and that's a decent number.
26
+ * Use cluster mode and set the number of workers to 1.5x the number of CPU cores
27
+ in the machine, starting from a minimum of 2.
28
+ * Set the number of threads to desired concurrent requests/number of workers.
29
+ Puma defaults to 5, and that's a decent number.
31
30
 
32
31
  #### Migrating from Unicorn
33
32
 
@@ -35,57 +34,69 @@ Here are some rules of thumb:
35
34
  * Set workers to half the number of unicorn workers you're using
36
35
  * Set threads to 2
37
36
  * Enjoy 50% memory savings
38
- * As you grow more confident in the thread safety of your app, you can tune the
37
+ * As you grow more confident in the thread-safety of your app, you can tune the
39
38
  workers down and the threads up.
40
39
 
40
+ #### Ubuntu / Systemd (Systemctl) Installation
41
+
42
+ See [systemd.md](systemd.md)
43
+
41
44
  #### Worker utilization
42
45
 
43
- **How do you know if you're got enough (or too many workers)?**
46
+ **How do you know if you've got enough (or too many workers)?**
47
+
48
+ A good question. Due to MRI's GIL, only one thread can be executing Ruby code at
49
+ a time. But since so many apps are waiting on IO from DBs, etc., they can
50
+ utilize threads to use the process more efficiently.
51
+
52
+ Generally, you never want processes that are pegged all the time. That can mean
53
+ there is more work to do than the process can get through. On the other hand, if
54
+ you have processes that sit around doing nothing, then they're just eating up
55
+ resources.
56
+
57
+ Watch your CPU utilization over time and aim for about 70% on average. 70%
58
+ utilization means you've got capacity still but aren't starving threads.
59
+
60
+ **Measuring utilization**
44
61
 
45
- A good question. Due to MRI's GIL, only one thread can be executing Ruby code at a time.
46
- But since so many apps are waiting on IO from DBs, etc., they can utilize threads
47
- to make better use of the process.
62
+ Using a timestamp header from an upstream proxy server (e.g., `nginx` or
63
+ `haproxy`) makes it possible to indicate how long requests have been waiting for
64
+ a Puma thread to become available.
48
65
 
49
- The rule of thumb is you never want processes that are pegged all the time. This
50
- means that there is more work to do that the process can get through. On the other
51
- hand, if you have processes that sit around doing nothing, then they're just eating
52
- up resources.
66
+ * Have your upstream proxy set a header with the time it received the request:
67
+ * nginx: `proxy_set_header X-Request-Start "${msec}";`
68
+ * haproxy >= 1.9: `http-request set-header X-Request-Start
69
+ t=%[date()]%[date_us()]`
70
+ * haproxy < 1.9: `http-request set-header X-Request-Start t=%[date()]`
71
+ * In your Rack middleware, determine the amount of time elapsed since
72
+ `X-Request-Start`.
73
+ * To improve accuracy, you will want to subtract time spent waiting for slow
74
+ clients:
75
+ * `env['puma.request_body_wait']` contains the number of milliseconds Puma
76
+ spent waiting for the client to send the request body.
77
+ * haproxy: `%Th` (TLS handshake time) and `%Ti` (idle time before request)
78
+ can can also be added as headers.
53
79
 
54
- Watching your CPU utilization over time and aim for about 70% on average. This means
55
- you've got capacity still but aren't starving threads.
80
+ ## Should I daemonize?
56
81
 
57
- ## Daemonizing
82
+ The Puma 5.0 release removed daemonization. For older versions and alternatives,
83
+ continue reading.
58
84
 
59
- I prefer to not daemonize my servers and use something like `runit` or `upstart` to
60
- monitor them as child processes. This gives them fast response to crashes and
85
+ I prefer not to daemonize my servers and use something like `runit` or `systemd`
86
+ to monitor them as child processes. This gives them fast response to crashes and
61
87
  makes it easy to figure out what is going on. Additionally, unlike `unicorn`,
62
- puma does not require daemonization to do zero-downtime restarts.
88
+ Puma does not require daemonization to do zero-downtime restarts.
63
89
 
64
- I see people using daemonization because they start puma directly via capistrano
65
- task and thus want it to live on past the `cap deploy`. To this people I said:
66
- You need to be using a process monitor. Nothing is making sure puma stays up in
67
- this scenario! You're just waiting for something weird to happen, puma to die,
68
- and to get paged at 3am. Do yourself a favor, at least the process monitoring
69
- your OS comes with, be it `sysvinit`, `upstart`, or `systemd`. Or branch out
70
- and use `runit` or hell, even `monit`.
90
+ I see people using daemonization because they start puma directly via Capistrano
91
+ task and thus want it to live on past the `cap deploy`. To these people, I say:
92
+ You need to be using a process monitor. Nothing is making sure Puma stays up in
93
+ this scenario! You're just waiting for something weird to happen, Puma to die,
94
+ and to get paged at 3 AM. Do yourself a favor, at least the process monitoring
95
+ your OS comes with, be it `sysvinit` or `systemd`. Or branch out and use `runit`
96
+ or hell, even `monit`.
71
97
 
72
98
  ## Restarting
73
99
 
74
100
  You probably will want to deploy some new code at some point, and you'd like
75
- puma to start running that new code. Minimizing the amount of time the server
76
- is unavailable would be nice as well. Here's how to do it:
77
-
78
- 1. Don't use `preload!`. This dirties the master process and means it will have
79
- to shutdown all the workers and re-exec itself to get your new code. It is not compatible with phased-restart and `prune_bundler` as well.
80
-
81
- 1. Use `prune_bundler`. This makes it so that the cluster master will detach itself
82
- from a Bundler context on start. This allows the cluster workers to load your app
83
- and start a brand new Bundler context within the worker only. This means your
84
- master remains pristine and can live on between new releases of your code.
85
-
86
- 1. Use phased-restart (`SIGUSR1` or `pumactl phased-restart`). This tells the master
87
- to kill off one worker at a time and restart them in your new code. This minimizes
88
- downtime and staggers the restart nicely. **WARNING** This means that both your
89
- old code and your new code will be running concurrently. Most deployment solutions
90
- already cause that, but it's worth warning you about it again. Be careful with your
91
- migrations, etc!
101
+ Puma to start running that new code. There are a few options for restarting
102
+ Puma, described separately in our [restart documentation](restart.md).
@@ -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 `on_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 `on_worker_boot` will trigger on the worker 0 child as normal.
29
+ - When forking the worker 0 child to grandchild workers, `on_refork` and `after_refork` will trigger on the worker 0 child, and `on_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 `on_refork` hook.
33
+ - Consider to copy logic from your `on_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).
@@ -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,9 @@
1
+ # Puma as a service
2
+
3
+ ## Systemd
4
+
5
+ See [/docs/systemd](https://github.com/puma/puma/blob/master/docs/systemd.md).
6
+
7
+ ## rc.d
8
+
9
+ See `/docs/jungle/rc.d` for FreeBSD's rc.d scripts
@@ -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"
@@ -0,0 +1,10 @@
1
+ {
2
+ "servers" : [
3
+ {
4
+ "dir": "/path/to/rails/project",
5
+ "user": "deploy-user",
6
+ "ruby_version": "ruby.version",
7
+ "ruby_env": "rbenv"
8
+ }
9
+ ]
10
+ }