puma 6.4.1 → 7.2.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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +407 -8
  3. data/README.md +109 -49
  4. data/docs/deployment.md +58 -23
  5. data/docs/fork_worker.md +11 -1
  6. data/docs/java_options.md +54 -0
  7. data/docs/jungle/README.md +1 -1
  8. data/docs/kubernetes.md +11 -16
  9. data/docs/plugins.md +6 -2
  10. data/docs/restart.md +2 -2
  11. data/docs/signals.md +21 -21
  12. data/docs/stats.md +11 -5
  13. data/docs/systemd.md +14 -5
  14. data/ext/puma_http11/extconf.rb +20 -32
  15. data/ext/puma_http11/mini_ssl.c +29 -9
  16. data/ext/puma_http11/org/jruby/puma/Http11.java +40 -9
  17. data/ext/puma_http11/puma_http11.c +125 -118
  18. data/lib/puma/app/status.rb +11 -3
  19. data/lib/puma/binder.rb +21 -11
  20. data/lib/puma/cli.rb +10 -8
  21. data/lib/puma/client.rb +183 -83
  22. data/lib/puma/cluster/worker.rb +24 -21
  23. data/lib/puma/cluster/worker_handle.rb +38 -8
  24. data/lib/puma/cluster.rb +73 -47
  25. data/lib/puma/cluster_accept_loop_delay.rb +91 -0
  26. data/lib/puma/commonlogger.rb +3 -3
  27. data/lib/puma/configuration.rb +131 -60
  28. data/lib/puma/const.rb +31 -12
  29. data/lib/puma/control_cli.rb +10 -6
  30. data/lib/puma/detect.rb +2 -0
  31. data/lib/puma/dsl.rb +411 -121
  32. data/lib/puma/error_logger.rb +7 -5
  33. data/lib/puma/events.rb +25 -10
  34. data/lib/puma/io_buffer.rb +8 -4
  35. data/lib/puma/jruby_restart.rb +0 -16
  36. data/lib/puma/launcher/bundle_pruner.rb +1 -1
  37. data/lib/puma/launcher.rb +73 -55
  38. data/lib/puma/log_writer.rb +9 -9
  39. data/lib/puma/minissl/context_builder.rb +1 -0
  40. data/lib/puma/minissl.rb +1 -1
  41. data/lib/puma/null_io.rb +26 -0
  42. data/lib/puma/plugin/systemd.rb +3 -3
  43. data/lib/puma/rack/urlmap.rb +1 -1
  44. data/lib/puma/reactor.rb +19 -13
  45. data/lib/puma/request.rb +71 -39
  46. data/lib/puma/runner.rb +15 -17
  47. data/lib/puma/sd_notify.rb +1 -4
  48. data/lib/puma/server.rb +134 -73
  49. data/lib/puma/single.rb +7 -4
  50. data/lib/puma/state_file.rb +3 -2
  51. data/lib/puma/thread_pool.rb +57 -80
  52. data/lib/puma/util.rb +0 -7
  53. data/lib/puma.rb +10 -0
  54. data/lib/rack/handler/puma.rb +10 -7
  55. data/tools/Dockerfile +15 -5
  56. metadata +14 -15
  57. data/ext/puma_http11/ext_help.h +0 -15
data/lib/puma/cluster.rb CHANGED
@@ -22,7 +22,8 @@ module Puma
22
22
  @workers = []
23
23
  @next_check = Time.now
24
24
 
25
- @phased_restart = false
25
+ @worker_max = [] # keeps track of 'max' stat values
26
+ @pending_phased_restart = false
26
27
  end
27
28
 
28
29
  # Returns the list of cluster worker handles.
@@ -44,10 +45,14 @@ module Puma
44
45
  end
45
46
  end
46
47
 
47
- def start_phased_restart
48
- @events.fire_on_restart!
48
+ def start_phased_restart(refork = false)
49
+ @events.fire_before_restart!
49
50
  @phase += 1
50
- log "- Starting phased worker restart, phase: #{@phase}"
51
+ if refork
52
+ log "- Starting worker refork, phase: #{@phase}"
53
+ else
54
+ log "- Starting phased worker restart, phase: #{@phase}"
55
+ end
51
56
 
52
57
  # Be sure to change the directory again before loading
53
58
  # the app. This way we can pick up new code.
@@ -87,6 +92,10 @@ module Puma
87
92
 
88
93
  if @options[:fork_worker] && all_workers_in_phase?
89
94
  @fork_writer << "0\n"
95
+
96
+ if worker_at(0).phase > 0
97
+ @fork_writer << "-2\n"
98
+ end
90
99
  end
91
100
  end
92
101
 
@@ -162,7 +171,7 @@ module Puma
162
171
  (@workers.map(&:pid) - idle_timed_out_worker_pids).empty?
163
172
  end
164
173
 
165
- def check_workers
174
+ def check_workers(refork = false)
166
175
  return if @next_check >= Time.now
167
176
 
168
177
  @next_check = Time.now + @options[:worker_check_interval]
@@ -177,10 +186,15 @@ module Puma
177
186
  # we need to phase any workers out (which will restart
178
187
  # in the right phase).
179
188
  #
180
- w = @workers.find { |x| x.phase != @phase }
189
+ w = @workers.find { |x| x.phase < @phase }
181
190
 
182
191
  if w
183
- log "- Stopping #{w.pid} for phased upgrade..."
192
+ if refork
193
+ log "- Stopping #{w.pid} for refork..."
194
+ else
195
+ log "- Stopping #{w.pid} for phased upgrade..."
196
+ end
197
+
184
198
  unless w.term?
185
199
  w.term
186
200
  log "- #{w.signal} sent to #{w.pid}..."
@@ -207,12 +221,11 @@ module Puma
207
221
  pipes[:wakeup] = @wakeup
208
222
  end
209
223
 
210
- server = start_server if preload?
211
224
  new_worker = Worker.new index: index,
212
225
  master: master,
213
226
  launcher: @launcher,
214
227
  pipes: pipes,
215
- server: server
228
+ app: (app if preload?)
216
229
  new_worker.run
217
230
  end
218
231
 
@@ -224,7 +237,7 @@ module Puma
224
237
  def phased_restart(refork = false)
225
238
  return false if @options[:preload_app] && !refork
226
239
 
227
- @phased_restart = true
240
+ @pending_phased_restart = refork ? :refork : true
228
241
  wakeup!
229
242
 
230
243
  true
@@ -254,11 +267,14 @@ module Puma
254
267
  end
255
268
 
256
269
  # Inside of a child process, this will return all zeroes, as @workers is only populated in
257
- # the master process.
270
+ # the master process. Calling this also resets stat 'max' values to zero.
258
271
  # @!attribute [r] stats
272
+ # @return [Hash]
273
+
259
274
  def stats
260
275
  old_worker_count = @workers.count { |w| w.phase != @phase }
261
276
  worker_status = @workers.map do |w|
277
+ w.reset_max
262
278
  {
263
279
  started_at: utc_iso8601(w.started_at),
264
280
  pid: w.pid,
@@ -269,7 +285,6 @@ module Puma
269
285
  last_status: w.last_status,
270
286
  }
271
287
  end
272
-
273
288
  {
274
289
  started_at: utc_iso8601(@started_at),
275
290
  workers: @workers.size,
@@ -338,7 +353,7 @@ module Puma
338
353
 
339
354
  stop_workers
340
355
  stop
341
- @events.fire_on_stopped!
356
+ @events.fire_after_stopped!
342
357
  raise(SignalException, "SIGTERM") if @options[:raise_exception_on_sigterm]
343
358
  exit 0 # Clean exit, workers were stopped
344
359
  end
@@ -348,8 +363,6 @@ module Puma
348
363
  def run
349
364
  @status = :run
350
365
 
351
- @idle_workers = {}
352
-
353
366
  output_header "cluster"
354
367
 
355
368
  # This is aligned with the output from Runner, see Runner#output_header
@@ -357,16 +370,12 @@ module Puma
357
370
 
358
371
  if preload?
359
372
  # Threads explicitly marked as fork safe will be ignored. Used in Rails,
360
- # but may be used by anyone. Note that we need to explicit
361
- # Process::Waiter check here because there's a bug in Ruby 2.6 and below
362
- # where calling thread_variable_get on a Process::Waiter will segfault.
363
- # We can drop that clause once those versions of Ruby are no longer
364
- # supported.
365
- fork_safe = ->(t) { !t.is_a?(Process::Waiter) && t.thread_variable_get(:fork_safe) }
373
+ # but may be used by anyone.
374
+ fork_safe = ->(t) { t.thread_variable_get(:fork_safe) }
366
375
 
367
376
  before = Thread.list.reject(&fork_safe)
368
377
 
369
- log "* Restarts: (\u2714) hot (\u2716) phased"
378
+ log "* Restarts: (\u2714) hot (\u2716) phased (#{@options[:fork_worker] ? "\u2714" : "\u2716"}) refork"
370
379
  log "* Preloading application"
371
380
  load_and_bind
372
381
 
@@ -384,7 +393,7 @@ module Puma
384
393
  end
385
394
  end
386
395
  else
387
- log "* Restarts: (\u2714) hot (\u2714) phased"
396
+ log "* Restarts: (\u2714) hot (\u2714) phased (#{@options[:fork_worker] ? "\u2714" : "\u2716"}) refork"
388
397
 
389
398
  unless @config.app_configured?
390
399
  error "No application configured, nothing to run"
@@ -411,6 +420,7 @@ module Puma
411
420
 
412
421
  log "Use Ctrl-C to stop"
413
422
 
423
+ warn_ruby_mn_threads
414
424
  single_worker_warning
415
425
 
416
426
  redirect_io
@@ -440,30 +450,37 @@ module Puma
440
450
 
441
451
  while @status == :run
442
452
  begin
443
- if all_workers_idle_timed_out?
453
+ if @options[:idle_timeout] && all_workers_idle_timed_out?
444
454
  log "- All workers reached idle timeout"
445
455
  break
446
456
  end
447
457
 
448
- if @phased_restart
449
- start_phased_restart
450
- @phased_restart = false
451
- in_phased_restart = true
458
+ if @pending_phased_restart
459
+ start_phased_restart(@pending_phased_restart == :refork)
460
+
461
+ in_phased_restart = @pending_phased_restart
462
+ @pending_phased_restart = false
463
+
452
464
  workers_not_booted = @options[:workers]
465
+ # worker 0 is not restarted on refork
466
+ workers_not_booted -= 1 if in_phased_restart == :refork
453
467
  end
454
468
 
455
- check_workers
469
+ check_workers(in_phased_restart == :refork)
456
470
 
457
471
  if read.wait_readable([0, @next_check - Time.now].max)
458
472
  req = read.read_nonblock(1)
473
+ next unless req
459
474
 
460
- @next_check = Time.now if req == "!"
461
- next if !req || req == "!"
475
+ if req == PIPE_WAKEUP
476
+ @next_check = Time.now
477
+ next
478
+ end
462
479
 
463
480
  result = read.gets
464
481
  pid = result.to_i
465
482
 
466
- if req == "b" || req == "f"
483
+ if req == PIPE_BOOT || req == PIPE_FORK
467
484
  pid, idx = result.split(':').map(&:to_i)
468
485
  w = worker_at idx
469
486
  w.pid = pid if w.pid.nil?
@@ -471,36 +488,36 @@ module Puma
471
488
 
472
489
  if w = @workers.find { |x| x.pid == pid }
473
490
  case req
474
- when "b"
491
+ when PIPE_BOOT
475
492
  w.boot!
476
493
  log "- Worker #{w.index} (PID: #{pid}) booted in #{w.uptime.round(2)}s, phase: #{w.phase}"
477
494
  @next_check = Time.now
478
495
  workers_not_booted -= 1
479
- when "e"
496
+ when PIPE_EXTERNAL_TERM
480
497
  # external term, see worker method, Signal.trap "SIGTERM"
481
498
  w.term!
482
- when "t"
499
+ when PIPE_TERM
483
500
  w.term unless w.term?
484
- when "p"
501
+ when PIPE_PING
485
502
  status = result.sub(/^\d+/,'').chomp
486
503
  w.ping!(status)
487
504
  @events.fire(:ping!, w)
488
505
 
489
- if in_phased_restart && workers_not_booted.positive? && w0 = worker_at(0)
506
+ if in_phased_restart && @options[:fork_worker] && workers_not_booted.positive? && w0 = worker_at(0)
490
507
  w0.ping!(status)
491
508
  @events.fire(:ping!, w0)
492
509
  end
493
510
 
494
511
  if !booted && @workers.none? {|worker| worker.last_status.empty?}
495
- @events.fire_on_booted!
512
+ @events.fire_after_booted!
496
513
  debug_loaded_extensions("Loaded Extensions - master:") if @log_writer.debug?
497
514
  booted = true
498
515
  end
499
- when "i"
500
- if @idle_workers[pid]
501
- @idle_workers.delete pid
516
+ when PIPE_IDLE
517
+ if idle_workers[pid]
518
+ idle_workers.delete pid
502
519
  else
503
- @idle_workers[pid] = true
520
+ idle_workers[pid] = true
504
521
  end
505
522
  end
506
523
  else
@@ -509,7 +526,7 @@ module Puma
509
526
  end
510
527
 
511
528
  if in_phased_restart && workers_not_booted.zero?
512
- @events.fire_on_booted!
529
+ @events.fire_after_booted!
513
530
  debug_loaded_extensions("Loaded Extensions - master:") if @log_writer.debug?
514
531
  in_phased_restart = false
515
532
  end
@@ -560,9 +577,14 @@ module Puma
560
577
  @workers.reject! do |w|
561
578
  next false if w.pid.nil?
562
579
  begin
563
- # When `fork_worker` is enabled, some worker may not be direct children, but grand children.
564
- # Because of this they won't be reaped by `Process.wait2(-1)`, so we need to check them individually)
565
- if reaped_children.delete(w.pid) || (@options[:fork_worker] && Process.wait(w.pid, Process::WNOHANG))
580
+ # We may need to check the PID individually because:
581
+ # 1. From Ruby versions 2.6 to 3.2, `Process.detach` can prevent or delay
582
+ # `Process.wait2(-1)` from detecting a terminated process: https://bugs.ruby-lang.org/issues/19837.
583
+ # 2. When `fork_worker` is enabled, some worker may not be direct children,
584
+ # but grand children. Because of this they won't be reaped by `Process.wait2(-1)`.
585
+ if (status = reaped_children.delete(w.pid) || Process.wait2(w.pid, Process::WNOHANG)&.last)
586
+ w.process_status = status
587
+ @config.run_hooks(:after_worker_shutdown, w, @log_writer)
566
588
  true
567
589
  else
568
590
  w.term if w.term?
@@ -602,7 +624,11 @@ module Puma
602
624
  end
603
625
 
604
626
  def idle_timed_out_worker_pids
605
- @idle_workers.keys
627
+ idle_workers.keys
628
+ end
629
+
630
+ def idle_workers
631
+ @idle_workers ||= {}
606
632
  end
607
633
  end
608
634
  end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Puma
4
+ # Calculate a delay value for sleeping when running in clustered mode
5
+ #
6
+ # The main reason this is a class is so it can be unit tested independently.
7
+ # This makes modification easier in the future if we can encode properties of the
8
+ # delay into a test instead of relying on end-to-end testing only.
9
+ #
10
+ # This is an imprecise mechanism to address specific goals:
11
+ #
12
+ # - Evenly distribute requests across all workers at start
13
+ # - Evenly distribute CPU resources across all workers
14
+ #
15
+ # ## Goal: Distribute requests across workers at start
16
+ #
17
+ # There was a perf bug in Puma where one worker would wake up slightly before the rest and accept
18
+ # all the requests on the socket even though it didn't have enough resources to process all of them.
19
+ # This was originally fixed by never calling accept when a worker had more requests than threads
20
+ # already https://github.com/puma/puma/pull/3678/files/2736ebddb3fc8528e5150b5913fba251c37a8bf7#diff-a95f46e7ce116caddc9b9a9aa81004246d5210d5da5f4df90a818c780630166bL251-L291
21
+ #
22
+ # With the introduction of true keepalive support, there are two ways a request can come in:
23
+ # - A new request from a new client comes into the socket and it must be "accept"-ed
24
+ # - A keepalive request is served and the connection is retained. Another request is then accepted
25
+ #
26
+ # Ideally the server handles requests in the order they come in, and ideally it doesn't accept more requests than it can handle.
27
+ # These goals are contradictory, because when the server is at maximum capacity due to keepalive connections, it could mean we
28
+ # block all new requests, even if those came in before the new request on the older keepalive connection.
29
+ #
30
+ # ## Goal: Distribute CPU resources across all workers
31
+ #
32
+ # - This issue was opened https://github.com/puma/puma/issues/2078
33
+ #
34
+ # There are several entangled issues and it's not exactly clear what the root cause is, but the observable outcome
35
+ # was that performance was better with a small sleep, and that eventually became the default.
36
+ #
37
+ # An attempt to describe why this works is here: https://github.com/puma/puma/issues/2078#issuecomment-3287032470.
38
+ #
39
+ # Summarizing: The delay is for tuning the rate at which "accept" is called on the socket.
40
+ # Puma works by calling "accept" nonblock on the socket in a loop. When there are multiple workers
41
+ # (processes), they will "race" to accept a request at roughly the same rate. However, if one
42
+ # worker has all threads busy processing requests, then accepting a new request might "steal" it from
43
+ # a less busy worker. If a worker has no work to do, it should loop as fast as possible.
44
+ #
45
+ # ## Solution: Distribute requests across workers at start
46
+ #
47
+ # For now, both goals are framed as "load balancing" across workers (processes) and achieved through
48
+ # the same mechanism of sleeping longer to delay busier workers. Rather than the prior Puma 6.x
49
+ # and earlier behavior of using a binary on/off sleep value, we increase it an amount proportional
50
+ # to the load the server is under, capping the maximum delay to the scenario where all threads are busy
51
+ # and the todo list has reached a multiplier of the maximum number of threads.
52
+ #
53
+ # Private: API may change unexpectedly
54
+ class ClusterAcceptLoopDelay
55
+ attr_reader :max_delay
56
+
57
+ # Initialize happens once, `call` happens often. Perform global calculations here.
58
+ def initialize(
59
+ # Number of workers in the cluster
60
+ workers: ,
61
+ # Maximum delay in seconds i.e. 0.005 is 5 milliseconds
62
+ max_delay:
63
+ )
64
+ @on = max_delay > 0 && workers >= 2
65
+ @max_delay = max_delay.to_f
66
+
67
+ # Reach maximum delay when `max_threads * overload_multiplier` is reached in the system
68
+ @overload_multiplier = 25.0
69
+ end
70
+
71
+ def on?
72
+ @on
73
+ end
74
+
75
+ # We want the extreme values of this delay to be known (minimum and maximum) as well as
76
+ # a predictable curve between the two. i.e. no step functions or hard cliffs.
77
+ #
78
+ # Return value is always numeric. Returns 0 if there should be no delay.
79
+ def calculate(
80
+ # Number of threads working right now, plus number of requests in the todo list
81
+ busy_threads_plus_todo:,
82
+ # Maximum number of threads in the pool, note that the busy threads (alone) may go over this value at times
83
+ # if the pool needs to be reaped. The busy thread plus todo count may go over this value by a large amount.
84
+ max_threads:
85
+ )
86
+ max_value = @overload_multiplier * max_threads
87
+ # Approaches max delay when `busy_threads_plus_todo` approaches `max_value`
88
+ return max_delay * busy_threads_plus_todo.clamp(0, max_value) / max_value
89
+ end
90
+ end
91
+ end
@@ -29,13 +29,13 @@ module Puma
29
29
 
30
30
  CONTENT_LENGTH = 'Content-Length' # should be lower case from app,
31
31
  # Util::HeaderHash allows mixed
32
- HTTP_VERSION = Const::HTTP_VERSION
33
32
  HTTP_X_FORWARDED_FOR = Const::HTTP_X_FORWARDED_FOR
34
33
  PATH_INFO = Const::PATH_INFO
35
34
  QUERY_STRING = Const::QUERY_STRING
36
35
  REMOTE_ADDR = Const::REMOTE_ADDR
37
36
  REMOTE_USER = 'REMOTE_USER'
38
37
  REQUEST_METHOD = Const::REQUEST_METHOD
38
+ SERVER_PROTOCOL = Const::SERVER_PROTOCOL
39
39
 
40
40
  def initialize(app, logger=nil)
41
41
  @app = app
@@ -70,7 +70,7 @@ module Puma
70
70
  env[REQUEST_METHOD],
71
71
  env[PATH_INFO],
72
72
  env[QUERY_STRING].empty? ? "" : "?#{env[QUERY_STRING]}",
73
- env[HTTP_VERSION],
73
+ env[SERVER_PROTOCOL],
74
74
  now - began_at ]
75
75
 
76
76
  write(msg)
@@ -87,7 +87,7 @@ module Puma
87
87
  env[REQUEST_METHOD],
88
88
  env[PATH_INFO],
89
89
  env[QUERY_STRING].empty? ? "" : "?#{env[QUERY_STRING]}",
90
- env[HTTP_VERSION],
90
+ env[SERVER_PROTOCOL],
91
91
  status.to_s[0..3],
92
92
  length,
93
93
  now - began_at ]