puma 5.0.0.beta2-java → 5.0.4-java
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.
Potentially problematic release.
This version of puma might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/History.md +1194 -570
- data/README.md +12 -5
- data/bin/puma-wild +3 -9
- data/docs/deployment.md +5 -6
- data/docs/jungle/README.md +0 -4
- data/docs/jungle/rc.d/puma +2 -2
- data/docs/nginx.md +1 -1
- data/docs/restart.md +46 -23
- data/docs/signals.md +3 -3
- data/docs/systemd.md +1 -1
- data/ext/puma_http11/ext_help.h +1 -1
- data/ext/puma_http11/mini_ssl.c +42 -37
- data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
- data/ext/puma_http11/org/jruby/puma/MiniSSL.java +40 -12
- data/ext/puma_http11/puma_http11.c +21 -10
- data/lib/puma.rb +15 -0
- data/lib/puma/app/status.rb +44 -43
- data/lib/puma/binder.rb +35 -8
- data/lib/puma/client.rb +32 -73
- data/lib/puma/cluster.rb +32 -191
- data/lib/puma/cluster/worker.rb +170 -0
- data/lib/puma/cluster/worker_handle.rb +83 -0
- data/lib/puma/configuration.rb +9 -7
- data/lib/puma/const.rb +2 -1
- data/lib/puma/control_cli.rb +2 -0
- data/lib/puma/detect.rb +9 -0
- data/lib/puma/dsl.rb +74 -36
- data/lib/puma/error_logger.rb +3 -2
- data/lib/puma/events.rb +7 -3
- data/lib/puma/launcher.rb +15 -8
- data/lib/puma/minissl.rb +28 -15
- data/lib/puma/minissl/context_builder.rb +0 -3
- data/lib/puma/puma_http11.jar +0 -0
- data/lib/puma/queue_close.rb +26 -0
- data/lib/puma/reactor.rb +77 -373
- data/lib/puma/request.rb +438 -0
- data/lib/puma/runner.rb +6 -18
- data/lib/puma/server.rb +192 -509
- data/lib/puma/single.rb +3 -2
- data/lib/puma/thread_pool.rb +27 -3
- data/lib/puma/util.rb +12 -0
- metadata +9 -8
- data/docs/jungle/upstart/README.md +0 -61
- data/docs/jungle/upstart/puma-manager.conf +0 -31
- data/docs/jungle/upstart/puma.conf +0 -69
- data/lib/puma/accept_nonblock.rb +0 -29
data/lib/puma/error_logger.rb
CHANGED
@@ -4,6 +4,7 @@ require 'puma/const'
|
|
4
4
|
|
5
5
|
module Puma
|
6
6
|
# The implementation of a detailed error logging.
|
7
|
+
# @version 5.0.0
|
7
8
|
#
|
8
9
|
class ErrorLogger
|
9
10
|
include Const
|
@@ -50,8 +51,8 @@ module Puma
|
|
50
51
|
|
51
52
|
string_block = []
|
52
53
|
string_block << title(options)
|
53
|
-
string_block << request_dump(req) if req
|
54
|
-
string_block <<
|
54
|
+
string_block << request_dump(req) if request_parsed?(req)
|
55
|
+
string_block << error.backtrace if error
|
55
56
|
|
56
57
|
ioerr.puts string_block.join("\n")
|
57
58
|
end
|
data/lib/puma/events.rb
CHANGED
@@ -91,6 +91,7 @@ module Puma
|
|
91
91
|
# An HTTP connection error has occurred.
|
92
92
|
# +error+ a connection exception, +req+ the request,
|
93
93
|
# and +text+ additional info
|
94
|
+
# @version 5.0.0
|
94
95
|
#
|
95
96
|
def connection_error(error, req, text="HTTP connection error")
|
96
97
|
@error_logger.info(error: error, req: req, text: text)
|
@@ -105,10 +106,12 @@ module Puma
|
|
105
106
|
end
|
106
107
|
|
107
108
|
# An SSL error has occurred.
|
108
|
-
#
|
109
|
-
#
|
109
|
+
# @param error <Puma::MiniSSL::SSLError>
|
110
|
+
# @param ssl_socket <Puma::MiniSSL::Socket>
|
110
111
|
#
|
111
|
-
def ssl_error(error,
|
112
|
+
def ssl_error(error, ssl_socket)
|
113
|
+
peeraddr = ssl_socket.peeraddr.last rescue "<unknown>"
|
114
|
+
peercert = ssl_socket.peercert
|
112
115
|
subject = peercert ? peercert.subject : nil
|
113
116
|
@error_logger.info(error: error, text: "SSL error, peer: #{peeraddr}, peer cert: #{subject}")
|
114
117
|
end
|
@@ -124,6 +127,7 @@ module Puma
|
|
124
127
|
# Log occurred error debug dump.
|
125
128
|
# +error+ an exception object, +req+ the request,
|
126
129
|
# and +text+ additional info
|
130
|
+
# @version 5.0.0
|
127
131
|
#
|
128
132
|
def debug_error(error, req=nil, text="")
|
129
133
|
@error_logger.debug(error: error, req: req, text: text)
|
data/lib/puma/launcher.rb
CHANGED
@@ -188,10 +188,13 @@ module Puma
|
|
188
188
|
end
|
189
189
|
|
190
190
|
# Return all tcp ports the launcher may be using, TCP or SSL
|
191
|
+
# @!attribute [r] connected_ports
|
192
|
+
# @version 5.0.0
|
191
193
|
def connected_ports
|
192
194
|
@binder.connected_ports
|
193
195
|
end
|
194
196
|
|
197
|
+
# @!attribute [r] restart_args
|
195
198
|
def restart_args
|
196
199
|
cmd = @options[:restart_cmd]
|
197
200
|
if cmd
|
@@ -206,6 +209,8 @@ module Puma
|
|
206
209
|
@binder.close_listeners
|
207
210
|
end
|
208
211
|
|
212
|
+
# @!attribute [r] thread_status
|
213
|
+
# @version 5.0.0
|
209
214
|
def thread_status
|
210
215
|
Thread.list.each do |thread|
|
211
216
|
name = "Thread: TID-#{thread.object_id.to_s(36)}"
|
@@ -259,16 +264,14 @@ module Puma
|
|
259
264
|
end
|
260
265
|
end
|
261
266
|
|
262
|
-
|
267
|
+
# @!attribute [r] files_to_require_after_prune
|
268
|
+
def files_to_require_after_prune
|
263
269
|
puma = spec_for_gem("puma")
|
264
270
|
|
265
|
-
|
266
|
-
"#{d.name}:#{spec_for_gem(d.name).version}"
|
267
|
-
end
|
268
|
-
|
269
|
-
[deps, require_paths_for_gem(puma) + extra_runtime_deps_directories]
|
271
|
+
require_paths_for_gem(puma) + extra_runtime_deps_directories
|
270
272
|
end
|
271
273
|
|
274
|
+
# @!attribute [r] extra_runtime_deps_directories
|
272
275
|
def extra_runtime_deps_directories
|
273
276
|
Array(@options[:extra_runtime_dependencies]).map do |d_name|
|
274
277
|
if (spec = spec_for_gem(d_name))
|
@@ -280,6 +283,7 @@ module Puma
|
|
280
283
|
end.flatten.compact
|
281
284
|
end
|
282
285
|
|
286
|
+
# @!attribute [r] puma_wild_location
|
283
287
|
def puma_wild_location
|
284
288
|
puma = spec_for_gem("puma")
|
285
289
|
dirs = require_paths_for_gem(puma)
|
@@ -296,7 +300,7 @@ module Puma
|
|
296
300
|
return
|
297
301
|
end
|
298
302
|
|
299
|
-
|
303
|
+
dirs = files_to_require_after_prune
|
300
304
|
|
301
305
|
log '* Pruning Bundler environment'
|
302
306
|
home = ENV['GEM_HOME']
|
@@ -305,7 +309,7 @@ module Puma
|
|
305
309
|
ENV['GEM_HOME'] = home
|
306
310
|
ENV['BUNDLE_GEMFILE'] = bundle_gemfile
|
307
311
|
ENV['PUMA_BUNDLER_PRUNED'] = '1'
|
308
|
-
args = [Gem.ruby, puma_wild_location, '-I', dirs.join(':')
|
312
|
+
args = [Gem.ruby, puma_wild_location, '-I', dirs.join(':')] + @original_argv
|
309
313
|
# Ruby 2.0+ defaults to true which breaks socket activation
|
310
314
|
args += [{:close_others => false}]
|
311
315
|
Kernel.exec(*args)
|
@@ -343,6 +347,7 @@ module Puma
|
|
343
347
|
Process.respond_to?(:setproctitle) ? Process.setproctitle(title) : $0 = title
|
344
348
|
end
|
345
349
|
|
350
|
+
# @!attribute [r] title
|
346
351
|
def title
|
347
352
|
buffer = "puma #{Puma::Const::VERSION} (#{@options[:binds].join(',')})"
|
348
353
|
buffer += " [#{@options[:tag]}]" if @options[:tag] && !@options[:tag].empty?
|
@@ -354,6 +359,7 @@ module Puma
|
|
354
359
|
ENV['RACK_ENV'] = environment
|
355
360
|
end
|
356
361
|
|
362
|
+
# @!attribute [r] environment
|
357
363
|
def environment
|
358
364
|
@environment
|
359
365
|
end
|
@@ -478,6 +484,7 @@ module Puma
|
|
478
484
|
"You must have RubyGems #{min_version}+ to use this feature."
|
479
485
|
end
|
480
486
|
|
487
|
+
# @version 5.0.0
|
481
488
|
def with_unbundled_env
|
482
489
|
bundler_ver = Gem::Version.new(Bundler::VERSION)
|
483
490
|
if bundler_ver < Gem::Version.new('2.1.0')
|
data/lib/puma/minissl.rb
CHANGED
@@ -10,9 +10,9 @@ require 'puma/puma_http11'
|
|
10
10
|
|
11
11
|
module Puma
|
12
12
|
module MiniSSL
|
13
|
-
|
14
|
-
# define constant at runtime, as it's easy to determine at built time,
|
13
|
+
# Define constant at runtime, as it's easy to determine at built time,
|
15
14
|
# but Puma could (it shouldn't) be loaded with an older OpenSSL version
|
15
|
+
# @version 5.0.0
|
16
16
|
HAS_TLS1_3 = !IS_JRUBY &&
|
17
17
|
(OPENSSL_VERSION[/ \d+\.\d+\.\d+/].split('.').map(&:to_i) <=> [1,1,1]) != -1 &&
|
18
18
|
(OPENSSL_LIBRARY_VERSION[/ \d+\.\d+\.\d+/].split('.').map(&:to_i) <=> [1,1,1]) !=-1
|
@@ -24,6 +24,7 @@ module Puma
|
|
24
24
|
@peercert = nil
|
25
25
|
end
|
26
26
|
|
27
|
+
# @!attribute [r] to_io
|
27
28
|
def to_io
|
28
29
|
@socket
|
29
30
|
end
|
@@ -32,19 +33,22 @@ module Puma
|
|
32
33
|
@socket.closed?
|
33
34
|
end
|
34
35
|
|
35
|
-
#
|
36
|
-
# first is protocol version (SSL_get_version)
|
36
|
+
# Returns a two element array,
|
37
|
+
# first is protocol version (SSL_get_version),
|
37
38
|
# second is 'handshake' state (SSL_state_string)
|
38
39
|
#
|
39
|
-
#
|
40
|
-
#
|
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
|
41
44
|
#
|
42
45
|
def ssl_version_state
|
43
46
|
IS_JRUBY ? [nil, nil] : @engine.ssl_vers_st
|
44
47
|
end
|
45
48
|
|
46
|
-
#
|
49
|
+
# Used to check the handshake status, in particular when a TCP connection
|
47
50
|
# is made with TLSv1.3 as an available protocol
|
51
|
+
# @version 5.0.0
|
48
52
|
def bad_tlsv1_3?
|
49
53
|
HAS_TLS1_3 && @engine.ssl_vers_st == ['TLSv1.3', 'SSLERR']
|
50
54
|
end
|
@@ -136,14 +140,18 @@ module Puma
|
|
136
140
|
alias_method :<<, :write
|
137
141
|
|
138
142
|
# This is a temporary fix to deal with websockets code using
|
139
|
-
# write_nonblock.
|
143
|
+
# write_nonblock.
|
144
|
+
|
145
|
+
# The problem with implementing it properly
|
140
146
|
# is that it means we'd have to have the ability to rewind
|
141
147
|
# an engine because after we write+extract, the socket
|
142
148
|
# write_nonblock call might raise an exception and later
|
143
149
|
# code would pass the same data in, but the engine would think
|
144
|
-
# it had already written the data in.
|
145
|
-
#
|
146
|
-
#
|
150
|
+
# it had already written the data in.
|
151
|
+
#
|
152
|
+
# So for the time being (and since write blocking is quite rare),
|
153
|
+
# go ahead and actually block in write_nonblock.
|
154
|
+
#
|
147
155
|
def write_nonblock(data, *_)
|
148
156
|
write data
|
149
157
|
end
|
@@ -182,10 +190,12 @@ module Puma
|
|
182
190
|
end
|
183
191
|
end
|
184
192
|
|
193
|
+
# @!attribute [r] peeraddr
|
185
194
|
def peeraddr
|
186
195
|
@socket.peeraddr
|
187
196
|
end
|
188
197
|
|
198
|
+
# @!attribute [r] peercert
|
189
199
|
def peercert
|
190
200
|
return @peercert if @peercert
|
191
201
|
|
@@ -203,8 +213,6 @@ module Puma
|
|
203
213
|
class SSLError < StandardError
|
204
214
|
# Define this for jruby even though it isn't used.
|
205
215
|
end
|
206
|
-
|
207
|
-
def self.check; end
|
208
216
|
end
|
209
217
|
|
210
218
|
class Context
|
@@ -260,14 +268,16 @@ module Puma
|
|
260
268
|
end
|
261
269
|
|
262
270
|
# disables TLSv1
|
271
|
+
# @!attribute [w] no_tlsv1=
|
263
272
|
def no_tlsv1=(tlsv1)
|
264
|
-
raise ArgumentError, "Invalid value of no_tlsv1" unless ['true', 'false', true, false].include?(tlsv1)
|
273
|
+
raise ArgumentError, "Invalid value of no_tlsv1=" unless ['true', 'false', true, false].include?(tlsv1)
|
265
274
|
@no_tlsv1 = tlsv1
|
266
275
|
end
|
267
276
|
|
268
277
|
# disables TLSv1 and TLSv1.1. Overrides `#no_tlsv1=`
|
278
|
+
# @!attribute [w] no_tlsv1_1=
|
269
279
|
def no_tlsv1_1=(tlsv1_1)
|
270
|
-
raise ArgumentError, "Invalid value of
|
280
|
+
raise ArgumentError, "Invalid value of no_tlsv1_1=" unless ['true', 'false', true, false].include?(tlsv1_1)
|
271
281
|
@no_tlsv1_1 = tlsv1_1
|
272
282
|
end
|
273
283
|
|
@@ -283,6 +293,7 @@ module Puma
|
|
283
293
|
@ctx = ctx
|
284
294
|
end
|
285
295
|
|
296
|
+
# @!attribute [r] to_io
|
286
297
|
def to_io
|
287
298
|
@socket
|
288
299
|
end
|
@@ -303,6 +314,8 @@ module Puma
|
|
303
314
|
Socket.new io, engine
|
304
315
|
end
|
305
316
|
|
317
|
+
# @!attribute [r] addr
|
318
|
+
# @version 5.0.0
|
306
319
|
def addr
|
307
320
|
@socket.addr
|
308
321
|
end
|
data/lib/puma/puma_http11.jar
CHANGED
Binary file
|
@@ -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 initialize
|
9
|
+
@closed = false
|
10
|
+
super
|
11
|
+
end
|
12
|
+
def close
|
13
|
+
@closed = true
|
14
|
+
end
|
15
|
+
def closed?
|
16
|
+
@closed
|
17
|
+
end
|
18
|
+
def push(object)
|
19
|
+
@closed ||= false
|
20
|
+
raise ClosedQueueError if @closed
|
21
|
+
super
|
22
|
+
end
|
23
|
+
alias << push
|
24
|
+
end
|
25
|
+
::Queue.prepend QueueClose
|
26
|
+
end
|
data/lib/puma/reactor.rb
CHANGED
@@ -1,405 +1,109 @@
|
|
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
|
-
# The Reactor object is responsible for ensuring that a request has been
|
12
|
-
# completely received before it starts to be processed. This may be known as read buffering.
|
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
|
6
|
+
# Monitors a collection of IO objects, calling a block whenever
|
7
|
+
# any monitored object either receives data or times out, or when the Reactor shuts down.
|
22
8
|
#
|
23
|
-
#
|
24
|
-
#
|
9
|
+
# The waiting/wake up is performed with nio4r, which will use the appropriate backend (libev,
|
10
|
+
# Java NIO or just plain IO#select). The call to `NIO::Selector#select` will
|
11
|
+
# 'wakeup' any IO object that receives data.
|
25
12
|
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
# return the references to any objects that caused it to "wake". The reactor
|
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.
|
13
|
+
# This class additionally tracks a timeout for every added object,
|
14
|
+
# and wakes up any object when its timeout elapses.
|
32
15
|
#
|
33
|
-
#
|
34
|
-
# data is written to that socket reference, then the loop is woken up and it is checked for completeness again.
|
35
|
-
#
|
36
|
-
# A detailed example is given in the docs for `run_internal` which is where the bulk
|
37
|
-
# of this logic lives.
|
16
|
+
# The implementation uses a Queue to synchronize adding new objects from the internal select loop.
|
38
17
|
class Reactor
|
39
|
-
|
40
|
-
|
41
|
-
#
|
42
|
-
|
43
|
-
|
44
|
-
# that is used to write a response for "low level errors"
|
45
|
-
# when there is an exception inside of the reactor.
|
46
|
-
#
|
47
|
-
# The `app_pool` is an instance of `Puma::ThreadPool`.
|
48
|
-
# Once a request is fully formed (header and body are received)
|
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
|
-
|
18
|
+
# Create a new Reactor to monitor IO objects added by #add.
|
19
|
+
# The provided block will be invoked when an IO has data available to read,
|
20
|
+
# its timeout elapses, or when the Reactor shuts down.
|
21
|
+
def initialize(&block)
|
22
|
+
require 'nio'
|
55
23
|
@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
|
24
|
+
@input = Queue.new
|
63
25
|
@timeouts = []
|
64
|
-
|
65
|
-
mon = @selector.register(@ready, :r)
|
66
|
-
mon.value = @ready
|
67
|
-
|
68
|
-
@monitors = [mon]
|
26
|
+
@block = block
|
69
27
|
end
|
70
28
|
|
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
|
-
if submon.value.can_close?
|
193
|
-
submon.value.close
|
194
|
-
else
|
195
|
-
# Pass remaining open client connections to the thread pool.
|
196
|
-
@app_pool << submon.value
|
197
|
-
end
|
198
|
-
begin
|
199
|
-
selector.deregister submon.value
|
200
|
-
rescue IOError
|
201
|
-
# nio4r on jruby seems to throw an IOError here if the IO is closed, so
|
202
|
-
# we need to swallow it.
|
203
|
-
end
|
204
|
-
true
|
205
|
-
end
|
206
|
-
end
|
207
|
-
when "!"
|
208
|
-
return
|
209
|
-
end
|
210
|
-
end
|
211
|
-
else
|
212
|
-
c = mon.value
|
213
|
-
|
214
|
-
# We have to be sure to remove it from the timeout
|
215
|
-
# list or we'll accidentally close the socket when
|
216
|
-
# it's in use!
|
217
|
-
if c.timeout_at
|
218
|
-
@mutex.synchronize do
|
219
|
-
@timeouts.delete mon
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
begin
|
224
|
-
if c.try_to_finish
|
225
|
-
@app_pool << c
|
226
|
-
clear_monitor mon
|
227
|
-
end
|
228
|
-
|
229
|
-
# Don't report these to the lowlevel_error handler, otherwise
|
230
|
-
# will be flooding them with errors when persistent connections
|
231
|
-
# are closed.
|
232
|
-
rescue ConnectionError
|
233
|
-
c.write_error(500)
|
234
|
-
c.close
|
235
|
-
|
236
|
-
clear_monitor mon
|
237
|
-
|
238
|
-
# SSL handshake failure
|
239
|
-
rescue MiniSSL::SSLError => e
|
240
|
-
@server.lowlevel_error(e, c.env)
|
241
|
-
|
242
|
-
ssl_socket = c.io
|
243
|
-
begin
|
244
|
-
addr = ssl_socket.peeraddr.last
|
245
|
-
# EINVAL can happen when browser closes socket w/security exception
|
246
|
-
rescue IOError, Errno::EINVAL
|
247
|
-
addr = "<unknown>"
|
248
|
-
end
|
249
|
-
|
250
|
-
cert = ssl_socket.peercert
|
251
|
-
|
252
|
-
c.close
|
253
|
-
clear_monitor mon
|
254
|
-
|
255
|
-
@events.ssl_error e, addr, cert
|
256
|
-
|
257
|
-
# The client doesn't know HTTP well
|
258
|
-
rescue HttpParserError => e
|
259
|
-
@server.lowlevel_error(e, c.env)
|
260
|
-
|
261
|
-
c.write_error(400)
|
262
|
-
c.close
|
263
|
-
|
264
|
-
clear_monitor mon
|
265
|
-
|
266
|
-
@events.parse_error e, c
|
267
|
-
rescue StandardError => e
|
268
|
-
@server.lowlevel_error(e, c.env)
|
269
|
-
|
270
|
-
c.write_error(500)
|
271
|
-
c.close
|
272
|
-
|
273
|
-
clear_monitor mon
|
274
|
-
end
|
275
|
-
end
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
unless @timeouts.empty?
|
280
|
-
@mutex.synchronize do
|
281
|
-
now = Time.now
|
282
|
-
|
283
|
-
while @timeouts.first.value.timeout_at < now
|
284
|
-
mon = @timeouts.shift
|
285
|
-
c = mon.value
|
286
|
-
c.write_error(408) if c.in_data_phase
|
287
|
-
c.close
|
288
|
-
|
289
|
-
clear_monitor mon
|
290
|
-
|
291
|
-
break if @timeouts.empty?
|
292
|
-
end
|
293
|
-
|
294
|
-
calculate_sleep
|
295
|
-
end
|
29
|
+
# Run the internal select loop, using a background thread by default.
|
30
|
+
def run(background=true)
|
31
|
+
if background
|
32
|
+
@thread = Thread.new do
|
33
|
+
Puma.set_thread_name "reactor"
|
34
|
+
select_loop
|
296
35
|
end
|
36
|
+
else
|
37
|
+
select_loop
|
297
38
|
end
|
298
39
|
end
|
299
40
|
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
ensure
|
310
|
-
@trigger.close
|
311
|
-
@ready.close
|
41
|
+
# Add a new IO object to monitor.
|
42
|
+
# The object must respond to #timeout and #timeout_at.
|
43
|
+
# Returns false if the reactor is already shut down.
|
44
|
+
def add(io)
|
45
|
+
@input << io
|
46
|
+
@selector.wakeup
|
47
|
+
true
|
48
|
+
rescue ClosedQueueError
|
49
|
+
false
|
312
50
|
end
|
313
51
|
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
STDERR.puts "Error in reactor loop escaped: #{e.message} (#{e.class})"
|
321
|
-
STDERR.puts e.backtrace
|
322
|
-
retry
|
323
|
-
ensure
|
324
|
-
@trigger.close
|
325
|
-
@ready.close
|
326
|
-
end
|
52
|
+
# Shutdown the reactor, blocking until the background thread is finished.
|
53
|
+
def shutdown
|
54
|
+
@input.close
|
55
|
+
begin
|
56
|
+
@selector.wakeup
|
57
|
+
rescue IOError # Ignore if selector is already closed
|
327
58
|
end
|
59
|
+
@thread.join if @thread
|
328
60
|
end
|
329
61
|
|
330
|
-
|
331
|
-
# sleep for in the main reactor loop when no sockets are being written to.
|
332
|
-
#
|
333
|
-
# The values kept in `@timeouts` are sorted so that the first timeout
|
334
|
-
# comes first in the array. When there are no timeouts the default timeout is used.
|
335
|
-
#
|
336
|
-
# Otherwise a sleep value is set that is the same as the amount of time it
|
337
|
-
# would take for the first element to time out.
|
338
|
-
#
|
339
|
-
# If that value is in the past, then a sleep value of zero is used.
|
340
|
-
def calculate_sleep
|
341
|
-
if @timeouts.empty?
|
342
|
-
@sleep_for = DefaultSleepFor
|
343
|
-
else
|
344
|
-
diff = @timeouts.first.value.timeout_at.to_f - Time.now.to_f
|
62
|
+
private
|
345
63
|
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
64
|
+
def select_loop
|
65
|
+
begin
|
66
|
+
until @input.closed? && @input.empty?
|
67
|
+
# Wakeup any registered object that receives incoming data.
|
68
|
+
# Block until the earliest timeout or Selector#wakeup is called.
|
69
|
+
timeout = (earliest = @timeouts.first) && earliest.timeout
|
70
|
+
@selector.select(timeout) {|mon| wakeup!(mon.value)}
|
71
|
+
|
72
|
+
# Wakeup all objects that timed out.
|
73
|
+
timed_out = @timeouts.take_while {|t| t.timeout == 0}
|
74
|
+
timed_out.each(&method(:wakeup!))
|
75
|
+
|
76
|
+
unless @input.empty?
|
77
|
+
until @input.empty?
|
78
|
+
client = @input.pop
|
79
|
+
register(client) if client.io_ok?
|
80
|
+
end
|
81
|
+
@timeouts.sort_by!(&:timeout_at)
|
82
|
+
end
|
350
83
|
end
|
84
|
+
rescue StandardError => e
|
85
|
+
STDERR.puts "Error in reactor loop escaped: #{e.message} (#{e.class})"
|
86
|
+
STDERR.puts e.backtrace
|
87
|
+
retry
|
351
88
|
end
|
89
|
+
# Wakeup all remaining objects on shutdown.
|
90
|
+
@timeouts.each(&@block)
|
91
|
+
@selector.close
|
352
92
|
end
|
353
93
|
|
354
|
-
#
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
# object.
|
359
|
-
#
|
360
|
-
# The main body of the reactor loop is in `run_internal` and it
|
361
|
-
# will sleep on `NIO::Selector#select`. When a new connection is added to the
|
362
|
-
# reactor it cannot be added directly to the `sockets` array, because
|
363
|
-
# the `NIO::Selector#select` will not be watching for it yet.
|
364
|
-
#
|
365
|
-
# Instead what needs to happen is that `NIO::Selector#select` needs to be woken up,
|
366
|
-
# the contents of `@input` added to the `sockets` array, and then
|
367
|
-
# another call to `NIO::Selector#select` needs to happen. Since the `Puma::Client`
|
368
|
-
# object can be read immediately, it does not block, but instead returns
|
369
|
-
# right away.
|
370
|
-
#
|
371
|
-
# This behavior is accomplished by writing to `@trigger` which wakes up
|
372
|
-
# the `NIO::Selector#select` and then there is logic to detect the value of `*`,
|
373
|
-
# pull the contents from `@input` and add them to the sockets array.
|
374
|
-
#
|
375
|
-
# If the object passed in has a timeout value in `timeout_at` then
|
376
|
-
# it is added to a `@timeouts` array. This array is then re-arranged
|
377
|
-
# so that the first element to timeout will be at the front of the
|
378
|
-
# array. Then a value to sleep for is derived in the call to `calculate_sleep`
|
379
|
-
def add(c)
|
380
|
-
@mutex.synchronize do
|
381
|
-
@input << c
|
382
|
-
@trigger << "*"
|
383
|
-
end
|
384
|
-
end
|
385
|
-
|
386
|
-
# Close all watched sockets and clear them from being watched
|
387
|
-
def clear!
|
388
|
-
begin
|
389
|
-
@trigger << "c"
|
390
|
-
rescue IOError
|
391
|
-
Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
|
392
|
-
end
|
94
|
+
# Start monitoring the object.
|
95
|
+
def register(io)
|
96
|
+
@selector.register(io, :r).value = io
|
97
|
+
@timeouts << io
|
393
98
|
end
|
394
99
|
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
100
|
+
# 'Wake up' a monitored object by calling the provided block.
|
101
|
+
# Stop monitoring the object if the block returns `true`.
|
102
|
+
def wakeup!(io)
|
103
|
+
if @block.call(io)
|
104
|
+
@selector.deregister(io)
|
105
|
+
@timeouts.delete(io)
|
400
106
|
end
|
401
|
-
|
402
|
-
@thread.join
|
403
107
|
end
|
404
108
|
end
|
405
109
|
end
|