puma 8.0.5 → 8.1.3
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.
- checksums.yaml +4 -4
- data/History.md +127 -2
- data/README.md +28 -378
- data/docs/binding.md +135 -0
- data/docs/cluster_mode.md +241 -0
- data/docs/configuration.md +78 -0
- data/docs/{stats.md → control_server.md} +28 -8
- data/docs/{deployment.md → deployment/README.md} +23 -7
- data/docs/{kubernetes.md → deployment/kubernetes.md} +4 -4
- data/docs/{systemd.md → deployment/systemd.md} +4 -2
- data/docs/plugins.md +62 -3
- data/docs/puma_support.md +92 -0
- data/docs/rails_dev_mode.md +2 -2
- data/docs/restart.md +8 -3
- data/docs/signals.md +4 -2
- data/docs/{5.0-Upgrade.md → upgrading/5.0-Upgrade.md} +3 -3
- data/docs/{6.0-Upgrade.md → upgrading/6.0-Upgrade.md} +3 -3
- data/docs/{7.0-Upgrade.md → upgrading/7.0-Upgrade.md} +2 -2
- data/docs/{8.0-Upgrade.md → upgrading/8.0-Upgrade.md} +4 -4
- data/ext/puma_http11/extconf.rb +2 -0
- data/ext/puma_http11/http11_parser.c +6 -2
- data/ext/puma_http11/mini_ssl.c +8 -4
- data/ext/puma_http11/puma_http11.c +24 -0
- data/lib/puma/binder.rb +29 -0
- data/lib/puma/cli.rb +6 -0
- data/lib/puma/client.rb +75 -29
- data/lib/puma/client_env.rb +32 -18
- data/lib/puma/cluster/worker.rb +109 -33
- data/lib/puma/cluster/worker_handle.rb +16 -0
- data/lib/puma/cluster.rb +173 -45
- data/lib/puma/commonlogger.rb +3 -3
- data/lib/puma/configuration.rb +22 -3
- data/lib/puma/const.rb +11 -9
- data/lib/puma/dsl.rb +265 -91
- data/lib/puma/launcher.rb +6 -4
- data/lib/puma/minissl.rb +2 -5
- data/lib/puma/plugin/systemd.rb +1 -1
- data/lib/puma/response.rb +13 -5
- data/lib/puma/runner.rb +5 -1
- data/lib/puma/sd_notify.rb +4 -1
- data/lib/puma/server.rb +45 -20
- data/lib/puma/thread_pool.rb +12 -1
- data/lib/puma.rb +1 -1
- metadata +17 -29
- data/docs/fork_worker.md +0 -41
- data/docs/grpc.md +0 -62
- data/docs/jungle/README.md +0 -9
- data/docs/testing_benchmarks_local_files.md +0 -150
- data/docs/testing_test_rackup_ci_files.md +0 -36
- data/ext/puma_http11/PumaHttp11Service.java +0 -17
- data/ext/puma_http11/http11_parser.java.rl +0 -131
- data/ext/puma_http11/http11_parser.rl +0 -149
- data/ext/puma_http11/http11_parser_common.rl +0 -54
- data/ext/puma_http11/no_ssl/PumaHttp11Service.java +0 -15
- data/ext/puma_http11/org/jruby/puma/EnvKey.java +0 -247
- data/ext/puma_http11/org/jruby/puma/Http11.java +0 -321
- data/ext/puma_http11/org/jruby/puma/Http11Parser.java +0 -441
- data/ext/puma_http11/org/jruby/puma/MiniSSL.java +0 -519
- data/tools/Dockerfile +0 -26
- data/tools/trickletest.rb +0 -44
- /data/docs/{nginx.md → deployment/nginx.md} +0 -0
- /data/docs/{jungle → deployment}/rc.d/README.md +0 -0
- /data/docs/{jungle → deployment}/rc.d/puma +0 -0
- /data/docs/{jungle → deployment}/rc.d/puma.conf +0 -0
data/lib/puma/client.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require_relative 'detect'
|
|
4
4
|
require_relative 'io_buffer'
|
|
5
5
|
require_relative 'client_env'
|
|
6
|
+
require_relative 'null_io'
|
|
6
7
|
require 'tempfile'
|
|
7
8
|
|
|
8
9
|
if Puma::IS_JRUBY
|
|
@@ -18,6 +19,17 @@ module Puma
|
|
|
18
19
|
|
|
19
20
|
class HttpParserError501 < IOError; end
|
|
20
21
|
|
|
22
|
+
# Raised when a +continue_callback+ answers an <tt>Expect: 100-continue</tt>
|
|
23
|
+
# request itself, so the request body is never read.
|
|
24
|
+
class ContinueRejected < StandardError
|
|
25
|
+
attr_reader :response
|
|
26
|
+
|
|
27
|
+
def initialize(response)
|
|
28
|
+
super("Expect: 100-continue answered by continue_callback")
|
|
29
|
+
@response = response
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
21
33
|
#———————————————————————— DO NOT USE — this class is for internal use only ———
|
|
22
34
|
|
|
23
35
|
|
|
@@ -46,12 +58,12 @@ module Puma
|
|
|
46
58
|
ALLOWED_TRANSFER_ENCODING = %w[compress deflate gzip].freeze
|
|
47
59
|
|
|
48
60
|
# chunked body validation
|
|
49
|
-
|
|
61
|
+
CHUNK_SIZE_VALID = /\A\h+\z/.freeze
|
|
50
62
|
CHUNK_VALID_ENDING = Const::LINE_END
|
|
51
63
|
CHUNK_VALID_ENDING_SIZE = CHUNK_VALID_ENDING.bytesize
|
|
52
64
|
|
|
53
65
|
# The maximum number of bytes we'll buffer looking for a valid
|
|
54
|
-
# chunk header.
|
|
66
|
+
# chunk header, and the maximum size of a trailer section.
|
|
55
67
|
MAX_CHUNK_HEADER_SIZE = 4096
|
|
56
68
|
|
|
57
69
|
SHUTDOWN_READ_GRACE = 1
|
|
@@ -78,7 +90,8 @@ module Puma
|
|
|
78
90
|
:tempfile, :io_buffer, :http_content_length_limit_exceeded,
|
|
79
91
|
:requests_served, :error_status_code, :bytes_read
|
|
80
92
|
|
|
81
|
-
attr_writer :peerip, :http_content_length_limit, :supported_http_methods
|
|
93
|
+
attr_writer :peerip, :http_content_length_limit, :supported_http_methods,
|
|
94
|
+
:allow_underscore_headers, :continue_callback
|
|
82
95
|
|
|
83
96
|
attr_accessor :remote_addr_header, :listener, :env_set_http_version,
|
|
84
97
|
:reactor_error
|
|
@@ -108,6 +121,8 @@ module Puma
|
|
|
108
121
|
|
|
109
122
|
@http_content_length_limit = nil
|
|
110
123
|
@http_content_length_limit_exceeded = nil
|
|
124
|
+
@allow_underscore_headers = true
|
|
125
|
+
@continue_callback = nil
|
|
111
126
|
@error_status_code = nil
|
|
112
127
|
@reactor_error = nil
|
|
113
128
|
|
|
@@ -354,6 +369,8 @@ module Puma
|
|
|
354
369
|
raise e unless HttpParserError === e && e.message.include?('non-SSL')
|
|
355
370
|
|
|
356
371
|
req, _ = @buffer.split "\r\n\r\n"
|
|
372
|
+
raise e if req.nil? || req.empty?
|
|
373
|
+
|
|
357
374
|
request_line, headers = req.split "\r\n", 2
|
|
358
375
|
|
|
359
376
|
# below checks for request issues and changes error message accordingly
|
|
@@ -384,6 +401,7 @@ module Puma
|
|
|
384
401
|
|
|
385
402
|
# processes the `env` and the request body
|
|
386
403
|
def process_env_body
|
|
404
|
+
validate_env
|
|
387
405
|
temp = setup_body
|
|
388
406
|
normalize_env
|
|
389
407
|
req_env_post_parse
|
|
@@ -496,10 +514,21 @@ module Puma
|
|
|
496
514
|
# @return [Boolean] true if the body can be completely read, false otherwise
|
|
497
515
|
#
|
|
498
516
|
def setup_body
|
|
517
|
+
# The parsers preserve underscores as commas to distinguish these aliases
|
|
518
|
+
# from real framing headers. Reject them before 100 Continue or body reads,
|
|
519
|
+
# regardless of allow_underscore_headers, to avoid proxy disagreements.
|
|
520
|
+
if @env.key?('HTTP_CONTENT,LENGTH') || @env.key?('HTTP_TRANSFER,ENCODING')
|
|
521
|
+
@error_status_code = 400
|
|
522
|
+
@env[HTTP_CONNECTION] = CLOSE
|
|
523
|
+
raise HttpParserError, 'Underscores are not allowed in HTTP framing headers'
|
|
524
|
+
end
|
|
525
|
+
|
|
499
526
|
@body_read_start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
|
500
527
|
|
|
501
528
|
if @env[HTTP_EXPECT] == CONTINUE
|
|
502
|
-
|
|
529
|
+
if @continue_callback && (response = @continue_callback.call(@env))
|
|
530
|
+
reject_continue response
|
|
531
|
+
end
|
|
503
532
|
write_interim HTTP_11_100
|
|
504
533
|
end
|
|
505
534
|
|
|
@@ -704,6 +733,8 @@ module Puma
|
|
|
704
733
|
end
|
|
705
734
|
|
|
706
735
|
def decode_chunk(chunk)
|
|
736
|
+
return finish_last_chunk(@prev_chunk + chunk) if @in_last_chunk
|
|
737
|
+
|
|
707
738
|
if @partial_part_left > 0
|
|
708
739
|
if @partial_part_left <= chunk.size
|
|
709
740
|
if @partial_part_left > 2
|
|
@@ -739,31 +770,18 @@ module Puma
|
|
|
739
770
|
# Puma doesn't process chunk extensions, but should parse if they're
|
|
740
771
|
# present, which is the reason for the semicolon regex
|
|
741
772
|
chunk_hex = line.strip[/\A[^;]+/]
|
|
742
|
-
|
|
743
|
-
|
|
773
|
+
unless CHUNK_SIZE_VALID.match? chunk_hex
|
|
774
|
+
err_msg = chunk_hex && !chunk_hex.empty? ?
|
|
775
|
+
"Invalid chunk size: '#{chunk_hex}'" :
|
|
776
|
+
"Chunk size cannot be empty or nil"
|
|
777
|
+
raise HttpParserError, err_msg
|
|
744
778
|
end
|
|
779
|
+
|
|
745
780
|
len = chunk_hex.to_i(16)
|
|
746
781
|
if len == 0
|
|
747
782
|
@in_last_chunk = true
|
|
748
783
|
@body.rewind
|
|
749
|
-
|
|
750
|
-
if rest.bytesize < CHUNK_VALID_ENDING_SIZE
|
|
751
|
-
@buffer = nil
|
|
752
|
-
@partial_part_left = CHUNK_VALID_ENDING_SIZE - rest.bytesize
|
|
753
|
-
return false
|
|
754
|
-
else
|
|
755
|
-
# if the next character is a CRLF, set buffer to everything after that CRLF
|
|
756
|
-
start_of_rest = if rest.start_with?(CHUNK_VALID_ENDING)
|
|
757
|
-
CHUNK_VALID_ENDING_SIZE
|
|
758
|
-
else # we have started a trailer section, which we do not support. skip it!
|
|
759
|
-
rest.index(CHUNK_VALID_ENDING*2) + CHUNK_VALID_ENDING_SIZE*2
|
|
760
|
-
end
|
|
761
|
-
|
|
762
|
-
@buffer = rest[start_of_rest..-1]
|
|
763
|
-
@buffer = nil if @buffer.empty?
|
|
764
|
-
set_ready
|
|
765
|
-
return true
|
|
766
|
-
end
|
|
784
|
+
return finish_last_chunk(io.read)
|
|
767
785
|
end
|
|
768
786
|
|
|
769
787
|
# Track the excess as a function of the size of the
|
|
@@ -816,12 +834,33 @@ module Puma
|
|
|
816
834
|
end
|
|
817
835
|
end
|
|
818
836
|
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
837
|
+
false
|
|
838
|
+
end
|
|
839
|
+
|
|
840
|
+
# Consume the terminator that ends a chunked body. Per RFC 9112 §7.1.2
|
|
841
|
+
# this is either a bare CRLF or an optional trailer section ending in
|
|
842
|
+
# CRLF*2. Puma does not surface trailers, so we skip past them. Bytes
|
|
843
|
+
# past the terminator belong to the next pipelined request.
|
|
844
|
+
def finish_last_chunk(rest)
|
|
845
|
+
if rest.start_with?(CHUNK_VALID_ENDING)
|
|
846
|
+
trailer_end = CHUNK_VALID_ENDING_SIZE
|
|
847
|
+
elsif idx = rest.index(CHUNK_VALID_ENDING * 2)
|
|
848
|
+
trailer_end = idx + CHUNK_VALID_ENDING_SIZE * 2
|
|
849
|
+
elsif rest.bytesize < MAX_CHUNK_HEADER_SIZE
|
|
850
|
+
@prev_chunk = rest
|
|
851
|
+
return false
|
|
852
|
+
end
|
|
853
|
+
|
|
854
|
+
if trailer_end.nil? || trailer_end > MAX_CHUNK_HEADER_SIZE
|
|
855
|
+
@env[HTTP_CONNECTION] = CLOSE
|
|
856
|
+
raise HttpParserError, "maximum size of chunk trailer exceeded"
|
|
824
857
|
end
|
|
858
|
+
|
|
859
|
+
@prev_chunk = ""
|
|
860
|
+
@buffer = rest[trailer_end..-1]
|
|
861
|
+
@buffer = nil if @buffer.empty?
|
|
862
|
+
set_ready
|
|
863
|
+
true
|
|
825
864
|
end
|
|
826
865
|
|
|
827
866
|
def set_ready
|
|
@@ -832,6 +871,13 @@ module Puma
|
|
|
832
871
|
@ready = true
|
|
833
872
|
end
|
|
834
873
|
|
|
874
|
+
def reject_continue(response)
|
|
875
|
+
@buffer = nil
|
|
876
|
+
@body = EmptyBody
|
|
877
|
+
@env[HTTP_CONNECTION] = 'close'
|
|
878
|
+
raise ContinueRejected, response
|
|
879
|
+
end
|
|
880
|
+
|
|
835
881
|
def raise_above_http_content_limit
|
|
836
882
|
@http_content_length_limit_exceeded = true
|
|
837
883
|
@buffer = nil
|
data/lib/puma/client_env.rb
CHANGED
|
@@ -12,6 +12,22 @@ module Puma
|
|
|
12
12
|
|
|
13
13
|
include Puma::Const
|
|
14
14
|
|
|
15
|
+
def validate_env
|
|
16
|
+
return unless @env[SERVER_PROTOCOL] == HTTP_11
|
|
17
|
+
|
|
18
|
+
unless @env.key? HTTP_HOST
|
|
19
|
+
@error_status_code = 400
|
|
20
|
+
@env[HTTP_CONNECTION] = CLOSE
|
|
21
|
+
raise HttpParserError, "Missing Host header"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
if @env[HTTP_HOST].empty?
|
|
25
|
+
@error_status_code = 400
|
|
26
|
+
@env[HTTP_CONNECTION] = CLOSE
|
|
27
|
+
raise HttpParserError, "Invalid Host header"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
15
31
|
# Given a Hash +env+ for the request read from +client+, add
|
|
16
32
|
# and fixup keys to comply with Rack's env guidelines.
|
|
17
33
|
# @param env [Hash] see Puma::Client#env, from request
|
|
@@ -105,9 +121,10 @@ module Puma
|
|
|
105
121
|
# avoid allocation in the common case (ie there are no headers
|
|
106
122
|
# with `,` in their names), that's why it has the extra conditionals.
|
|
107
123
|
#
|
|
108
|
-
# @note If
|
|
109
|
-
#
|
|
110
|
-
#
|
|
124
|
+
# @note If underscore headers are disallowed, `,` headers are discarded.
|
|
125
|
+
# Otherwise, if a normalized version of a `,` header already exists, we
|
|
126
|
+
# ignore the `,` version. This prevents clobbering headers managed by
|
|
127
|
+
# proxies but not by clients (Like X-Forwarded-For).
|
|
111
128
|
#
|
|
112
129
|
# @param env [Hash] see Puma::Client#env, from request, modifies in place
|
|
113
130
|
# @version 5.0.3
|
|
@@ -115,28 +132,25 @@ module Puma
|
|
|
115
132
|
def req_env_post_parse
|
|
116
133
|
to_delete = nil
|
|
117
134
|
to_add = nil
|
|
135
|
+
underscore_headers = nil
|
|
118
136
|
|
|
119
137
|
@env.each do |k,v|
|
|
120
|
-
|
|
121
|
-
if to_delete
|
|
122
|
-
to_delete << k
|
|
123
|
-
else
|
|
124
|
-
to_delete = [k]
|
|
125
|
-
end
|
|
138
|
+
next unless k.start_with?("HTTP_") && k.include?(",")
|
|
126
139
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
next
|
|
130
|
-
end
|
|
140
|
+
(underscore_headers ||= []) << k.delete_prefix("HTTP_").tr("_,", "-_")
|
|
141
|
+
next if @allow_underscore_headers && UNMASKABLE_HEADERS.key?(k)
|
|
131
142
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
end
|
|
143
|
+
(to_delete ||= []) << k
|
|
144
|
+
next unless @allow_underscore_headers
|
|
135
145
|
|
|
136
|
-
|
|
137
|
-
|
|
146
|
+
new_k = k.tr(",", "_")
|
|
147
|
+
next if @env.key?(new_k)
|
|
148
|
+
|
|
149
|
+
(to_add ||= {})[new_k] = v
|
|
138
150
|
end
|
|
139
151
|
|
|
152
|
+
@env[PUMA_UNDERSCORE_HEADERS] = underscore_headers if underscore_headers
|
|
153
|
+
|
|
140
154
|
if to_delete # rubocop:disable Style/SafeNavigation
|
|
141
155
|
to_delete.each { |k| env.delete(k) }
|
|
142
156
|
end
|
data/lib/puma/cluster/worker.rb
CHANGED
|
@@ -26,6 +26,7 @@ module Puma
|
|
|
26
26
|
@app = app
|
|
27
27
|
@server = nil
|
|
28
28
|
@hook_data = {}
|
|
29
|
+
@worker_pids = []
|
|
29
30
|
end
|
|
30
31
|
|
|
31
32
|
def run
|
|
@@ -72,12 +73,7 @@ module Puma
|
|
|
72
73
|
|
|
73
74
|
if fork_worker
|
|
74
75
|
restart_server.clear
|
|
75
|
-
|
|
76
|
-
Signal.trap "SIGCHLD" do
|
|
77
|
-
wakeup! if worker_pids.reject! do |p|
|
|
78
|
-
Process.wait(p, Process::WNOHANG) rescue true
|
|
79
|
-
end
|
|
80
|
-
end
|
|
76
|
+
trap_sigchld
|
|
81
77
|
|
|
82
78
|
Thread.new do
|
|
83
79
|
Puma.set_thread_name "wrkr fork"
|
|
@@ -94,13 +90,24 @@ module Puma
|
|
|
94
90
|
elsif idx == 0 # restart server
|
|
95
91
|
restart_server << true << false
|
|
96
92
|
else # fork worker
|
|
97
|
-
|
|
93
|
+
pid = spawn_worker(idx)
|
|
98
94
|
@worker_write << "#{PIPE_FORK}#{pid}:#{idx}\n" rescue nil
|
|
99
95
|
end
|
|
100
96
|
end
|
|
101
97
|
end
|
|
102
98
|
end
|
|
103
99
|
|
|
100
|
+
mold = false
|
|
101
|
+
|
|
102
|
+
if @options[:mold_worker]
|
|
103
|
+
Signal.trap "SIGURG" do
|
|
104
|
+
mold = true
|
|
105
|
+
restart_server.clear
|
|
106
|
+
@server.begin_restart
|
|
107
|
+
restart_server << false
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
104
111
|
Signal.trap "SIGTERM" do
|
|
105
112
|
@worker_write << "#{PIPE_EXTERNAL_TERM}#{Process.pid}\n" rescue nil
|
|
106
113
|
restart_server.clear
|
|
@@ -117,38 +124,24 @@ module Puma
|
|
|
117
124
|
|
|
118
125
|
while restart_server.pop
|
|
119
126
|
server_thread = @server.run
|
|
127
|
+
# A promotion received before the server was running had no effect.
|
|
128
|
+
@server.begin_restart if mold
|
|
120
129
|
|
|
121
130
|
if @log_writer.debug? && index == 0
|
|
122
131
|
debug_loaded_extensions "Loaded Extensions - worker 0:"
|
|
123
132
|
end
|
|
124
133
|
|
|
125
|
-
|
|
126
|
-
Puma.set_thread_name "stat pld"
|
|
127
|
-
base_payload = "#{PIPE_PING}#{Process.pid}"
|
|
128
|
-
|
|
129
|
-
while true
|
|
130
|
-
begin
|
|
131
|
-
payload = base_payload.dup
|
|
132
|
-
|
|
133
|
-
hsh = @server.stats
|
|
134
|
-
hsh.each do |k, v|
|
|
135
|
-
payload << %Q! "#{k}":#{v || 0},!
|
|
136
|
-
end
|
|
137
|
-
# sub call properly adds 'closing' string
|
|
138
|
-
io << payload.sub(/,\z/, " }\n")
|
|
139
|
-
@server.reset_max
|
|
140
|
-
rescue IOError
|
|
141
|
-
break
|
|
142
|
-
end
|
|
143
|
-
sleep @options[:worker_check_interval]
|
|
144
|
-
end
|
|
145
|
-
end
|
|
134
|
+
start_stat_thread
|
|
146
135
|
server_thread.join
|
|
147
136
|
end
|
|
148
137
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
138
|
+
if mold
|
|
139
|
+
run_mold
|
|
140
|
+
else
|
|
141
|
+
# Invoke any worker shutdown hooks so they can prevent the worker
|
|
142
|
+
# exiting until any background operations are completed
|
|
143
|
+
@config.run_hooks(:before_worker_shutdown, index, @log_writer, @hook_data)
|
|
144
|
+
end
|
|
152
145
|
ensure
|
|
153
146
|
@worker_write << "#{PIPE_TERM}#{Process.pid}\n" rescue nil
|
|
154
147
|
@worker_write.close
|
|
@@ -156,15 +149,97 @@ module Puma
|
|
|
156
149
|
|
|
157
150
|
private
|
|
158
151
|
|
|
152
|
+
def start_stat_thread
|
|
153
|
+
@stat_thread ||= Thread.new(@worker_write) do |io|
|
|
154
|
+
Puma.set_thread_name "stat pld"
|
|
155
|
+
base_payload = "#{PIPE_PING}#{Process.pid}"
|
|
156
|
+
|
|
157
|
+
while true
|
|
158
|
+
begin
|
|
159
|
+
payload = base_payload.dup
|
|
160
|
+
|
|
161
|
+
hsh = @server.stats
|
|
162
|
+
hsh.each do |k, v|
|
|
163
|
+
payload << %Q! "#{k}":#{v || 0},!
|
|
164
|
+
end
|
|
165
|
+
# sub call properly adds 'closing' string
|
|
166
|
+
io << payload.sub(/,\z/, " }\n")
|
|
167
|
+
@server.reset_max
|
|
168
|
+
rescue SystemCallError, IOError
|
|
169
|
+
break
|
|
170
|
+
end
|
|
171
|
+
sleep @options[:worker_check_interval]
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def run_mold
|
|
177
|
+
$0 = $0.sub("cluster worker", "mold")
|
|
178
|
+
|
|
179
|
+
retired = false
|
|
180
|
+
stopping = false
|
|
181
|
+
stop_read, stop_write = @mold_stop_pipe = Puma::Util.pipe
|
|
182
|
+
Signal.trap "SIGURG" do
|
|
183
|
+
retired = true
|
|
184
|
+
stop_write.write_nonblock(".", exception: false)
|
|
185
|
+
end
|
|
186
|
+
Signal.trap "SIGTERM" do
|
|
187
|
+
@worker_write << "#{PIPE_EXTERNAL_TERM}#{Process.pid}\n" rescue nil
|
|
188
|
+
stopping = true
|
|
189
|
+
stop_write.write_nonblock(".", exception: false)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
trap_sigchld
|
|
193
|
+
|
|
194
|
+
@config.run_hooks(:after_mold_promotion, index, @log_writer, @hook_data)
|
|
195
|
+
start_stat_thread
|
|
196
|
+
@worker_write << "#{PIPE_MOLD}#{Process.pid}\n"
|
|
197
|
+
|
|
198
|
+
loop do
|
|
199
|
+
IO.select([@fork_pipe, stop_read])
|
|
200
|
+
# A retired mold must not read indices meant for its replacement.
|
|
201
|
+
break if retired || stopping
|
|
202
|
+
|
|
203
|
+
idx = @fork_pipe.sysread(MOLD_FORK_BYTES).unpack1(MOLD_FORK_FORMAT)
|
|
204
|
+
pid = spawn_worker(idx)
|
|
205
|
+
@worker_write << "#{PIPE_FORK}#{pid}:#{idx}\n"
|
|
206
|
+
rescue EOFError
|
|
207
|
+
break
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
if retired
|
|
211
|
+
# Stay alive until stopped, so the workers this mold forked are
|
|
212
|
+
# reaped and the master is woken as they exit.
|
|
213
|
+
stop_read.read(1) until stopping
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
@config.run_hooks(:before_mold_shutdown, index, @log_writer, @hook_data)
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def trap_sigchld
|
|
220
|
+
Signal.trap "SIGCHLD" do
|
|
221
|
+
wakeup! if @worker_pids.reject! do |p|
|
|
222
|
+
Process.wait(p, Process::WNOHANG) rescue true
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
159
227
|
def spawn_worker(idx)
|
|
160
228
|
@config.run_hooks(:before_worker_fork, idx, @log_writer, @hook_data)
|
|
161
229
|
|
|
230
|
+
pipes = { check_pipe: @check_pipe, worker_write: @worker_write }
|
|
231
|
+
if @options[:mold_worker]
|
|
232
|
+
pipes[:fork_pipe] = @fork_pipe
|
|
233
|
+
pipes[:wakeup] = @wakeup
|
|
234
|
+
end
|
|
235
|
+
|
|
162
236
|
pid = fork do
|
|
237
|
+
@mold_stop_pipe&.each(&:close)
|
|
238
|
+
|
|
163
239
|
new_worker = Worker.new index: idx,
|
|
164
240
|
master: master,
|
|
165
241
|
launcher: @launcher,
|
|
166
|
-
pipes:
|
|
167
|
-
worker_write: @worker_write },
|
|
242
|
+
pipes: pipes,
|
|
168
243
|
app: @app
|
|
169
244
|
new_worker.run
|
|
170
245
|
end
|
|
@@ -175,6 +250,7 @@ module Puma
|
|
|
175
250
|
exit! 1
|
|
176
251
|
end
|
|
177
252
|
|
|
253
|
+
@worker_pids << pid
|
|
178
254
|
@config.run_hooks(:after_worker_fork, idx, @log_writer, @hook_data)
|
|
179
255
|
pid
|
|
180
256
|
end
|
|
@@ -25,6 +25,7 @@ module Puma
|
|
|
25
25
|
@last_checkin = Time.now
|
|
26
26
|
@last_status = {}
|
|
27
27
|
@term = false
|
|
28
|
+
@mold = false
|
|
28
29
|
@worker_max = Array.new WORKER_MAX_KEYS.length, 0
|
|
29
30
|
end
|
|
30
31
|
|
|
@@ -54,6 +55,21 @@ module Puma
|
|
|
54
55
|
@term
|
|
55
56
|
end
|
|
56
57
|
|
|
58
|
+
def mold?
|
|
59
|
+
@mold
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def mold!
|
|
63
|
+
@mold = true
|
|
64
|
+
Process.kill "URG", @pid
|
|
65
|
+
rescue Errno::ESRCH
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def retire!
|
|
69
|
+
Process.kill "URG", @pid
|
|
70
|
+
rescue Errno::ESRCH
|
|
71
|
+
end
|
|
72
|
+
|
|
57
73
|
def ping!(status)
|
|
58
74
|
hsh = {}
|
|
59
75
|
k, v = nil, nil
|