puma 4.3.3 → 5.3.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of puma might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/History.md +1348 -519
- data/LICENSE +23 -20
- data/README.md +74 -31
- data/bin/puma-wild +3 -9
- data/docs/architecture.md +24 -20
- data/docs/compile_options.md +19 -0
- data/docs/deployment.md +15 -10
- data/docs/fork_worker.md +33 -0
- data/docs/jungle/README.md +9 -0
- data/{tools → docs}/jungle/rc.d/README.md +1 -1
- data/{tools → docs}/jungle/rc.d/puma +2 -2
- data/{tools → docs}/jungle/rc.d/puma.conf +0 -0
- data/docs/kubernetes.md +66 -0
- data/docs/nginx.md +1 -1
- data/docs/plugins.md +2 -2
- data/docs/rails_dev_mode.md +29 -0
- data/docs/restart.md +46 -23
- data/docs/signals.md +7 -6
- data/docs/stats.md +142 -0
- data/docs/systemd.md +27 -67
- data/ext/puma_http11/PumaHttp11Service.java +2 -4
- data/ext/puma_http11/ext_help.h +1 -1
- data/ext/puma_http11/extconf.rb +22 -8
- data/ext/puma_http11/http11_parser.c +48 -48
- data/ext/puma_http11/http11_parser.h +1 -1
- data/ext/puma_http11/http11_parser.java.rl +1 -1
- data/ext/puma_http11/http11_parser.rl +4 -2
- data/ext/puma_http11/mini_ssl.c +211 -118
- data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
- data/ext/puma_http11/org/jruby/puma/Http11.java +3 -3
- data/ext/puma_http11/org/jruby/puma/Http11Parser.java +5 -7
- data/ext/puma_http11/org/jruby/puma/MiniSSL.java +77 -18
- data/ext/puma_http11/puma_http11.c +32 -50
- data/lib/puma.rb +46 -0
- data/lib/puma/app/status.rb +48 -35
- data/lib/puma/binder.rb +177 -103
- data/lib/puma/cli.rb +11 -15
- data/lib/puma/client.rb +83 -76
- data/lib/puma/cluster.rb +184 -198
- data/lib/puma/cluster/worker.rb +183 -0
- data/lib/puma/cluster/worker_handle.rb +90 -0
- data/lib/puma/commonlogger.rb +2 -2
- data/lib/puma/configuration.rb +55 -49
- data/lib/puma/const.rb +13 -5
- data/lib/puma/control_cli.rb +93 -76
- data/lib/puma/detect.rb +24 -3
- data/lib/puma/dsl.rb +266 -92
- data/lib/puma/error_logger.rb +104 -0
- data/lib/puma/events.rb +55 -34
- data/lib/puma/io_buffer.rb +9 -2
- data/lib/puma/jruby_restart.rb +0 -58
- data/lib/puma/json.rb +96 -0
- data/lib/puma/launcher.rb +113 -45
- data/lib/puma/minissl.rb +114 -33
- data/lib/puma/minissl/context_builder.rb +6 -3
- data/lib/puma/null_io.rb +13 -1
- data/lib/puma/plugin.rb +1 -10
- data/lib/puma/queue_close.rb +26 -0
- data/lib/puma/rack/builder.rb +0 -4
- data/lib/puma/reactor.rb +85 -369
- data/lib/puma/request.rb +467 -0
- data/lib/puma/runner.rb +29 -58
- data/lib/puma/server.rb +267 -698
- data/lib/puma/single.rb +9 -65
- data/lib/puma/state_file.rb +8 -3
- data/lib/puma/systemd.rb +46 -0
- data/lib/puma/thread_pool.rb +119 -53
- data/lib/puma/util.rb +12 -0
- data/lib/rack/handler/puma.rb +2 -3
- data/tools/{docker/Dockerfile → Dockerfile} +0 -0
- metadata +28 -24
- data/docs/tcp_mode.md +0 -96
- data/ext/puma_http11/io_buffer.c +0 -155
- data/ext/puma_http11/org/jruby/puma/IOBuffer.java +0 -72
- data/lib/puma/accept_nonblock.rb +0 -29
- data/lib/puma/tcp_logger.rb +0 -41
- data/tools/jungle/README.md +0 -19
- data/tools/jungle/init.d/README.md +0 -61
- data/tools/jungle/init.d/puma +0 -421
- data/tools/jungle/init.d/run-puma +0 -18
- data/tools/jungle/upstart/README.md +0 -61
- data/tools/jungle/upstart/puma-manager.conf +0 -31
- data/tools/jungle/upstart/puma.conf +0 -69
data/lib/puma/minissl.rb
CHANGED
@@ -5,8 +5,18 @@ begin
|
|
5
5
|
rescue LoadError
|
6
6
|
end
|
7
7
|
|
8
|
+
# need for Puma::MiniSSL::OPENSSL constants used in `HAS_TLS1_3`
|
9
|
+
require 'puma/puma_http11'
|
10
|
+
|
8
11
|
module Puma
|
9
12
|
module MiniSSL
|
13
|
+
# Define constant at runtime, as it's easy to determine at built time,
|
14
|
+
# but Puma could (it shouldn't) be loaded with an older OpenSSL version
|
15
|
+
# @version 5.0.0
|
16
|
+
HAS_TLS1_3 = !IS_JRUBY &&
|
17
|
+
(OPENSSL_VERSION[/ \d+\.\d+\.\d+/].split('.').map(&:to_i) <=> [1,1,1]) != -1 &&
|
18
|
+
(OPENSSL_LIBRARY_VERSION[/ \d+\.\d+\.\d+/].split('.').map(&:to_i) <=> [1,1,1]) !=-1
|
19
|
+
|
10
20
|
class Socket
|
11
21
|
def initialize(socket, engine)
|
12
22
|
@socket = socket
|
@@ -14,6 +24,7 @@ module Puma
|
|
14
24
|
@peercert = nil
|
15
25
|
end
|
16
26
|
|
27
|
+
# @!attribute [r] to_io
|
17
28
|
def to_io
|
18
29
|
@socket
|
19
30
|
end
|
@@ -22,6 +33,27 @@ module Puma
|
|
22
33
|
@socket.closed?
|
23
34
|
end
|
24
35
|
|
36
|
+
# Returns a two element array,
|
37
|
+
# first is protocol version (SSL_get_version),
|
38
|
+
# second is 'handshake' state (SSL_state_string)
|
39
|
+
#
|
40
|
+
# Used for dropping tcp connections to ssl.
|
41
|
+
# See OpenSSL ssl/ssl_stat.c SSL_state_string for info
|
42
|
+
# @!attribute [r] ssl_version_state
|
43
|
+
# @version 5.0.0
|
44
|
+
#
|
45
|
+
def ssl_version_state
|
46
|
+
IS_JRUBY ? [nil, nil] : @engine.ssl_vers_st
|
47
|
+
end
|
48
|
+
|
49
|
+
# Used to check the handshake status, in particular when a TCP connection
|
50
|
+
# is made with TLSv1.3 as an available protocol
|
51
|
+
# @version 5.0.0
|
52
|
+
def bad_tlsv1_3?
|
53
|
+
HAS_TLS1_3 && @engine.ssl_vers_st == ['TLSv1.3', 'SSLERR']
|
54
|
+
end
|
55
|
+
private :bad_tlsv1_3?
|
56
|
+
|
25
57
|
def readpartial(size)
|
26
58
|
while true
|
27
59
|
output = @engine.read
|
@@ -67,6 +99,7 @@ module Puma
|
|
67
99
|
# ourselves.
|
68
100
|
raise IO::EAGAINWaitReadable
|
69
101
|
elsif data.nil?
|
102
|
+
raise SSLError.exception "HTTP connection?" if bad_tlsv1_3?
|
70
103
|
return nil
|
71
104
|
end
|
72
105
|
|
@@ -84,22 +117,23 @@ module Puma
|
|
84
117
|
def write(data)
|
85
118
|
return 0 if data.empty?
|
86
119
|
|
87
|
-
|
120
|
+
data_size = data.bytesize
|
121
|
+
need = data_size
|
88
122
|
|
89
123
|
while true
|
90
124
|
wrote = @engine.write data
|
91
|
-
enc = @engine.extract
|
92
125
|
|
93
|
-
|
94
|
-
|
95
|
-
|
126
|
+
enc_wr = ''.dup
|
127
|
+
while (enc = @engine.extract)
|
128
|
+
enc_wr << enc
|
96
129
|
end
|
130
|
+
@socket.write enc_wr unless enc_wr.empty?
|
97
131
|
|
98
132
|
need -= wrote
|
99
133
|
|
100
|
-
return
|
134
|
+
return data_size if need == 0
|
101
135
|
|
102
|
-
data = data
|
136
|
+
data = data.byteslice(wrote..-1)
|
103
137
|
end
|
104
138
|
end
|
105
139
|
|
@@ -107,14 +141,18 @@ module Puma
|
|
107
141
|
alias_method :<<, :write
|
108
142
|
|
109
143
|
# This is a temporary fix to deal with websockets code using
|
110
|
-
# write_nonblock.
|
144
|
+
# write_nonblock.
|
145
|
+
|
146
|
+
# The problem with implementing it properly
|
111
147
|
# is that it means we'd have to have the ability to rewind
|
112
148
|
# an engine because after we write+extract, the socket
|
113
149
|
# write_nonblock call might raise an exception and later
|
114
150
|
# code would pass the same data in, but the engine would think
|
115
|
-
# it had already written the data in.
|
116
|
-
#
|
117
|
-
#
|
151
|
+
# it had already written the data in.
|
152
|
+
#
|
153
|
+
# So for the time being (and since write blocking is quite rare),
|
154
|
+
# go ahead and actually block in write_nonblock.
|
155
|
+
#
|
118
156
|
def write_nonblock(data, *_)
|
119
157
|
write data
|
120
158
|
end
|
@@ -125,11 +163,14 @@ module Puma
|
|
125
163
|
|
126
164
|
def read_and_drop(timeout = 1)
|
127
165
|
return :timeout unless IO.select([@socket], nil, nil, timeout)
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
166
|
+
case @socket.read_nonblock(1024, exception: false)
|
167
|
+
when nil
|
168
|
+
:eof
|
169
|
+
when :wait_readable
|
170
|
+
:eagain
|
171
|
+
else
|
172
|
+
:drop
|
173
|
+
end
|
133
174
|
end
|
134
175
|
|
135
176
|
def should_drop_bytes?
|
@@ -141,9 +182,7 @@ module Puma
|
|
141
182
|
# Read any drop any partially initialized sockets and any received bytes during shutdown.
|
142
183
|
# Don't let this socket hold this loop forever.
|
143
184
|
# If it can't send more packets within 1s, then give up.
|
144
|
-
while should_drop_bytes?
|
145
|
-
return if [:timeout, :eof].include?(read_and_drop(1))
|
146
|
-
end
|
185
|
+
return if [:timeout, :eof].include?(read_and_drop(1)) while should_drop_bytes?
|
147
186
|
rescue IOError, SystemCallError
|
148
187
|
Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
|
149
188
|
# nothing
|
@@ -152,10 +191,12 @@ module Puma
|
|
152
191
|
end
|
153
192
|
end
|
154
193
|
|
194
|
+
# @!attribute [r] peeraddr
|
155
195
|
def peeraddr
|
156
196
|
@socket.peeraddr
|
157
197
|
end
|
158
198
|
|
199
|
+
# @!attribute [r] peercert
|
159
200
|
def peercert
|
160
201
|
return @peercert if @peercert
|
161
202
|
|
@@ -166,12 +207,13 @@ module Puma
|
|
166
207
|
end
|
167
208
|
end
|
168
209
|
|
169
|
-
if
|
210
|
+
if IS_JRUBY
|
211
|
+
OPENSSL_NO_SSL3 = false
|
212
|
+
OPENSSL_NO_TLS1 = false
|
213
|
+
|
170
214
|
class SSLError < StandardError
|
171
215
|
# Define this for jruby even though it isn't used.
|
172
216
|
end
|
173
|
-
|
174
|
-
def self.check; end
|
175
217
|
end
|
176
218
|
|
177
219
|
class Context
|
@@ -183,7 +225,7 @@ module Puma
|
|
183
225
|
@no_tlsv1_1 = false
|
184
226
|
end
|
185
227
|
|
186
|
-
if
|
228
|
+
if IS_JRUBY
|
187
229
|
# jruby-specific Context properties: java uses a keystore and password pair rather than a cert/key pair
|
188
230
|
attr_reader :keystore
|
189
231
|
attr_accessor :keystore_pass
|
@@ -204,6 +246,7 @@ module Puma
|
|
204
246
|
attr_reader :cert
|
205
247
|
attr_reader :ca
|
206
248
|
attr_accessor :ssl_cipher_filter
|
249
|
+
attr_accessor :verification_flags
|
207
250
|
|
208
251
|
def key=(key)
|
209
252
|
raise ArgumentError, "No such key file '#{key}'" unless File.exist? key
|
@@ -227,14 +270,16 @@ module Puma
|
|
227
270
|
end
|
228
271
|
|
229
272
|
# disables TLSv1
|
273
|
+
# @!attribute [w] no_tlsv1=
|
230
274
|
def no_tlsv1=(tlsv1)
|
231
|
-
raise ArgumentError, "Invalid value of no_tlsv1" unless ['true', 'false', true, false].include?(tlsv1)
|
275
|
+
raise ArgumentError, "Invalid value of no_tlsv1=" unless ['true', 'false', true, false].include?(tlsv1)
|
232
276
|
@no_tlsv1 = tlsv1
|
233
277
|
end
|
234
278
|
|
235
279
|
# disables TLSv1 and TLSv1.1. Overrides `#no_tlsv1=`
|
280
|
+
# @!attribute [w] no_tlsv1_1=
|
236
281
|
def no_tlsv1_1=(tlsv1_1)
|
237
|
-
raise ArgumentError, "Invalid value of
|
282
|
+
raise ArgumentError, "Invalid value of no_tlsv1_1=" unless ['true', 'false', true, false].include?(tlsv1_1)
|
238
283
|
@no_tlsv1_1 = tlsv1_1
|
239
284
|
end
|
240
285
|
|
@@ -244,35 +289,71 @@ module Puma
|
|
244
289
|
VERIFY_PEER = 1
|
245
290
|
VERIFY_FAIL_IF_NO_PEER_CERT = 2
|
246
291
|
|
292
|
+
# https://github.com/openssl/openssl/blob/master/include/openssl/x509_vfy.h.in
|
293
|
+
# /* Certificate verify flags */
|
294
|
+
VERIFICATION_FLAGS = {
|
295
|
+
"USE_CHECK_TIME" => 0x2,
|
296
|
+
"CRL_CHECK" => 0x4,
|
297
|
+
"CRL_CHECK_ALL" => 0x8,
|
298
|
+
"IGNORE_CRITICAL" => 0x10,
|
299
|
+
"X509_STRICT" => 0x20,
|
300
|
+
"ALLOW_PROXY_CERTS" => 0x40,
|
301
|
+
"POLICY_CHECK" => 0x80,
|
302
|
+
"EXPLICIT_POLICY" => 0x100,
|
303
|
+
"INHIBIT_ANY" => 0x200,
|
304
|
+
"INHIBIT_MAP" => 0x400,
|
305
|
+
"NOTIFY_POLICY" => 0x800,
|
306
|
+
"EXTENDED_CRL_SUPPORT" => 0x1000,
|
307
|
+
"USE_DELTAS" => 0x2000,
|
308
|
+
"CHECK_SS_SIGNATURE" => 0x4000,
|
309
|
+
"TRUSTED_FIRST" => 0x8000,
|
310
|
+
"SUITEB_128_LOS_ONLY" => 0x10000,
|
311
|
+
"SUITEB_192_LOS" => 0x20000,
|
312
|
+
"SUITEB_128_LOS" => 0x30000,
|
313
|
+
"PARTIAL_CHAIN" => 0x80000,
|
314
|
+
"NO_ALT_CHAINS" => 0x100000,
|
315
|
+
"NO_CHECK_TIME" => 0x200000
|
316
|
+
}.freeze
|
317
|
+
|
247
318
|
class Server
|
248
319
|
def initialize(socket, ctx)
|
249
320
|
@socket = socket
|
250
321
|
@ctx = ctx
|
251
|
-
|
252
|
-
|
253
|
-
def to_io
|
254
|
-
@socket
|
322
|
+
@eng_ctx = IS_JRUBY ? @ctx : SSLContext.new(ctx)
|
255
323
|
end
|
256
324
|
|
257
325
|
def accept
|
258
326
|
@ctx.check
|
259
327
|
io = @socket.accept
|
260
|
-
engine = Engine.server @
|
261
|
-
|
328
|
+
engine = Engine.server @eng_ctx
|
262
329
|
Socket.new io, engine
|
263
330
|
end
|
264
331
|
|
265
332
|
def accept_nonblock
|
266
333
|
@ctx.check
|
267
334
|
io = @socket.accept_nonblock
|
268
|
-
engine = Engine.server @
|
269
|
-
|
335
|
+
engine = Engine.server @eng_ctx
|
270
336
|
Socket.new io, engine
|
271
337
|
end
|
272
338
|
|
339
|
+
# @!attribute [r] to_io
|
340
|
+
def to_io
|
341
|
+
@socket
|
342
|
+
end
|
343
|
+
|
344
|
+
# @!attribute [r] addr
|
345
|
+
# @version 5.0.0
|
346
|
+
def addr
|
347
|
+
@socket.addr
|
348
|
+
end
|
349
|
+
|
273
350
|
def close
|
274
351
|
@socket.close unless @socket.closed? # closed? call is for Windows
|
275
352
|
end
|
353
|
+
|
354
|
+
def closed?
|
355
|
+
@socket.closed?
|
356
|
+
end
|
276
357
|
end
|
277
358
|
end
|
278
359
|
end
|
@@ -2,9 +2,6 @@ module Puma
|
|
2
2
|
module MiniSSL
|
3
3
|
class ContextBuilder
|
4
4
|
def initialize(params, events)
|
5
|
-
require 'puma/minissl'
|
6
|
-
MiniSSL.check
|
7
|
-
|
8
5
|
@params = params
|
9
6
|
@events = events
|
10
7
|
end
|
@@ -65,6 +62,12 @@ module Puma
|
|
65
62
|
end
|
66
63
|
end
|
67
64
|
|
65
|
+
if params['verification_flags']
|
66
|
+
ctx.verification_flags = params['verification_flags'].split(',').
|
67
|
+
map { |flag| MiniSSL::VERIFICATION_FLAGS.fetch(flag) }.
|
68
|
+
inject { |sum, flag| sum ? sum | flag : flag }
|
69
|
+
end
|
70
|
+
|
68
71
|
ctx
|
69
72
|
end
|
70
73
|
|
data/lib/puma/null_io.rb
CHANGED
@@ -9,13 +9,17 @@ module Puma
|
|
9
9
|
nil
|
10
10
|
end
|
11
11
|
|
12
|
+
def string
|
13
|
+
""
|
14
|
+
end
|
15
|
+
|
12
16
|
def each
|
13
17
|
end
|
14
18
|
|
15
19
|
# Mimics IO#read with no data.
|
16
20
|
#
|
17
21
|
def read(count = nil, _buffer = nil)
|
18
|
-
|
22
|
+
count && count > 0 ? nil : ""
|
19
23
|
end
|
20
24
|
|
21
25
|
def rewind
|
@@ -32,6 +36,10 @@ module Puma
|
|
32
36
|
true
|
33
37
|
end
|
34
38
|
|
39
|
+
def sync
|
40
|
+
true
|
41
|
+
end
|
42
|
+
|
35
43
|
def sync=(v)
|
36
44
|
end
|
37
45
|
|
@@ -40,5 +48,9 @@ module Puma
|
|
40
48
|
|
41
49
|
def write(*ary)
|
42
50
|
end
|
51
|
+
|
52
|
+
def flush
|
53
|
+
self
|
54
|
+
end
|
43
55
|
end
|
44
56
|
end
|
data/lib/puma/plugin.rb
CHANGED
@@ -10,7 +10,7 @@ module Puma
|
|
10
10
|
|
11
11
|
def create(name)
|
12
12
|
if cls = Plugins.find(name)
|
13
|
-
plugin = cls.new
|
13
|
+
plugin = cls.new
|
14
14
|
@instances << plugin
|
15
15
|
return plugin
|
16
16
|
end
|
@@ -104,17 +104,8 @@ module Puma
|
|
104
104
|
Plugins.register name, cls
|
105
105
|
end
|
106
106
|
|
107
|
-
def initialize(loader)
|
108
|
-
@loader = loader
|
109
|
-
end
|
110
|
-
|
111
107
|
def in_background(&blk)
|
112
108
|
Plugins.add_background blk
|
113
109
|
end
|
114
|
-
|
115
|
-
def workers_supported?
|
116
|
-
return false if Puma.jruby? || Puma.windows?
|
117
|
-
true
|
118
|
-
end
|
119
110
|
end
|
120
111
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class ClosedQueueError < StandardError; end
|
2
|
+
module Puma
|
3
|
+
|
4
|
+
# Queue#close was added in Ruby 2.3.
|
5
|
+
# Add a simple implementation for earlier Ruby versions.
|
6
|
+
#
|
7
|
+
module QueueClose
|
8
|
+
def close
|
9
|
+
num_waiting.times {push nil}
|
10
|
+
@closed = true
|
11
|
+
end
|
12
|
+
def closed?
|
13
|
+
@closed ||= false
|
14
|
+
end
|
15
|
+
def push(object)
|
16
|
+
raise ClosedQueueError if closed?
|
17
|
+
super
|
18
|
+
end
|
19
|
+
alias << push
|
20
|
+
def pop(non_block=false)
|
21
|
+
return nil if !non_block && closed? && empty?
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
::Queue.prepend QueueClose
|
26
|
+
end
|
data/lib/puma/rack/builder.rb
CHANGED
@@ -67,10 +67,6 @@ module Puma::Rack
|
|
67
67
|
options[:environment] = e
|
68
68
|
}
|
69
69
|
|
70
|
-
opts.on("-D", "--daemonize", "run daemonized in the background") { |d|
|
71
|
-
options[:daemonize] = d ? true : false
|
72
|
-
}
|
73
|
-
|
74
70
|
opts.on("-P", "--pid FILE", "file to store PID") { |f|
|
75
71
|
options[:pid] = ::File.expand_path(f)
|
76
72
|
}
|
data/lib/puma/reactor.rb
CHANGED
@@ -1,400 +1,116 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'puma/
|
4
|
-
require 'puma/minissl'
|
5
|
-
|
6
|
-
require 'nio'
|
3
|
+
require 'puma/queue_close' unless ::Queue.instance_methods.include? :close
|
7
4
|
|
8
5
|
module Puma
|
9
|
-
|
10
|
-
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# If read buffering is not done, and no other read buffering is performed (such as by an application server
|
14
|
-
# such as nginx) then the application would be subject to a slow client attack.
|
15
|
-
#
|
16
|
-
# Each Puma "worker" process has its own Reactor. For example if you start puma with `$ puma -w 5` then
|
17
|
-
# it will have 5 workers and each worker will have it's own reactor.
|
18
|
-
#
|
19
|
-
# For a graphical representation of how the reactor works see [architecture.md](https://github.com/puma/puma/blob/master/docs/architecture.md#connection-pipeline).
|
20
|
-
#
|
21
|
-
# ## Reactor Flow
|
22
|
-
#
|
23
|
-
# A connection comes into a `Puma::Server` instance, it is then passed to a `Puma::Reactor` instance,
|
24
|
-
# which stores it in an array and waits for any of the connections to be ready for reading.
|
6
|
+
class UnsupportedBackend < StandardError; end
|
7
|
+
|
8
|
+
# Monitors a collection of IO objects, calling a block whenever
|
9
|
+
# any monitored object either receives data or times out, or when the Reactor shuts down.
|
25
10
|
#
|
26
|
-
# The waiting/wake up is performed with nio4r, which will use the appropriate backend (libev,
|
27
|
-
# just plain IO#select). The call to `NIO::Selector#select` will
|
28
|
-
#
|
29
|
-
# then loops through each of these request objects, and sees if they're complete. If they
|
30
|
-
# have a full header and body then the reactor passes the request to a thread pool.
|
31
|
-
# Once in a thread pool, a "worker thread" can run the the application's Ruby code against the request.
|
11
|
+
# The waiting/wake up is performed with nio4r, which will use the appropriate backend (libev,
|
12
|
+
# Java NIO or just plain IO#select). The call to `NIO::Selector#select` will
|
13
|
+
# 'wakeup' any IO object that receives data.
|
32
14
|
#
|
33
|
-
#
|
34
|
-
#
|
15
|
+
# This class additionally tracks a timeout for every added object,
|
16
|
+
# and wakes up any object when its timeout elapses.
|
35
17
|
#
|
36
|
-
#
|
37
|
-
# of this logic lives.
|
18
|
+
# The implementation uses a Queue to synchronize adding new objects from the internal select loop.
|
38
19
|
class Reactor
|
39
|
-
|
40
|
-
|
41
|
-
#
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
# it will be passed to the `app_pool`.
|
50
|
-
def initialize(server, app_pool)
|
51
|
-
@server = server
|
52
|
-
@events = server.events
|
53
|
-
@app_pool = app_pool
|
54
|
-
|
55
|
-
@selector = NIO::Selector.new
|
56
|
-
|
57
|
-
@mutex = Mutex.new
|
58
|
-
|
59
|
-
# Read / Write pipes to wake up internal while loop
|
60
|
-
@ready, @trigger = Puma::Util.pipe
|
61
|
-
@input = []
|
62
|
-
@sleep_for = DefaultSleepFor
|
20
|
+
# Create a new Reactor to monitor IO objects added by #add.
|
21
|
+
# The provided block will be invoked when an IO has data available to read,
|
22
|
+
# its timeout elapses, or when the Reactor shuts down.
|
23
|
+
def initialize(backend, &block)
|
24
|
+
require 'nio'
|
25
|
+
unless backend == :auto || NIO::Selector.backends.include?(backend)
|
26
|
+
raise "unsupported IO selector backend: #{backend} (available backends: #{NIO::Selector.backends.join(', ')})"
|
27
|
+
end
|
28
|
+
@selector = backend == :auto ? NIO::Selector.new : NIO::Selector.new(backend)
|
29
|
+
@input = Queue.new
|
63
30
|
@timeouts = []
|
64
|
-
|
65
|
-
mon = @selector.register(@ready, :r)
|
66
|
-
mon.value = @ready
|
67
|
-
|
68
|
-
@monitors = [mon]
|
31
|
+
@block = block
|
69
32
|
end
|
70
33
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
# will break on `NIO::Selector#select` and return an array.
|
78
|
-
#
|
79
|
-
# ## When a request is added:
|
80
|
-
#
|
81
|
-
# When the `add` method is called, an instance of `Puma::Client` is added to the `@input` array.
|
82
|
-
# Next the `@ready` pipe is "woken" by writing a string of `"*"` to `@trigger`.
|
83
|
-
#
|
84
|
-
# When that happens, the internal loop stops blocking at `NIO::Selector#select` and returns a reference
|
85
|
-
# to whatever "woke" it up. On the very first loop, the only thing in `sockets` is `@ready`.
|
86
|
-
# When `@trigger` is written-to, the loop "wakes" and the `ready`
|
87
|
-
# variable returns an array of arrays that looks like `[[#<IO:fd 10>], [], []]` where the
|
88
|
-
# first IO object is the `@ready` object. This first array `[#<IO:fd 10>]`
|
89
|
-
# is saved as a `reads` variable.
|
90
|
-
#
|
91
|
-
# The `reads` variable is iterated through. In the case that the object
|
92
|
-
# is the same as the `@ready` input pipe, then we know that there was a `trigger` event.
|
93
|
-
#
|
94
|
-
# If there was a trigger event, then one byte of `@ready` is read into memory. In the case of the first request,
|
95
|
-
# the reactor sees that it's a `"*"` value and the reactor adds the contents of `@input` into the `sockets` array.
|
96
|
-
# The while then loop continues to iterate again, but now the `sockets` array contains a `Puma::Client` instance in addition
|
97
|
-
# to the `@ready` IO object. For example: `[#<IO:fd 10>, #<Puma::Client:0x3fdc1103bee8 @ready=false>]`.
|
98
|
-
#
|
99
|
-
# Since the `Puma::Client` in this example has data that has not been read yet,
|
100
|
-
# the `NIO::Selector#select` is immediately able to "wake" and read from the `Puma::Client`. At this point the
|
101
|
-
# `ready` output looks like this: `[[#<Puma::Client:0x3fdc1103bee8 @ready=false>], [], []]`.
|
102
|
-
#
|
103
|
-
# Each element in the first entry is iterated over. The `Puma::Client` object is not
|
104
|
-
# the `@ready` pipe, so the reactor checks to see if it has the full header and body with
|
105
|
-
# the `Puma::Client#try_to_finish` method. If the full request has been sent,
|
106
|
-
# then the request is passed off to the `@app_pool` thread pool so that a "worker thread"
|
107
|
-
# can pick up the request and begin to execute application logic. This is done
|
108
|
-
# via `@app_pool << c`. The `Puma::Client` is then removed from the `sockets` array.
|
109
|
-
#
|
110
|
-
# If the request body is not present then nothing will happen, and the loop will iterate
|
111
|
-
# again. When the client sends more data to the socket the `Puma::Client` object will
|
112
|
-
# wake up the `NIO::Selector#select` and it can again be checked to see if it's ready to be
|
113
|
-
# passed to the thread pool.
|
114
|
-
#
|
115
|
-
# ## Time Out Case
|
116
|
-
#
|
117
|
-
# In addition to being woken via a write to one of the sockets the `NIO::Selector#select` will
|
118
|
-
# periodically "time out" of the sleep. One of the functions of this is to check for
|
119
|
-
# any requests that have "timed out". At the end of the loop it's checked to see if
|
120
|
-
# the first element in the `@timeout` array has exceed its allowed time. If so,
|
121
|
-
# the client object is removed from the timeout array, a 408 response is written.
|
122
|
-
# Then its connection is closed, and the object is removed from the `sockets` array
|
123
|
-
# that watches for new data.
|
124
|
-
#
|
125
|
-
# This behavior loops until all the objects that have timed out have been removed.
|
126
|
-
#
|
127
|
-
# Once all the timeouts have been processed, the next duration of the `NIO::Selector#select` sleep
|
128
|
-
# will be set to be equal to the amount of time it will take for the next timeout to occur.
|
129
|
-
# This calculation happens in `calculate_sleep`.
|
130
|
-
def run_internal
|
131
|
-
monitors = @monitors
|
132
|
-
selector = @selector
|
133
|
-
|
134
|
-
while true
|
135
|
-
begin
|
136
|
-
ready = selector.select @sleep_for
|
137
|
-
rescue IOError => e
|
138
|
-
Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
|
139
|
-
if monitors.any? { |mon| mon.value.closed? }
|
140
|
-
STDERR.puts "Error in select: #{e.message} (#{e.class})"
|
141
|
-
STDERR.puts e.backtrace
|
142
|
-
|
143
|
-
monitors.reject! do |mon|
|
144
|
-
if mon.value.closed?
|
145
|
-
selector.deregister mon.value
|
146
|
-
true
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
retry
|
151
|
-
else
|
152
|
-
raise
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
if ready
|
157
|
-
ready.each do |mon|
|
158
|
-
if mon.value == @ready
|
159
|
-
@mutex.synchronize do
|
160
|
-
case @ready.read(1)
|
161
|
-
when "*"
|
162
|
-
@input.each do |c|
|
163
|
-
mon = nil
|
164
|
-
begin
|
165
|
-
begin
|
166
|
-
mon = selector.register(c, :r)
|
167
|
-
rescue ArgumentError
|
168
|
-
# There is a bug where we seem to be registering an already registered
|
169
|
-
# client. This code deals with this situation but I wish we didn't have to.
|
170
|
-
monitors.delete_if { |submon| submon.value.to_io == c.to_io }
|
171
|
-
selector.deregister(c)
|
172
|
-
mon = selector.register(c, :r)
|
173
|
-
end
|
174
|
-
rescue IOError
|
175
|
-
# Means that the io is closed, so we should ignore this request
|
176
|
-
# entirely
|
177
|
-
else
|
178
|
-
mon.value = c
|
179
|
-
@timeouts << mon if c.timeout_at
|
180
|
-
monitors << mon
|
181
|
-
end
|
182
|
-
end
|
183
|
-
@input.clear
|
184
|
-
|
185
|
-
@timeouts.sort! { |a,b| a.value.timeout_at <=> b.value.timeout_at }
|
186
|
-
calculate_sleep
|
187
|
-
when "c"
|
188
|
-
monitors.reject! do |submon|
|
189
|
-
if submon.value == @ready
|
190
|
-
false
|
191
|
-
else
|
192
|
-
submon.value.close
|
193
|
-
begin
|
194
|
-
selector.deregister submon.value
|
195
|
-
rescue IOError
|
196
|
-
# nio4r on jruby seems to throw an IOError here if the IO is closed, so
|
197
|
-
# we need to swallow it.
|
198
|
-
end
|
199
|
-
true
|
200
|
-
end
|
201
|
-
end
|
202
|
-
when "!"
|
203
|
-
return
|
204
|
-
end
|
205
|
-
end
|
206
|
-
else
|
207
|
-
c = mon.value
|
208
|
-
|
209
|
-
# We have to be sure to remove it from the timeout
|
210
|
-
# list or we'll accidentally close the socket when
|
211
|
-
# it's in use!
|
212
|
-
if c.timeout_at
|
213
|
-
@mutex.synchronize do
|
214
|
-
@timeouts.delete mon
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
begin
|
219
|
-
if c.try_to_finish
|
220
|
-
@app_pool << c
|
221
|
-
clear_monitor mon
|
222
|
-
end
|
223
|
-
|
224
|
-
# Don't report these to the lowlevel_error handler, otherwise
|
225
|
-
# will be flooding them with errors when persistent connections
|
226
|
-
# are closed.
|
227
|
-
rescue ConnectionError
|
228
|
-
c.write_error(500)
|
229
|
-
c.close
|
230
|
-
|
231
|
-
clear_monitor mon
|
232
|
-
|
233
|
-
# SSL handshake failure
|
234
|
-
rescue MiniSSL::SSLError => e
|
235
|
-
@server.lowlevel_error(e, c.env)
|
236
|
-
|
237
|
-
ssl_socket = c.io
|
238
|
-
begin
|
239
|
-
addr = ssl_socket.peeraddr.last
|
240
|
-
# EINVAL can happen when browser closes socket w/security exception
|
241
|
-
rescue IOError, Errno::EINVAL
|
242
|
-
addr = "<unknown>"
|
243
|
-
end
|
244
|
-
|
245
|
-
cert = ssl_socket.peercert
|
246
|
-
|
247
|
-
c.close
|
248
|
-
clear_monitor mon
|
249
|
-
|
250
|
-
@events.ssl_error @server, addr, cert, e
|
251
|
-
|
252
|
-
# The client doesn't know HTTP well
|
253
|
-
rescue HttpParserError => e
|
254
|
-
@server.lowlevel_error(e, c.env)
|
255
|
-
|
256
|
-
c.write_error(400)
|
257
|
-
c.close
|
258
|
-
|
259
|
-
clear_monitor mon
|
260
|
-
|
261
|
-
@events.parse_error @server, c.env, e
|
262
|
-
rescue StandardError => e
|
263
|
-
@server.lowlevel_error(e, c.env)
|
264
|
-
|
265
|
-
c.write_error(500)
|
266
|
-
c.close
|
267
|
-
|
268
|
-
clear_monitor mon
|
269
|
-
end
|
270
|
-
end
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
unless @timeouts.empty?
|
275
|
-
@mutex.synchronize do
|
276
|
-
now = Time.now
|
277
|
-
|
278
|
-
while @timeouts.first.value.timeout_at < now
|
279
|
-
mon = @timeouts.shift
|
280
|
-
c = mon.value
|
281
|
-
c.write_error(408) if c.in_data_phase
|
282
|
-
c.close
|
283
|
-
|
284
|
-
clear_monitor mon
|
285
|
-
|
286
|
-
break if @timeouts.empty?
|
287
|
-
end
|
288
|
-
|
289
|
-
calculate_sleep
|
290
|
-
end
|
34
|
+
# Run the internal select loop, using a background thread by default.
|
35
|
+
def run(background=true)
|
36
|
+
if background
|
37
|
+
@thread = Thread.new do
|
38
|
+
Puma.set_thread_name "reactor"
|
39
|
+
select_loop
|
291
40
|
end
|
41
|
+
else
|
42
|
+
select_loop
|
292
43
|
end
|
293
44
|
end
|
294
45
|
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
ensure
|
305
|
-
@trigger.close
|
306
|
-
@ready.close
|
46
|
+
# Add a new client to monitor.
|
47
|
+
# The object must respond to #timeout and #timeout_at.
|
48
|
+
# Returns false if the reactor is already shut down.
|
49
|
+
def add(client)
|
50
|
+
@input << client
|
51
|
+
@selector.wakeup
|
52
|
+
true
|
53
|
+
rescue ClosedQueueError
|
54
|
+
false
|
307
55
|
end
|
308
56
|
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
STDERR.puts "Error in reactor loop escaped: #{e.message} (#{e.class})"
|
316
|
-
STDERR.puts e.backtrace
|
317
|
-
retry
|
318
|
-
ensure
|
319
|
-
@trigger.close
|
320
|
-
@ready.close
|
321
|
-
end
|
57
|
+
# Shutdown the reactor, blocking until the background thread is finished.
|
58
|
+
def shutdown
|
59
|
+
@input.close
|
60
|
+
begin
|
61
|
+
@selector.wakeup
|
62
|
+
rescue IOError # Ignore if selector is already closed
|
322
63
|
end
|
64
|
+
@thread.join if @thread
|
323
65
|
end
|
324
66
|
|
325
|
-
|
326
|
-
# sleep for in the main reactor loop when no sockets are being written to.
|
327
|
-
#
|
328
|
-
# The values kept in `@timeouts` are sorted so that the first timeout
|
329
|
-
# comes first in the array. When there are no timeouts the default timeout is used.
|
330
|
-
#
|
331
|
-
# Otherwise a sleep value is set that is the same as the amount of time it
|
332
|
-
# would take for the first element to time out.
|
333
|
-
#
|
334
|
-
# If that value is in the past, then a sleep value of zero is used.
|
335
|
-
def calculate_sleep
|
336
|
-
if @timeouts.empty?
|
337
|
-
@sleep_for = DefaultSleepFor
|
338
|
-
else
|
339
|
-
diff = @timeouts.first.value.timeout_at.to_f - Time.now.to_f
|
67
|
+
private
|
340
68
|
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
69
|
+
def select_loop
|
70
|
+
begin
|
71
|
+
until @input.closed? && @input.empty?
|
72
|
+
# Wakeup any registered object that receives incoming data.
|
73
|
+
# Block until the earliest timeout or Selector#wakeup is called.
|
74
|
+
timeout = (earliest = @timeouts.first) && earliest.timeout
|
75
|
+
@selector.select(timeout) {|mon| wakeup!(mon.value)}
|
76
|
+
|
77
|
+
# Wakeup all objects that timed out.
|
78
|
+
timed_out = @timeouts.take_while {|t| t.timeout == 0}
|
79
|
+
timed_out.each(&method(:wakeup!))
|
80
|
+
|
81
|
+
unless @input.empty?
|
82
|
+
until @input.empty?
|
83
|
+
client = @input.pop
|
84
|
+
register(client) if client.io_ok?
|
85
|
+
end
|
86
|
+
@timeouts.sort_by!(&:timeout_at)
|
87
|
+
end
|
345
88
|
end
|
89
|
+
rescue StandardError => e
|
90
|
+
STDERR.puts "Error in reactor loop escaped: #{e.message} (#{e.class})"
|
91
|
+
STDERR.puts e.backtrace
|
92
|
+
retry
|
346
93
|
end
|
94
|
+
# Wakeup all remaining objects on shutdown.
|
95
|
+
@timeouts.each(&@block)
|
96
|
+
@selector.close
|
347
97
|
end
|
348
98
|
|
349
|
-
#
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
# The main body of the reactor loop is in `run_internal` and it
|
356
|
-
# will sleep on `NIO::Selector#select`. When a new connection is added to the
|
357
|
-
# reactor it cannot be added directly to the `sockets` array, because
|
358
|
-
# the `NIO::Selector#select` will not be watching for it yet.
|
359
|
-
#
|
360
|
-
# Instead what needs to happen is that `NIO::Selector#select` needs to be woken up,
|
361
|
-
# the contents of `@input` added to the `sockets` array, and then
|
362
|
-
# another call to `NIO::Selector#select` needs to happen. Since the `Puma::Client`
|
363
|
-
# object can be read immediately, it does not block, but instead returns
|
364
|
-
# right away.
|
365
|
-
#
|
366
|
-
# This behavior is accomplished by writing to `@trigger` which wakes up
|
367
|
-
# the `NIO::Selector#select` and then there is logic to detect the value of `*`,
|
368
|
-
# pull the contents from `@input` and add them to the sockets array.
|
369
|
-
#
|
370
|
-
# If the object passed in has a timeout value in `timeout_at` then
|
371
|
-
# it is added to a `@timeouts` array. This array is then re-arranged
|
372
|
-
# so that the first element to timeout will be at the front of the
|
373
|
-
# array. Then a value to sleep for is derived in the call to `calculate_sleep`
|
374
|
-
def add(c)
|
375
|
-
@mutex.synchronize do
|
376
|
-
@input << c
|
377
|
-
@trigger << "*"
|
378
|
-
end
|
379
|
-
end
|
380
|
-
|
381
|
-
# Close all watched sockets and clear them from being watched
|
382
|
-
def clear!
|
383
|
-
begin
|
384
|
-
@trigger << "c"
|
385
|
-
rescue IOError
|
386
|
-
Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
|
387
|
-
end
|
99
|
+
# Start monitoring the object.
|
100
|
+
def register(client)
|
101
|
+
@selector.register(client.to_io, :r).value = client
|
102
|
+
@timeouts << client
|
103
|
+
rescue ArgumentError
|
104
|
+
# unreadable clients raise error when processed by NIO
|
388
105
|
end
|
389
106
|
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
107
|
+
# 'Wake up' a monitored object by calling the provided block.
|
108
|
+
# Stop monitoring the object if the block returns `true`.
|
109
|
+
def wakeup!(client)
|
110
|
+
if @block.call client
|
111
|
+
@selector.deregister client.to_io
|
112
|
+
@timeouts.delete client
|
395
113
|
end
|
396
|
-
|
397
|
-
@thread.join
|
398
114
|
end
|
399
115
|
end
|
400
116
|
end
|