puma 3.11.3 → 4.0.0
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 +5 -5
- data/History.md +61 -0
- data/README.md +41 -11
- data/docs/architecture.md +2 -1
- data/docs/deployment.md +24 -4
- data/docs/restart.md +5 -3
- data/docs/systemd.md +37 -9
- data/ext/puma_http11/PumaHttp11Service.java +2 -0
- data/ext/puma_http11/mini_ssl.c +42 -5
- data/ext/puma_http11/org/jruby/puma/IOBuffer.java +72 -0
- data/ext/puma_http11/org/jruby/puma/MiniSSL.java +17 -4
- data/lib/puma.rb +8 -0
- data/lib/puma/app/status.rb +3 -2
- data/lib/puma/binder.rb +22 -10
- data/lib/puma/cli.rb +18 -7
- data/lib/puma/client.rb +54 -22
- data/lib/puma/cluster.rb +54 -15
- data/lib/puma/commonlogger.rb +2 -0
- data/lib/puma/configuration.rb +4 -1
- data/lib/puma/const.rb +8 -2
- data/lib/puma/control_cli.rb +23 -11
- data/lib/puma/convenient.rb +2 -0
- data/lib/puma/daemon_ext.rb +2 -0
- data/lib/puma/delegation.rb +2 -0
- data/lib/puma/detect.rb +2 -0
- data/lib/puma/dsl.rb +63 -11
- data/lib/puma/events.rb +2 -0
- data/lib/puma/io_buffer.rb +3 -6
- data/lib/puma/jruby_restart.rb +2 -0
- data/lib/puma/launcher.rb +15 -13
- data/lib/puma/minissl.rb +20 -4
- data/lib/puma/null_io.rb +2 -0
- data/lib/puma/plugin.rb +2 -0
- data/lib/puma/rack/builder.rb +2 -1
- data/lib/puma/reactor.rb +215 -30
- data/lib/puma/runner.rb +11 -2
- data/lib/puma/server.rb +63 -26
- data/lib/puma/single.rb +14 -3
- data/lib/puma/state_file.rb +2 -0
- data/lib/puma/tcp_logger.rb +2 -0
- data/lib/puma/thread_pool.rb +50 -5
- data/lib/puma/util.rb +2 -6
- data/lib/rack/handler/puma.rb +4 -0
- data/tools/jungle/README.md +10 -4
- data/tools/jungle/init.d/README.md +2 -0
- data/tools/jungle/init.d/puma +7 -7
- data/tools/jungle/init.d/run-puma +1 -1
- data/tools/jungle/rc.d/README.md +74 -0
- data/tools/jungle/rc.d/puma +61 -0
- data/tools/jungle/rc.d/puma.conf +10 -0
- metadata +23 -9
- data/lib/puma/compat.rb +0 -14
- data/lib/puma/java_io_buffer.rb +0 -45
- data/lib/puma/rack/backports/uri/common_193.rb +0 -33
data/lib/puma/events.rb
CHANGED
data/lib/puma/io_buffer.rb
CHANGED
data/lib/puma/jruby_restart.rb
CHANGED
data/lib/puma/launcher.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'puma/events'
|
2
4
|
require 'puma/detect'
|
3
5
|
|
@@ -63,8 +65,8 @@ module Puma
|
|
63
65
|
|
64
66
|
generate_restart_data
|
65
67
|
|
66
|
-
if clustered? &&
|
67
|
-
unsupported
|
68
|
+
if clustered? && !Process.respond_to?(:fork)
|
69
|
+
unsupported "worker mode not supported on #{RUBY_ENGINE} on this platform"
|
68
70
|
end
|
69
71
|
|
70
72
|
if @options[:daemon] && Puma.windows?
|
@@ -86,6 +88,7 @@ module Puma
|
|
86
88
|
else
|
87
89
|
@runner = Single.new(self, @events)
|
88
90
|
end
|
91
|
+
Puma.stats_object = @runner
|
89
92
|
|
90
93
|
@status = :run
|
91
94
|
end
|
@@ -211,6 +214,15 @@ module Puma
|
|
211
214
|
end
|
212
215
|
end
|
213
216
|
|
217
|
+
def close_binder_listeners
|
218
|
+
@binder.listeners.each do |l, io|
|
219
|
+
io.close
|
220
|
+
uri = URI.parse(l)
|
221
|
+
next unless uri.scheme == 'unix'
|
222
|
+
File.unlink("#{uri.host}#{uri.path}")
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
214
226
|
private
|
215
227
|
|
216
228
|
def reload_worker_directory
|
@@ -316,16 +328,6 @@ module Puma
|
|
316
328
|
@options[:prune_bundler] && clustered? && !@options[:preload_app]
|
317
329
|
end
|
318
330
|
|
319
|
-
def close_binder_listeners
|
320
|
-
@binder.listeners.each do |l, io|
|
321
|
-
io.close
|
322
|
-
uri = URI.parse(l)
|
323
|
-
next unless uri.scheme == 'unix'
|
324
|
-
File.unlink("#{uri.host}#{uri.path}")
|
325
|
-
end
|
326
|
-
end
|
327
|
-
|
328
|
-
|
329
331
|
def generate_restart_data
|
330
332
|
if dir = @options[:directory]
|
331
333
|
@restart_dir = dir
|
@@ -394,7 +396,7 @@ module Puma
|
|
394
396
|
Signal.trap "SIGTERM" do
|
395
397
|
graceful_stop
|
396
398
|
|
397
|
-
raise
|
399
|
+
raise(SignalException, "SIGTERM") if @options[:raise_exception_on_sigterm]
|
398
400
|
end
|
399
401
|
rescue Exception
|
400
402
|
log "*** SIGTERM not implemented, signal based gracefully stopping unavailable!"
|
data/lib/puma/minissl.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
begin
|
2
4
|
require 'io/wait'
|
3
|
-
|
5
|
+
rescue LoadError
|
4
6
|
end
|
5
7
|
|
6
8
|
module Puma
|
@@ -124,7 +126,7 @@ module Puma
|
|
124
126
|
|
125
127
|
def read_and_drop(timeout = 1)
|
126
128
|
return :timeout unless IO.select([@socket], nil, nil, timeout)
|
127
|
-
read_nonblock(1024)
|
129
|
+
return :eof unless read_nonblock(1024)
|
128
130
|
:drop
|
129
131
|
rescue Errno::EAGAIN
|
130
132
|
# do nothing
|
@@ -141,7 +143,7 @@ module Puma
|
|
141
143
|
# Don't let this socket hold this loop forever.
|
142
144
|
# If it can't send more packets within 1s, then give up.
|
143
145
|
while should_drop_bytes?
|
144
|
-
return if read_and_drop(1)
|
146
|
+
return if [:timeout, :eof].include?(read_and_drop(1))
|
145
147
|
end
|
146
148
|
rescue IOError, SystemCallError
|
147
149
|
Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
|
@@ -175,11 +177,17 @@ module Puma
|
|
175
177
|
|
176
178
|
class Context
|
177
179
|
attr_accessor :verify_mode
|
180
|
+
attr_reader :no_tlsv1
|
181
|
+
|
182
|
+
def initialize
|
183
|
+
@no_tlsv1 = false
|
184
|
+
end
|
178
185
|
|
179
186
|
if defined?(JRUBY_VERSION)
|
180
187
|
# jruby-specific Context properties: java uses a keystore and password pair rather than a cert/key pair
|
181
188
|
attr_reader :keystore
|
182
189
|
attr_accessor :keystore_pass
|
190
|
+
attr_accessor :ssl_cipher_list
|
183
191
|
|
184
192
|
def keystore=(keystore)
|
185
193
|
raise ArgumentError, "No such keystore file '#{keystore}'" unless File.exist? keystore
|
@@ -195,6 +203,7 @@ module Puma
|
|
195
203
|
attr_reader :key
|
196
204
|
attr_reader :cert
|
197
205
|
attr_reader :ca
|
206
|
+
attr_accessor :ssl_cipher_filter
|
198
207
|
|
199
208
|
def key=(key)
|
200
209
|
raise ArgumentError, "No such key file '#{key}'" unless File.exist? key
|
@@ -211,11 +220,18 @@ module Puma
|
|
211
220
|
@ca = ca
|
212
221
|
end
|
213
222
|
|
223
|
+
|
214
224
|
def check
|
215
225
|
raise "Key not configured" unless @key
|
216
226
|
raise "Cert not configured" unless @cert
|
217
227
|
end
|
218
228
|
end
|
229
|
+
|
230
|
+
def no_tlsv1=(tlsv1)
|
231
|
+
raise ArgumentError, "Invalid value of no_tlsv1" unless ['true', 'false', true, false].include?(tlsv1)
|
232
|
+
@no_tlsv1 = tlsv1
|
233
|
+
end
|
234
|
+
|
219
235
|
end
|
220
236
|
|
221
237
|
VERIFY_NONE = 0
|
@@ -249,7 +265,7 @@ module Puma
|
|
249
265
|
end
|
250
266
|
|
251
267
|
def close
|
252
|
-
@socket.close
|
268
|
+
@socket.close unless @socket.closed? # closed? call is for Windows
|
253
269
|
end
|
254
270
|
end
|
255
271
|
end
|
data/lib/puma/null_io.rb
CHANGED
data/lib/puma/plugin.rb
CHANGED
data/lib/puma/rack/builder.rb
CHANGED
@@ -110,7 +110,8 @@ module Puma::Rack
|
|
110
110
|
|
111
111
|
has_options = false
|
112
112
|
server.valid_options.each do |name, description|
|
113
|
-
next if name.to_s
|
113
|
+
next if name.to_s =~ /^(Host|Port)[^a-zA-Z]/ # ignore handler's host and port options, we do our own.
|
114
|
+
|
114
115
|
info << " -O %-21s %s" % [name, description]
|
115
116
|
has_options = true
|
116
117
|
end
|
data/lib/puma/reactor.rb
CHANGED
@@ -1,58 +1,201 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'puma/util'
|
2
4
|
require 'puma/minissl'
|
3
5
|
|
6
|
+
require 'nio'
|
7
|
+
|
4
8
|
module Puma
|
9
|
+
# Internal Docs, Not a public interface.
|
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
|
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.
|
25
|
+
#
|
26
|
+
# The waiting/wake up is performed with nio4r, which will use the apropriate backend (libev, Java NIO or
|
27
|
+
# just plain IO#select). The call to `NIO::Selector#select` will "wake up" and
|
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.
|
32
|
+
#
|
33
|
+
# If the request is not complete, then it stays in the array, and the next time any
|
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.
|
5
38
|
class Reactor
|
6
39
|
DefaultSleepFor = 5
|
7
40
|
|
41
|
+
# Creates an instance of Puma::Reactor
|
42
|
+
#
|
43
|
+
# The `server` argument is an instance of `Puma::Server`
|
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`.
|
8
50
|
def initialize(server, app_pool)
|
9
51
|
@server = server
|
10
52
|
@events = server.events
|
11
53
|
@app_pool = app_pool
|
12
54
|
|
55
|
+
@selector = NIO::Selector.new
|
56
|
+
|
13
57
|
@mutex = Mutex.new
|
58
|
+
|
59
|
+
# Read / Write pipes to wake up internal while loop
|
14
60
|
@ready, @trigger = Puma::Util.pipe
|
15
61
|
@input = []
|
16
62
|
@sleep_for = DefaultSleepFor
|
17
63
|
@timeouts = []
|
18
64
|
|
19
|
-
|
65
|
+
mon = @selector.register(@ready, :r)
|
66
|
+
mon.value = @ready
|
67
|
+
|
68
|
+
@monitors = [mon]
|
20
69
|
end
|
21
70
|
|
22
71
|
private
|
23
72
|
|
73
|
+
# Until a request is added via the `add` method this method will internally
|
74
|
+
# loop, waiting on the `sockets` array objects. The only object in this
|
75
|
+
# array at first is the `@ready` IO object, which is the read end of a pipe
|
76
|
+
# connected to `@trigger` object. When `@trigger` is written to, then the loop
|
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`.
|
24
130
|
def run_internal
|
25
|
-
|
131
|
+
monitors = @monitors
|
132
|
+
selector = @selector
|
26
133
|
|
27
134
|
while true
|
28
135
|
begin
|
29
|
-
ready =
|
136
|
+
ready = selector.select @sleep_for
|
30
137
|
rescue IOError => e
|
31
138
|
Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
|
32
|
-
if
|
139
|
+
if monitors.any? { |mon| mon.value.closed? }
|
33
140
|
STDERR.puts "Error in select: #{e.message} (#{e.class})"
|
34
141
|
STDERR.puts e.backtrace
|
35
|
-
|
142
|
+
|
143
|
+
monitors.reject! do |mon|
|
144
|
+
if mon.value.closed?
|
145
|
+
selector.deregister mon.value
|
146
|
+
true
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
36
150
|
retry
|
37
151
|
else
|
38
152
|
raise
|
39
153
|
end
|
40
154
|
end
|
41
155
|
|
42
|
-
if ready
|
43
|
-
|
44
|
-
if
|
156
|
+
if ready
|
157
|
+
ready.each do |mon|
|
158
|
+
if mon.value == @ready
|
45
159
|
@mutex.synchronize do
|
46
160
|
case @ready.read(1)
|
47
161
|
when "*"
|
48
|
-
|
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
|
49
183
|
@input.clear
|
184
|
+
|
185
|
+
@timeouts.sort! { |a,b| a.value.timeout_at <=> b.value.timeout_at }
|
186
|
+
calculate_sleep
|
50
187
|
when "c"
|
51
|
-
|
52
|
-
if
|
188
|
+
monitors.reject! do |submon|
|
189
|
+
if submon.value == @ready
|
53
190
|
false
|
54
191
|
else
|
55
|
-
|
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
|
56
199
|
true
|
57
200
|
end
|
58
201
|
end
|
@@ -61,19 +204,21 @@ module Puma
|
|
61
204
|
end
|
62
205
|
end
|
63
206
|
else
|
207
|
+
c = mon.value
|
208
|
+
|
64
209
|
# We have to be sure to remove it from the timeout
|
65
210
|
# list or we'll accidentally close the socket when
|
66
211
|
# it's in use!
|
67
212
|
if c.timeout_at
|
68
213
|
@mutex.synchronize do
|
69
|
-
@timeouts.delete
|
214
|
+
@timeouts.delete mon
|
70
215
|
end
|
71
216
|
end
|
72
217
|
|
73
218
|
begin
|
74
219
|
if c.try_to_finish
|
75
220
|
@app_pool << c
|
76
|
-
|
221
|
+
clear_monitor mon
|
77
222
|
end
|
78
223
|
|
79
224
|
# Don't report these to the lowlevel_error handler, otherwise
|
@@ -83,18 +228,23 @@ module Puma
|
|
83
228
|
c.write_500
|
84
229
|
c.close
|
85
230
|
|
86
|
-
|
231
|
+
clear_monitor mon
|
87
232
|
|
88
233
|
# SSL handshake failure
|
89
234
|
rescue MiniSSL::SSLError => e
|
90
235
|
@server.lowlevel_error(e, c.env)
|
91
236
|
|
92
237
|
ssl_socket = c.io
|
93
|
-
|
238
|
+
begin
|
239
|
+
addr = ssl_socket.peeraddr.last
|
240
|
+
rescue IOError
|
241
|
+
addr = "<unknown>"
|
242
|
+
end
|
243
|
+
|
94
244
|
cert = ssl_socket.peercert
|
95
245
|
|
96
246
|
c.close
|
97
|
-
|
247
|
+
clear_monitor mon
|
98
248
|
|
99
249
|
@events.ssl_error @server, addr, cert, e
|
100
250
|
|
@@ -105,7 +255,7 @@ module Puma
|
|
105
255
|
c.write_400
|
106
256
|
c.close
|
107
257
|
|
108
|
-
|
258
|
+
clear_monitor mon
|
109
259
|
|
110
260
|
@events.parse_error @server, c.env, e
|
111
261
|
rescue StandardError => e
|
@@ -114,7 +264,7 @@ module Puma
|
|
114
264
|
c.write_500
|
115
265
|
c.close
|
116
266
|
|
117
|
-
|
267
|
+
clear_monitor mon
|
118
268
|
end
|
119
269
|
end
|
120
270
|
end
|
@@ -124,11 +274,13 @@ module Puma
|
|
124
274
|
@mutex.synchronize do
|
125
275
|
now = Time.now
|
126
276
|
|
127
|
-
while @timeouts.first.timeout_at < now
|
128
|
-
|
277
|
+
while @timeouts.first.value.timeout_at < now
|
278
|
+
mon = @timeouts.shift
|
279
|
+
c = mon.value
|
129
280
|
c.write_408 if c.in_data_phase
|
130
281
|
c.close
|
131
|
-
|
282
|
+
|
283
|
+
clear_monitor mon
|
132
284
|
|
133
285
|
break if @timeouts.empty?
|
134
286
|
end
|
@@ -139,6 +291,11 @@ module Puma
|
|
139
291
|
end
|
140
292
|
end
|
141
293
|
|
294
|
+
def clear_monitor(mon)
|
295
|
+
@selector.deregister mon.value
|
296
|
+
@monitors.delete mon
|
297
|
+
end
|
298
|
+
|
142
299
|
public
|
143
300
|
|
144
301
|
def run
|
@@ -163,11 +320,21 @@ module Puma
|
|
163
320
|
end
|
164
321
|
end
|
165
322
|
|
323
|
+
# The `calculate_sleep` sets the value that the `NIO::Selector#select` will
|
324
|
+
# sleep for in the main reactor loop when no sockets are being written to.
|
325
|
+
#
|
326
|
+
# The values kept in `@timeouts` are sorted so that the first timeout
|
327
|
+
# comes first in the array. When there are no timeouts the default timeout is used.
|
328
|
+
#
|
329
|
+
# Otherwise a sleep value is set that is the same as the amount of time it
|
330
|
+
# would take for the first element to time out.
|
331
|
+
#
|
332
|
+
# If that value is in the past, then a sleep value of zero is used.
|
166
333
|
def calculate_sleep
|
167
334
|
if @timeouts.empty?
|
168
335
|
@sleep_for = DefaultSleepFor
|
169
336
|
else
|
170
|
-
diff = @timeouts.first.timeout_at.to_f - Time.now.to_f
|
337
|
+
diff = @timeouts.first.value.timeout_at.to_f - Time.now.to_f
|
171
338
|
|
172
339
|
if diff < 0.0
|
173
340
|
@sleep_for = 0
|
@@ -177,17 +344,35 @@ module Puma
|
|
177
344
|
end
|
178
345
|
end
|
179
346
|
|
347
|
+
# This method adds a connection to the reactor
|
348
|
+
#
|
349
|
+
# Typically called by `Puma::Server` the value passed in
|
350
|
+
# is usually a `Puma::Client` object that responds like an IO
|
351
|
+
# object.
|
352
|
+
#
|
353
|
+
# The main body of the reactor loop is in `run_internal` and it
|
354
|
+
# will sleep on `NIO::Selector#select`. When a new connection is added to the
|
355
|
+
# reactor it cannot be added directly to the `sockets` array, because
|
356
|
+
# the `NIO::Selector#select` will not be watching for it yet.
|
357
|
+
#
|
358
|
+
# Instead what needs to happen is that `NIO::Selector#select` needs to be woken up,
|
359
|
+
# the contents of `@input` added to the `sockets` array, and then
|
360
|
+
# another call to `NIO::Selector#select` needs to happen. Since the `Puma::Client`
|
361
|
+
# object can be read immediately, it does not block, but instead returns
|
362
|
+
# right away.
|
363
|
+
#
|
364
|
+
# This behavior is accomplished by writing to `@trigger` which wakes up
|
365
|
+
# the `NIO::Selector#select` and then there is logic to detect the value of `*`,
|
366
|
+
# pull the contents from `@input` and add them to the sockets array.
|
367
|
+
#
|
368
|
+
# If the object passed in has a timeout value in `timeout_at` then
|
369
|
+
# it is added to a `@timeouts` array. This array is then re-arranged
|
370
|
+
# so that the first element to timeout will be at the front of the
|
371
|
+
# array. Then a value to sleep for is derived in the call to `calculate_sleep`
|
180
372
|
def add(c)
|
181
373
|
@mutex.synchronize do
|
182
374
|
@input << c
|
183
375
|
@trigger << "*"
|
184
|
-
|
185
|
-
if c.timeout_at
|
186
|
-
@timeouts << c
|
187
|
-
@timeouts.sort! { |a,b| a.timeout_at <=> b.timeout_at }
|
188
|
-
|
189
|
-
calculate_sleep
|
190
|
-
end
|
191
376
|
end
|
192
377
|
end
|
193
378
|
|