puma 8.0.3 → 8.1.1

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 (64) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +125 -0
  3. data/README.md +28 -378
  4. data/docs/binding.md +135 -0
  5. data/docs/cluster_mode.md +241 -0
  6. data/docs/configuration.md +78 -0
  7. data/docs/{stats.md → control_server.md} +28 -8
  8. data/docs/{deployment.md → deployment/README.md} +23 -7
  9. data/docs/{kubernetes.md → deployment/kubernetes.md} +4 -4
  10. data/docs/{systemd.md → deployment/systemd.md} +4 -2
  11. data/docs/plugins.md +62 -3
  12. data/docs/puma_support.md +92 -0
  13. data/docs/rails_dev_mode.md +2 -2
  14. data/docs/restart.md +8 -3
  15. data/docs/signals.md +4 -2
  16. data/docs/{5.0-Upgrade.md → upgrading/5.0-Upgrade.md} +3 -3
  17. data/docs/{6.0-Upgrade.md → upgrading/6.0-Upgrade.md} +3 -3
  18. data/docs/{7.0-Upgrade.md → upgrading/7.0-Upgrade.md} +2 -2
  19. data/docs/{8.0-Upgrade.md → upgrading/8.0-Upgrade.md} +4 -4
  20. data/ext/puma_http11/extconf.rb +2 -0
  21. data/ext/puma_http11/http11_parser.c +6 -2
  22. data/ext/puma_http11/mini_ssl.c +8 -4
  23. data/ext/puma_http11/puma_http11.c +24 -0
  24. data/lib/puma/binder.rb +29 -0
  25. data/lib/puma/cli.rb +6 -0
  26. data/lib/puma/client.rb +75 -29
  27. data/lib/puma/client_env.rb +32 -18
  28. data/lib/puma/cluster/worker.rb +109 -33
  29. data/lib/puma/cluster/worker_handle.rb +16 -0
  30. data/lib/puma/cluster.rb +173 -45
  31. data/lib/puma/commonlogger.rb +3 -3
  32. data/lib/puma/configuration.rb +22 -3
  33. data/lib/puma/const.rb +11 -9
  34. data/lib/puma/dsl.rb +265 -91
  35. data/lib/puma/launcher.rb +6 -4
  36. data/lib/puma/minissl.rb +2 -5
  37. data/lib/puma/plugin/systemd.rb +1 -1
  38. data/lib/puma/response.rb +13 -5
  39. data/lib/puma/runner.rb +5 -1
  40. data/lib/puma/sd_notify.rb +4 -1
  41. data/lib/puma/server.rb +45 -20
  42. data/lib/puma/thread_pool.rb +12 -1
  43. data/lib/puma.rb +1 -1
  44. metadata +18 -30
  45. data/docs/fork_worker.md +0 -41
  46. data/docs/grpc.md +0 -62
  47. data/docs/jungle/README.md +0 -9
  48. data/docs/testing_benchmarks_local_files.md +0 -150
  49. data/docs/testing_test_rackup_ci_files.md +0 -36
  50. data/ext/puma_http11/PumaHttp11Service.java +0 -17
  51. data/ext/puma_http11/http11_parser.java.rl +0 -131
  52. data/ext/puma_http11/http11_parser.rl +0 -149
  53. data/ext/puma_http11/http11_parser_common.rl +0 -54
  54. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +0 -15
  55. data/ext/puma_http11/org/jruby/puma/EnvKey.java +0 -241
  56. data/ext/puma_http11/org/jruby/puma/Http11.java +0 -321
  57. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +0 -441
  58. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +0 -519
  59. data/tools/Dockerfile +0 -26
  60. data/tools/trickletest.rb +0 -44
  61. /data/docs/{nginx.md → deployment/nginx.md} +0 -0
  62. /data/docs/{jungle → deployment}/rc.d/README.md +0 -0
  63. /data/docs/{jungle → deployment}/rc.d/puma +0 -0
  64. /data/docs/{jungle → deployment}/rc.d/puma.conf +0 -0
data/lib/puma/launcher.rb CHANGED
@@ -78,6 +78,7 @@ module Puma
78
78
  @binder = Binder.new(@log_writer, @options)
79
79
  @binder.create_inherited_fds(env).each { |k| env.delete k }
80
80
  @binder.create_activated_fds(env).each { |k| env.delete k }
81
+ @control_activated_socket = @binder.take_activated_socket(@options[:control_url]) if @options[:control_url]
81
82
 
82
83
  @environment = @config.environment
83
84
 
@@ -112,7 +113,7 @@ module Puma
112
113
  @status = :run
113
114
  end
114
115
 
115
- attr_reader :binder, :log_writer, :events, :config, :options, :restart_dir
116
+ attr_reader :binder, :log_writer, :events, :config, :options, :restart_dir, :control_activated_socket
116
117
 
117
118
  # Return stats about the server
118
119
  def stats
@@ -187,6 +188,9 @@ module Puma
187
188
  if clustered? && @runner.respond_to?(:fork_worker!) && @options[:fork_worker]
188
189
  @runner.fork_worker!
189
190
  true
191
+ elsif clustered? && @runner.respond_to?(:mold_worker!) && @options[:mold_worker]
192
+ @runner.mold_worker!
193
+ true
190
194
  else
191
195
  log "* refork called but not available."
192
196
  false
@@ -197,8 +201,6 @@ module Puma
197
201
  def run
198
202
  previous_env = get_env
199
203
 
200
- @config.clamp
201
-
202
204
  @config.plugins.fire_starts self
203
205
 
204
206
  setup_signals
@@ -282,8 +284,8 @@ module Puma
282
284
  end
283
285
 
284
286
  def do_graceful_stop
285
- @events.fire_after_stopped!
286
287
  @runner.stop_blocked
288
+ @events.fire_after_stopped!
287
289
  end
288
290
 
289
291
  def do_restart(previous_env)
data/lib/puma/minissl.rb CHANGED
@@ -97,7 +97,7 @@ module Puma
97
97
  # It would make more sense to let @socket.read_nonblock raise
98
98
  # EAGAIN if necessary but it seems like it'll misbehave on Windows.
99
99
  # I don't have a Windows machine to debug this so I can't explain
100
- # exactly whats happening in that OS. Please let me know if you
100
+ # exactly what's happening in that OS. Please let me know if you
101
101
  # find out!
102
102
  #
103
103
  # In the meantime, we can emulate the correct behavior by
@@ -205,10 +205,7 @@ module Puma
205
205
  private :queue_ciphertext
206
206
 
207
207
  def flush_write_buffer
208
- return if @write_buffer.empty?
209
-
210
- @socket.write @write_buffer
211
- @write_buffer = +''
208
+ @socket.wait_writable until flush_write_buffer_nonblock
212
209
  end
213
210
  private :flush_write_buffer
214
211
 
@@ -5,7 +5,7 @@ require_relative '../plugin'
5
5
  # Puma's systemd integration allows Puma to inform systemd:
6
6
  # 1. when it has successfully started
7
7
  # 2. when it is starting shutdown
8
- # 3. periodically for a liveness check with a watchdog thread
8
+ # 3. at startup and then periodically for a liveness check with a watchdog thread
9
9
  # 4. periodically set the status
10
10
  Puma::Plugin.create do
11
11
  def start(launcher)
data/lib/puma/response.rb CHANGED
@@ -75,6 +75,7 @@ module Puma
75
75
  end
76
76
 
77
77
  env["puma.mark_as_io_bound"] = -> { processor.mark_as_io_thread! }
78
+ env["puma.connection_closed?"] = -> { closed_socket?(socket) }
78
79
 
79
80
  begin
80
81
  status, headers, app_body = @thread_pool.with_force_shutdown do
@@ -165,7 +166,7 @@ module Puma
165
166
  if res_body.respond_to?(:each) && !resp_info[:response_hijack]
166
167
  # below converts app_body into body, dependent on app_body's characteristics, and
167
168
  # content_length will be set if it can be determined
168
- if !content_length && !resp_info[:transfer_encoding] && status != 204
169
+ if !content_length && !resp_info[:transfer_encoding] && status != 204 && status != 304
169
170
  if res_body.respond_to?(:to_ary) && (array_body = res_body.to_ary) &&
170
171
  array_body.is_a?(Array)
171
172
  body = array_body.compact
@@ -176,16 +177,16 @@ module Puma
176
177
  elsif res_body.respond_to?(:to_path) && (fn = res_body.to_path) &&
177
178
  File.readable?(fn)
178
179
  body = File.open fn, 'rb'
179
- content_length = body.size
180
180
  close_body = true
181
+ content_length = body.size
181
182
  else
182
183
  body = res_body
183
184
  end
184
185
  elsif !res_body.is_a?(::File) && res_body.respond_to?(:to_path) &&
185
186
  (fn = res_body.to_path) && File.readable?(fn = res_body.to_path)
186
187
  body = File.open fn, 'rb'
187
- content_length = body.size
188
188
  close_body = true
189
+ content_length = body.size
189
190
  elsif !res_body.is_a?(::File) && res_body.respond_to?(:filename) &&
190
191
  res_body.respond_to?(:bytesize) && File.readable?(fn = res_body.filename)
191
192
  # Sprockets::Asset
@@ -246,10 +247,11 @@ module Puma
246
247
  end
247
248
 
248
249
  fast_write_response socket, body, io_buffer, chunked, content_length.to_i
249
- body.close if close_body
250
250
 
251
251
  # if we're shutting down, close keep_alive connections
252
252
  !shutting_down? && keep_alive ? :keep_alive : :close
253
+ ensure
254
+ body.close if close_body
253
255
  end
254
256
 
255
257
  def write_pending_interim(socket, client)
@@ -430,6 +432,12 @@ module Puma
430
432
  end
431
433
  private :fetch_status_code
432
434
 
435
+ def request_has_close?(env)
436
+ conn = env.fetch(HTTP_CONNECTION, "").downcase
437
+ conn.include?(CLOSE) && conn.split(",").any? { |option| option.strip == CLOSE }
438
+ end
439
+ private :request_has_close?
440
+
433
441
  # Processes and write headers to the IOBuffer.
434
442
  # @param env [Hash] see Puma::Client#env, from request
435
443
  # @param status [Integer] the status returned by the Rack application
@@ -453,7 +461,7 @@ module Puma
453
461
  http_11 = env[SERVER_PROTOCOL] == HTTP_11
454
462
  if http_11
455
463
  resp_info[:allow_chunked] = true
456
- resp_info[:keep_alive] = env.fetch(HTTP_CONNECTION, "").downcase != CLOSE
464
+ resp_info[:keep_alive] = !request_has_close?(env)
457
465
 
458
466
  # An optimization. The most common response is 200, so we can
459
467
  # reply with the proper 200 status without having to compute
data/lib/puma/runner.rb CHANGED
@@ -30,7 +30,7 @@ module Puma
30
30
  def wakeup!
31
31
  return unless @wakeup
32
32
 
33
- @wakeup.write PIPE_WAKEUP unless @wakeup.closed?
33
+ @wakeup.write_nonblock(PIPE_WAKEUP) unless @wakeup.closed?
34
34
 
35
35
  rescue SystemCallError, IOError
36
36
  end
@@ -77,6 +77,10 @@ module Puma
77
77
  control = Puma::Server.new app, nil,
78
78
  { min_threads: 0, max_threads: 1, queue_requests: false, log_writer: @log_writer }
79
79
 
80
+ if (activated = @launcher.control_activated_socket)
81
+ control.binder.activated_sockets.store(*activated)
82
+ end
83
+
80
84
  begin
81
85
  control.binder.parse [str], nil, 'Starting control server'
82
86
  rescue Errno::EADDRINUSE, Errno::EACCES => e
@@ -49,8 +49,11 @@ module Puma
49
49
  WATCHDOG = "WATCHDOG=1"
50
50
  FDSTORE = "FDSTORE=1"
51
51
 
52
+ # @note When the service manager expects watchdog pings, this sends an
53
+ # immediate `WATCHDOG=1` in the same datagram, which closes the watchdog
54
+ # timeout window between startup and the first periodic watchdog tick.
52
55
  def self.ready(unset_env=false)
53
- notify(READY, unset_env)
56
+ notify(watchdog? ? "#{READY}\n#{WATCHDOG}" : READY, unset_env)
54
57
  end
55
58
 
56
59
  def self.reloading(unset_env=false)
data/lib/puma/server.rb CHANGED
@@ -109,6 +109,8 @@ module Puma
109
109
  @enable_keep_alives &&= @queue_requests
110
110
  @io_selector_backend = @options[:io_selector_backend]
111
111
  @http_content_length_limit = @options[:http_content_length_limit]
112
+ @continue_callback = @options[:continue_callback]
113
+ @allow_underscore_headers = @options.fetch(:allow_underscore_headers, true)
112
114
  @cluster_accept_loop_delay = ClusterAcceptLoopDelay.new(
113
115
  workers: @options[:workers],
114
116
  max_delay: @options[:wait_for_less_busy_worker] || 0 # Real default is in Configuration::DEFAULTS, this is for unit testing
@@ -412,13 +414,21 @@ module Puma
412
414
  # clients until the code is finished.
413
415
  pool.wait_while_out_of_band_running
414
416
 
415
- # A well rested herd (cluster) runs faster
416
- if @cluster_accept_loop_delay.on? && (busy_threads_plus_todo = pool.busy_threads) > 0
417
- delay = @cluster_accept_loop_delay.calculate(
418
- max_threads: @max_threads,
419
- busy_threads_plus_todo: busy_threads_plus_todo
420
- )
421
- sleep(delay)
417
+ unless shutting_down?
418
+ if @queue_requests
419
+ if @cluster_accept_loop_delay.on? && (busy_threads_plus_todo = pool.busy_threads) > 0
420
+ delay = @cluster_accept_loop_delay.calculate(
421
+ max_threads: @max_threads,
422
+ busy_threads_plus_todo: busy_threads_plus_todo
423
+ )
424
+ sleep(delay)
425
+ end
426
+ else
427
+ pool.wait_until_not_full
428
+
429
+ # A stop may have arrived while waiting for a thread
430
+ break if check.wait_readable(0) && handle_check
431
+ end
422
432
  end
423
433
 
424
434
  io = begin
@@ -486,12 +496,14 @@ module Puma
486
496
  client.env_set_http_version = @env_set_http_version
487
497
  client.http_content_length_limit = @http_content_length_limit
488
498
  client.supported_http_methods = @supported_http_methods
499
+ client.allow_underscore_headers = @allow_underscore_headers
500
+ client.continue_callback = @continue_callback
489
501
  client
490
502
  end
491
503
 
492
504
  # :nodoc:
493
505
  def handle_check
494
- cmd = @check.read(1)
506
+ cmd = @check.sysread 1
495
507
 
496
508
  case cmd
497
509
  when STOP_COMMAND
@@ -568,8 +580,12 @@ module Puma
568
580
  if @thread_pool.waiting > 0
569
581
  can_loop = true
570
582
  else
571
- @thread_pool << client
572
- close_socket = false
583
+ @thread_pool.with_mutex do
584
+ unless shutting_down?
585
+ @thread_pool << client
586
+ close_socket = false
587
+ end
588
+ end
573
589
  end
574
590
  elsif @queue_requests
575
591
  client.set_timeout @persistent_timeout
@@ -646,6 +662,13 @@ module Puma
646
662
  when HttpParserError501
647
663
  response_to_error(client, requests, e, 501)
648
664
  @log_writer.parse_error e, client
665
+ when ContinueRejected
666
+ status, headers, res_body = e.response
667
+ begin
668
+ prepare_response(status, headers, res_body, requests, client)
669
+ ensure
670
+ res_body.close if res_body.respond_to? :close
671
+ end
649
672
  else
650
673
  response_to_error(client, requests, e, 500)
651
674
  @log_writer.unknown_error e, nil, "Read"
@@ -708,16 +731,18 @@ module Puma
708
731
  end
709
732
 
710
733
  def notify_safely(message)
711
- @notify << message
712
- rescue IOError, NoMethodError, Errno::EPIPE, Errno::EBADF
713
- # The server, in another thread, is shutting down
714
- rescue RuntimeError => e
715
- # Temporary workaround for https://bugs.ruby-lang.org/issues/13239
716
- if e.message.include?('IOError')
717
- # ignore
718
- else
719
- raise e
720
- end
734
+ # Use write_nonblock rather than `<<` or `syswrite`. This method is
735
+ # called from signal handlers (TERM/USR2/INT/HUP, and the cluster
736
+ # worker's SIGTERM trap). On Ruby < 4.0, a blocking write from trap
737
+ # context that is still in flight when handle_servers' ensure block
738
+ # runs IO#close leaves the closing thread parked in IO#close forever
739
+ # (puma#3677). write_nonblock is a single write(2) that never waits,
740
+ # so IO#close has nothing to wait for. If the pipe is full a command
741
+ # is already pending, so dropping this one is harmless.
742
+ notify = @notify
743
+ notify.write_nonblock(message, exception: false) if notify && !notify.closed?
744
+ rescue IOError, NoMethodError, Errno::EPIPE, Errno::EBADF => e
745
+ @log_writer.unknown_error(e, nil, "notify_safely error") if Puma::IS_MRI && RUBY_VERSION > '3.3'
721
746
  end
722
747
  private :notify_safely
723
748
 
@@ -7,7 +7,6 @@ require_relative 'server_plugin_control'
7
7
 
8
8
  module Puma
9
9
 
10
- # Add `Thread#puma_server` and `Thread#puma_server=`
11
10
  Thread.attr_accessor(:puma_server)
12
11
 
13
12
  # Internal Docs for A simple thread pool management object.
@@ -211,6 +210,7 @@ module Puma
211
210
  processor.marked_as_io_thread = false
212
211
  else
213
212
  # We're already at max threads, so we exit the extra io thread.
213
+ @spawned -= 1
214
214
  @processors.delete(processor)
215
215
  trigger_before_thread_exit_hooks
216
216
  Thread.exit
@@ -314,6 +314,17 @@ module Puma
314
314
  end
315
315
  end
316
316
 
317
+ def wait_until_not_full
318
+ with_mutex do
319
+ while true
320
+ return if @shutdown
321
+ return if (@spawned - @waiting + @todo.size) < @max
322
+
323
+ @not_full.wait @mutex
324
+ end
325
+ end
326
+ end
327
+
317
328
  # @version 5.0.0
318
329
  def with_mutex(&block)
319
330
  @mutex.owned? ?
data/lib/puma.rb CHANGED
@@ -82,7 +82,7 @@ module Puma
82
82
  #
83
83
  def self.deprecate_method_change(method_old, method_caller, method_new)
84
84
  if method_old == method_caller
85
- warn "Use '#{method_new}', '#{method_caller}' is deprecated and will be removed in v8"
85
+ warn "Use '#{method_new}', '#{method_caller}' is deprecated and will be removed in Puma v9"
86
86
  end
87
87
  end
88
88
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puma
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.0.3
4
+ version: 8.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Phoenix
@@ -43,15 +43,19 @@ files:
43
43
  - bin/puma
44
44
  - bin/puma-wild
45
45
  - bin/pumactl
46
- - docs/5.0-Upgrade.md
47
- - docs/6.0-Upgrade.md
48
- - docs/7.0-Upgrade.md
49
- - docs/8.0-Upgrade.md
50
46
  - docs/architecture.md
47
+ - docs/binding.md
48
+ - docs/cluster_mode.md
51
49
  - docs/compile_options.md
52
- - docs/deployment.md
53
- - docs/fork_worker.md
54
- - docs/grpc.md
50
+ - docs/configuration.md
51
+ - docs/control_server.md
52
+ - docs/deployment/README.md
53
+ - docs/deployment/kubernetes.md
54
+ - docs/deployment/nginx.md
55
+ - docs/deployment/rc.d/README.md
56
+ - docs/deployment/rc.d/puma
57
+ - docs/deployment/rc.d/puma.conf
58
+ - docs/deployment/systemd.md
55
59
  - docs/images/favicon.svg
56
60
  - docs/images/puma-connection-flow-no-reactor.png
57
61
  - docs/images/puma-connection-flow.png
@@ -59,33 +63,19 @@ files:
59
63
  - docs/images/running-puma.svg
60
64
  - docs/images/standard-logo.svg
61
65
  - docs/java_options.md
62
- - docs/jungle/README.md
63
- - docs/jungle/rc.d/README.md
64
- - docs/jungle/rc.d/puma
65
- - docs/jungle/rc.d/puma.conf
66
- - docs/kubernetes.md
67
- - docs/nginx.md
68
66
  - docs/plugins.md
67
+ - docs/puma_support.md
69
68
  - docs/rails_dev_mode.md
70
69
  - docs/restart.md
71
70
  - docs/signals.md
72
- - docs/stats.md
73
- - docs/systemd.md
74
- - docs/testing_benchmarks_local_files.md
75
- - docs/testing_test_rackup_ci_files.md
76
- - ext/puma_http11/PumaHttp11Service.java
71
+ - docs/upgrading/5.0-Upgrade.md
72
+ - docs/upgrading/6.0-Upgrade.md
73
+ - docs/upgrading/7.0-Upgrade.md
74
+ - docs/upgrading/8.0-Upgrade.md
77
75
  - ext/puma_http11/extconf.rb
78
76
  - ext/puma_http11/http11_parser.c
79
77
  - ext/puma_http11/http11_parser.h
80
- - ext/puma_http11/http11_parser.java.rl
81
- - ext/puma_http11/http11_parser.rl
82
- - ext/puma_http11/http11_parser_common.rl
83
78
  - ext/puma_http11/mini_ssl.c
84
- - ext/puma_http11/no_ssl/PumaHttp11Service.java
85
- - ext/puma_http11/org/jruby/puma/EnvKey.java
86
- - ext/puma_http11/org/jruby/puma/Http11.java
87
- - ext/puma_http11/org/jruby/puma/Http11Parser.java
88
- - ext/puma_http11/org/jruby/puma/MiniSSL.java
89
79
  - ext/puma_http11/puma_http11.c
90
80
  - lib/puma.rb
91
81
  - lib/puma/app/status.rb
@@ -131,8 +121,6 @@ files:
131
121
  - lib/puma/thread_pool.rb
132
122
  - lib/puma/util.rb
133
123
  - lib/rack/handler/puma.rb
134
- - tools/Dockerfile
135
- - tools/trickletest.rb
136
124
  homepage: https://puma.io
137
125
  licenses:
138
126
  - BSD-3-Clause
@@ -157,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
145
  - !ruby/object:Gem::Version
158
146
  version: '0'
159
147
  requirements: []
160
- rubygems_version: 4.0.20
148
+ rubygems_version: 4.0.23
161
149
  specification_version: 4
162
150
  summary: A Ruby/Rack web server built for parallelism.
163
151
  test_files: []
data/docs/fork_worker.md DELETED
@@ -1,41 +0,0 @@
1
- # Fork-Worker Cluster Mode [Experimental]
2
-
3
- Puma 5 introduces an experimental new cluster-mode configuration option, `fork_worker` (`--fork-worker` from the CLI). This mode causes Puma to fork additional workers from worker 0, instead of directly from the master process:
4
-
5
- ```
6
- 10000 \_ puma 4.3.3 (tcp://0.0.0.0:9292) [puma]
7
- 10001 \_ puma: cluster worker 0: 10000 [puma]
8
- 10002 \_ puma: cluster worker 1: 10000 [puma]
9
- 10003 \_ puma: cluster worker 2: 10000 [puma]
10
- 10004 \_ puma: cluster worker 3: 10000 [puma]
11
- ```
12
-
13
- The `fork_worker` option allows your application to be initialized only once for copy-on-write memory savings, and it has two additional advantages:
14
-
15
- 1. **Compatible with phased restart.** Because the master process itself doesn't preload the application, this mode works with phased restart (`SIGUSR1` or `pumactl phased-restart`). When worker 0 reloads as part of a phased restart, it initializes a new copy of your application first, then the other workers reload by forking from this new worker already containing the new preloaded application.
16
-
17
- This allows a phased restart to complete as quickly as a hot restart (`SIGUSR2` or `pumactl restart`), while still minimizing downtime by staggering the restart across cluster workers.
18
-
19
- 2. **'Refork' for additional copy-on-write improvements in running applications.** Fork-worker mode introduces a new `refork` command that re-loads all nonzero workers by re-forking them from worker 0.
20
-
21
- This command can potentially improve memory utilization in large or complex applications that don't fully pre-initialize on startup, because the re-forked workers can share copy-on-write memory with a worker that has been running for a while and serving requests.
22
-
23
- You can trigger a refork by sending the cluster the `SIGURG` signal or running the `pumactl refork` command at any time. A refork will also automatically trigger once, after a certain number of requests have been processed by worker 0 (default 1000). To configure the number of requests before the auto-refork, pass a positive integer argument to `fork_worker` (e.g., `fork_worker 1000`), or `0` to disable.
24
-
25
- ### Usage Considerations
26
-
27
- - `fork_worker` introduces new `before_refork` and `after_refork` configuration hooks. Note the following:
28
- - When initially forking the parent process to the worker 0 child, `before_fork` will trigger on the parent process and `before_worker_boot` will trigger on the worker 0 child as normal.
29
- - When forking the worker 0 child to grandchild workers, `before_refork` and `after_refork` will trigger on the worker 0 child, and `before_worker_boot` will trigger on each grandchild worker.
30
- - For clarity, `before_fork` does not trigger on worker 0, and `after_refork` does not trigger on the grandchild.
31
- - As a general migration guide:
32
- - Copy any logic within your existing `before_fork` hook to the `before_refork` hook.
33
- - Consider to copy logic from your `before_worker_boot` hook to the `after_refork` hook, if it is needed to reset the state of worker 0 after it forks.
34
-
35
- ### Limitations
36
-
37
- - This mode is still very experimental so there may be bugs or edge-cases, particularly around expected behavior of existing hooks. Please open a [bug report](https://github.com/puma/puma/issues/new?template=bug_report.md) if you encounter any issues.
38
-
39
- - In order to fork new workers cleanly, worker 0 shuts down its server and stops serving requests so there are no open file descriptors or other kinds of shared global state between processes, and to maximize copy-on-write efficiency across the newly-forked workers. This may temporarily reduce total capacity of the cluster during a phased restart / refork.
40
-
41
- - In a cluster with `n` workers, a normal phased restart stops and restarts workers one by one while the application is loaded in each process, so `n-1` workers are available serving requests during the restart. In a phased restart in fork-worker mode, the application is first loaded in worker 0 while `n-1` workers are available, then worker 0 remains stopped while the rest of the workers are reloaded one by one, leaving only `n-2` workers to be available for a brief period of time. Reloading the rest of the workers should be quick because the application is preloaded at that point, but there may be situations where it can take longer (slow clients, long-running application code, slow worker-fork hooks, etc).
data/docs/grpc.md DELETED
@@ -1,62 +0,0 @@
1
- # Using gRPC with Puma in Clustered Mode
2
-
3
- This guide shows how to set up gRPC with Puma in a clustered environment using Puma's hooks to manage gRPC's lifecycle methods during forking.
4
-
5
- ## The Problem
6
-
7
- In a clustered Puma setup, you might encounter the following error when using gRPC:
8
-
9
- ```
10
- grpc cannot be used between calls to GRPC.prefork and GRPC.postfork_child or GRPC.postfork_parent
11
- ```
12
-
13
- To work correctly, gRPC needs these methods called at specific points in the process lifecycle:
14
- - `GRPC.prefork`: Called before forking.
15
- - `GRPC.postfork_child`: Called in the child process after forking.
16
- - `GRPC.postfork_parent`: Called in the parent process after forking.
17
-
18
- Puma provides hooks such as `before_worker_fork`, `after_worker_fork`, and `before_worker_boot` to execute code during these lifecycle events. Understanding the behavior of these hooks is key to ensuring gRPC operates correctly in a clustered setup.
19
-
20
- ## The Solution
21
-
22
- ### Example Configuration
23
-
24
- This configuration integrates gRPC's lifecycle methods in a clustered Puma setup and works whether preloading is enabled or not.
25
-
26
- ```ruby
27
- # config/puma.rb
28
-
29
- is_mac = RUBY_PLATFORM.include?("darwin")
30
-
31
- before_worker_fork do |index|
32
- GRPC.prefork unless is_mac
33
- end
34
-
35
- after_worker_fork do |index|
36
- GRPC.postfork_parent unless is_mac
37
- end
38
-
39
- before_worker_boot do
40
- GRPC.postfork_child unless is_mac
41
- end
42
- ```
43
-
44
- ### Understanding the Lifecycle and Hooks
45
-
46
- Puma's hooks determine when to call gRPC's lifecycle methods. Each hook plays a specific role in managing the lifecycle during forking:
47
-
48
- - **`before_worker_fork`**:
49
- - This hook runs before forking workers and is where you call `GRPC.prefork`.
50
- - In preloading setups (default in Puma v7), it runs in the **master process** before workers are forked, as the application is preloaded in the master process.
51
- - Without preloading, it still runs in the **master process** before forking workers, but the application is not preloaded.
52
- - `GRPC.prefork` is called here to prepare GRPC for the forking process.
53
-
54
- - **`after_worker_fork`**:
55
- - This hook always runs in the **master process** after a worker is forked, regardless of whether preloading is enabled.
56
- - Call `GRPC.postfork_parent` here to finalize the master process's state after forking.
57
-
58
- - **`before_worker_boot`**:
59
- - This hook always runs in the **worker process** after it is forked, regardless of whether preloading is enabled.
60
- - Call `GRPC.postfork_child` here to finalize the worker's state.
61
-
62
- **Note**: On macOS, these methods are skipped because gRPC does not require them due to differences in how forking works.
@@ -1,9 +0,0 @@
1
- # Puma as a service
2
-
3
- ## Systemd
4
-
5
- See [/docs/systemd](https://github.com/puma/puma/blob/main/docs/systemd.md).
6
-
7
- ## rc.d
8
-
9
- See `/docs/jungle/rc.d` for FreeBSD's rc.d scripts