puma 3.8.2 → 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.

Files changed (71) hide show
  1. checksums.yaml +5 -5
  2. data/History.md +157 -0
  3. data/README.md +155 -225
  4. data/docs/architecture.md +37 -0
  5. data/{DEPLOYMENT.md → docs/deployment.md} +24 -4
  6. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  7. data/docs/images/puma-connection-flow.png +0 -0
  8. data/docs/images/puma-general-arch.png +0 -0
  9. data/docs/plugins.md +28 -0
  10. data/docs/restart.md +41 -0
  11. data/docs/signals.md +56 -3
  12. data/docs/systemd.md +130 -37
  13. data/ext/puma_http11/PumaHttp11Service.java +2 -0
  14. data/ext/puma_http11/http11_parser.c +84 -84
  15. data/ext/puma_http11/http11_parser.rl +9 -9
  16. data/ext/puma_http11/mini_ssl.c +51 -9
  17. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +13 -16
  18. data/ext/puma_http11/org/jruby/puma/IOBuffer.java +72 -0
  19. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +26 -6
  20. data/lib/puma.rb +8 -0
  21. data/lib/puma/app/status.rb +9 -0
  22. data/lib/puma/binder.rb +31 -18
  23. data/lib/puma/cli.rb +22 -7
  24. data/lib/puma/client.rb +67 -18
  25. data/lib/puma/cluster.rb +64 -19
  26. data/lib/puma/commonlogger.rb +2 -0
  27. data/lib/puma/configuration.rb +22 -14
  28. data/lib/puma/const.rb +13 -2
  29. data/lib/puma/control_cli.rb +26 -14
  30. data/lib/puma/convenient.rb +2 -0
  31. data/lib/puma/daemon_ext.rb +2 -0
  32. data/lib/puma/delegation.rb +2 -0
  33. data/lib/puma/detect.rb +2 -0
  34. data/lib/puma/dsl.rb +91 -12
  35. data/lib/puma/events.rb +3 -2
  36. data/lib/puma/io_buffer.rb +3 -6
  37. data/lib/puma/jruby_restart.rb +2 -1
  38. data/lib/puma/launcher.rb +51 -30
  39. data/lib/puma/minissl.rb +79 -28
  40. data/lib/puma/null_io.rb +2 -0
  41. data/lib/puma/plugin.rb +2 -0
  42. data/lib/puma/plugin/tmp_restart.rb +0 -1
  43. data/lib/puma/rack/builder.rb +2 -1
  44. data/lib/puma/reactor.rb +218 -30
  45. data/lib/puma/runner.rb +17 -4
  46. data/lib/puma/server.rb +113 -49
  47. data/lib/puma/single.rb +16 -5
  48. data/lib/puma/state_file.rb +2 -0
  49. data/lib/puma/tcp_logger.rb +2 -0
  50. data/lib/puma/thread_pool.rb +59 -6
  51. data/lib/puma/util.rb +2 -6
  52. data/lib/rack/handler/puma.rb +13 -2
  53. data/tools/jungle/README.md +12 -2
  54. data/tools/jungle/init.d/README.md +2 -0
  55. data/tools/jungle/init.d/puma +7 -7
  56. data/tools/jungle/init.d/run-puma +1 -1
  57. data/tools/jungle/rc.d/README.md +74 -0
  58. data/tools/jungle/rc.d/puma +61 -0
  59. data/tools/jungle/rc.d/puma.conf +10 -0
  60. data/tools/trickletest.rb +1 -1
  61. metadata +25 -87
  62. data/.github/issue_template.md +0 -20
  63. data/Gemfile +0 -12
  64. data/Manifest.txt +0 -78
  65. data/Rakefile +0 -158
  66. data/Release.md +0 -9
  67. data/gemfiles/2.1-Gemfile +0 -12
  68. data/lib/puma/compat.rb +0 -14
  69. data/lib/puma/java_io_buffer.rb +0 -45
  70. data/lib/puma/rack/backports/uri/common_193.rb +0 -33
  71. data/puma.gemspec +0 -52
data/lib/puma/events.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puma/const'
2
4
  require "puma/null_io"
3
5
  require 'stringio'
@@ -91,8 +93,7 @@ module Puma
91
93
  # parsing exception.
92
94
  #
93
95
  def parse_error(server, env, error)
94
- @stderr.puts "#{Time.now}: HTTP parse error, malformed request (#{env[HTTP_X_FORWARDED_FOR] || env[REMOTE_ADDR]}): #{error.inspect}"
95
- @stderr.puts "#{Time.now}: ENV: #{env.inspect}\n---\n"
96
+ @stderr.puts "#{Time.now}: HTTP parse error, malformed request (#{env[HTTP_X_FORWARDED_FOR] || env[REMOTE_ADDR]}): #{error.inspect}\n---\n"
96
97
  end
97
98
 
98
99
  # An SSL error has occurred.
@@ -1,7 +1,4 @@
1
- require 'puma/detect'
1
+ # frozen_string_literal: true
2
2
 
3
- if Puma.jruby?
4
- require 'puma/java_io_buffer'
5
- else
6
- require 'puma/puma_http11'
7
- end
3
+ require 'puma/detect'
4
+ require 'puma/puma_http11'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ffi'
2
4
 
3
5
  module Puma
@@ -80,4 +82,3 @@ module Puma
80
82
  end
81
83
  end
82
84
  end
83
-
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
 
@@ -40,7 +42,7 @@ module Puma
40
42
  # [200, {}, ["hello world"]]
41
43
  # end
42
44
  # end
43
- # Puma::Launcher.new(conf, argv: Puma::Events.stdio).run
45
+ # Puma::Launcher.new(conf, events: Puma::Events.stdio).run
44
46
  def initialize(conf, launcher_args={})
45
47
  @runner = nil
46
48
  @events = launcher_args[:events] || Events::DEFAULT
@@ -63,8 +65,8 @@ module Puma
63
65
 
64
66
  generate_restart_data
65
67
 
66
- if clustered? && (Puma.jruby? || Puma.windows?)
67
- unsupported 'worker mode not supported on JRuby or Windows'
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
@@ -163,6 +166,17 @@ module Puma
163
166
 
164
167
  # Run the server. This blocks until the server is stopped
165
168
  def run
169
+ previous_env =
170
+ if defined?(Bundler)
171
+ env = Bundler::ORIGINAL_ENV.dup
172
+ # add -rbundler/setup so we load from Gemfile when restarting
173
+ bundle = "-rbundler/setup"
174
+ env["RUBYOPT"] = [env["RUBYOPT"], bundle].join(" ").lstrip unless env["RUBYOPT"].to_s.include?(bundle)
175
+ env
176
+ else
177
+ ENV.to_h
178
+ end
179
+
166
180
  @config.clamp
167
181
 
168
182
  @config.plugins.fire_starts self
@@ -178,6 +192,7 @@ module Puma
178
192
  graceful_stop
179
193
  when :restart
180
194
  log "* Restarting..."
195
+ ENV.replace(previous_env)
181
196
  @runner.before_restart
182
197
  restart!
183
198
  when :exit
@@ -199,6 +214,15 @@ module Puma
199
214
  end
200
215
  end
201
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
+
202
226
  private
203
227
 
204
228
  def reload_worker_directory
@@ -222,8 +246,8 @@ module Puma
222
246
  else
223
247
  redirects = {:close_others => true}
224
248
  @binder.listeners.each_with_index do |(l, io), i|
225
- ENV["PUMA_INHERIT_#{i}"] = "#{io.to_i}:#{l}"
226
- redirects[io.to_i] = io.to_i
249
+ ENV["PUMA_INHERIT_#{i}"] = "#{io.to_i}:#{l}"
250
+ redirects[io.to_i] = io.to_i
227
251
  end
228
252
 
229
253
  argv = restart_args
@@ -286,8 +310,8 @@ module Puma
286
310
  end
287
311
 
288
312
  def title
289
- buffer = "puma #{Puma::Const::VERSION} (#{@options[:binds].join(',')})"
290
- buffer << " [#{@options[:tag]}]" if @options[:tag] && !@options[:tag].empty?
313
+ buffer = "puma #{Puma::Const::VERSION} (#{@options[:binds].join(',')})"
314
+ buffer += " [#{@options[:tag]}]" if @options[:tag] && !@options[:tag].empty?
291
315
  buffer
292
316
  end
293
317
 
@@ -304,16 +328,6 @@ module Puma
304
328
  @options[:prune_bundler] && clustered? && !@options[:preload_app]
305
329
  end
306
330
 
307
- def close_binder_listeners
308
- @binder.listeners.each do |l, io|
309
- io.close
310
- uri = URI.parse(l)
311
- next unless uri.scheme == 'unix'
312
- File.unlink("#{uri.host}#{uri.path}")
313
- end
314
- end
315
-
316
-
317
331
  def generate_restart_data
318
332
  if dir = @options[:directory]
319
333
  @restart_dir = dir
@@ -337,8 +351,6 @@ module Puma
337
351
 
338
352
  @restart_dir ||= Dir.pwd
339
353
 
340
- require 'rubygems'
341
-
342
354
  # if $0 is a file in the current directory, then restart
343
355
  # it the same, otherwise add -S on there because it was
344
356
  # picked up in PATH.
@@ -349,9 +361,10 @@ module Puma
349
361
  arg0 = [Gem.ruby, "-S", $0]
350
362
  end
351
363
 
352
- # Detect and reinject -Ilib from the command line
364
+ # Detect and reinject -Ilib from the command line, used for testing without bundler
365
+ # cruby has an expanded path, jruby has just "lib"
353
366
  lib = File.expand_path "lib"
354
- arg0[1,0] = ["-I", lib] if $:[0] == lib
367
+ arg0[1,0] = ["-I", lib] if [lib, "lib"].include?($LOAD_PATH[0])
355
368
 
356
369
  if defined? Puma::WILD_ARGS
357
370
  @restart_argv = arg0 + Puma::WILD_ARGS + @original_argv
@@ -381,12 +394,28 @@ module Puma
381
394
 
382
395
  begin
383
396
  Signal.trap "SIGTERM" do
384
- stop
397
+ graceful_stop
398
+
399
+ raise(SignalException, "SIGTERM") if @options[:raise_exception_on_sigterm]
385
400
  end
386
401
  rescue Exception
387
402
  log "*** SIGTERM not implemented, signal based gracefully stopping unavailable!"
388
403
  end
389
404
 
405
+ begin
406
+ Signal.trap "SIGINT" do
407
+ if Puma.jruby?
408
+ @status = :exit
409
+ graceful_stop
410
+ exit
411
+ end
412
+
413
+ stop
414
+ end
415
+ rescue Exception
416
+ log "*** SIGINT not implemented, signal based gracefully stopping unavailable!"
417
+ end
418
+
390
419
  begin
391
420
  Signal.trap "SIGHUP" do
392
421
  if @runner.redirected_io?
@@ -398,14 +427,6 @@ module Puma
398
427
  rescue Exception
399
428
  log "*** SIGHUP not implemented, signal based logs reopening unavailable!"
400
429
  end
401
-
402
- if Puma.jruby?
403
- Signal.trap("INT") do
404
- @status = :exit
405
- graceful_stop
406
- exit
407
- end
408
- end
409
430
  end
410
431
  end
411
432
  end
data/lib/puma/minissl.rb CHANGED
@@ -1,3 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'io/wait'
5
+ rescue LoadError
6
+ end
7
+
1
8
  module Puma
2
9
  module MiniSSL
3
10
  class Socket
@@ -11,6 +18,10 @@ module Puma
11
18
  @socket
12
19
  end
13
20
 
21
+ def closed?
22
+ @socket.closed?
23
+ end
24
+
14
25
  def readpartial(size)
15
26
  while true
16
27
  output = @engine.read
@@ -36,12 +47,29 @@ module Puma
36
47
  output
37
48
  end
38
49
 
39
- def read_nonblock(size)
50
+ def read_nonblock(size, *_)
51
+ # *_ is to deal with keyword args that were added
52
+ # at some point (and being used in the wild)
40
53
  while true
41
54
  output = engine_read_all
42
55
  return output if output
43
56
 
44
- data = @socket.read_nonblock(size)
57
+ begin
58
+ data = @socket.read_nonblock(size, exception: false)
59
+ if data == :wait_readable || data == :wait_writable
60
+ if @socket.to_io.respond_to?(data)
61
+ @socket.to_io.__send__(data)
62
+ elsif data == :wait_readable
63
+ IO.select([@socket.to_io])
64
+ else
65
+ IO.select(nil, [@socket.to_io])
66
+ end
67
+ elsif !data
68
+ return nil
69
+ else
70
+ break
71
+ end
72
+ end while true
45
73
 
46
74
  @engine.inject(data)
47
75
  output = engine_read_all
@@ -55,6 +83,8 @@ module Puma
55
83
  end
56
84
 
57
85
  def write(data)
86
+ return 0 if data.empty?
87
+
58
88
  need = data.bytesize
59
89
 
60
90
  while true
@@ -77,39 +107,46 @@ module Puma
77
107
  alias_method :syswrite, :write
78
108
  alias_method :<<, :write
79
109
 
110
+ # This is a temporary fix to deal with websockets code using
111
+ # write_nonblock. The problem with implementing it properly
112
+ # is that it means we'd have to have the ability to rewind
113
+ # an engine because after we write+extract, the socket
114
+ # write_nonblock call might raise an exception and later
115
+ # code would pass the same data in, but the engine would think
116
+ # it had already written the data in. So for the time being
117
+ # (and since write blocking is quite rare), go ahead and actually
118
+ # block in write_nonblock.
119
+ def write_nonblock(data, *_)
120
+ write data
121
+ end
122
+
80
123
  def flush
81
124
  @socket.flush
82
125
  end
83
126
 
84
- def close
85
- begin
86
- # Try to setup (so that we can then close them) any
87
- # partially initialized sockets.
88
- while @engine.init?
89
- # Don't let this socket hold this loop forever.
90
- # If it can't send more packets within 1s, then
91
- # give up.
92
- return unless IO.select([@socket], nil, nil, 1)
93
- begin
94
- read_nonblock(1024)
95
- rescue Errno::EAGAIN
96
- end
97
- end
98
-
99
- done = @engine.shutdown
100
-
101
- while true
102
- enc = @engine.extract
103
- @socket.write enc
104
-
105
- notify = @socket.sysread(1024)
127
+ def read_and_drop(timeout = 1)
128
+ return :timeout unless IO.select([@socket], nil, nil, timeout)
129
+ return :eof unless read_nonblock(1024)
130
+ :drop
131
+ rescue Errno::EAGAIN
132
+ # do nothing
133
+ :eagain
134
+ end
106
135
 
107
- @engine.inject notify
108
- done = @engine.shutdown
136
+ def should_drop_bytes?
137
+ @engine.init? || !@engine.shutdown
138
+ end
109
139
 
110
- break if done
140
+ def close
141
+ begin
142
+ # Read any drop any partially initialized sockets and any received bytes during shutdown.
143
+ # Don't let this socket hold this loop forever.
144
+ # If it can't send more packets within 1s, then give up.
145
+ while should_drop_bytes?
146
+ return if [:timeout, :eof].include?(read_and_drop(1))
111
147
  end
112
148
  rescue IOError, SystemCallError
149
+ Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
113
150
  # nothing
114
151
  ensure
115
152
  @socket.close
@@ -140,11 +177,17 @@ module Puma
140
177
 
141
178
  class Context
142
179
  attr_accessor :verify_mode
180
+ attr_reader :no_tlsv1
181
+
182
+ def initialize
183
+ @no_tlsv1 = false
184
+ end
143
185
 
144
186
  if defined?(JRUBY_VERSION)
145
187
  # jruby-specific Context properties: java uses a keystore and password pair rather than a cert/key pair
146
188
  attr_reader :keystore
147
189
  attr_accessor :keystore_pass
190
+ attr_accessor :ssl_cipher_list
148
191
 
149
192
  def keystore=(keystore)
150
193
  raise ArgumentError, "No such keystore file '#{keystore}'" unless File.exist? keystore
@@ -160,6 +203,7 @@ module Puma
160
203
  attr_reader :key
161
204
  attr_reader :cert
162
205
  attr_reader :ca
206
+ attr_accessor :ssl_cipher_filter
163
207
 
164
208
  def key=(key)
165
209
  raise ArgumentError, "No such key file '#{key}'" unless File.exist? key
@@ -176,11 +220,18 @@ module Puma
176
220
  @ca = ca
177
221
  end
178
222
 
223
+
179
224
  def check
180
225
  raise "Key not configured" unless @key
181
226
  raise "Cert not configured" unless @cert
182
227
  end
183
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
+
184
235
  end
185
236
 
186
237
  VERIFY_NONE = 0
@@ -214,7 +265,7 @@ module Puma
214
265
  end
215
266
 
216
267
  def close
217
- @socket.close
268
+ @socket.close unless @socket.closed? # closed? call is for Windows
218
269
  end
219
270
  end
220
271
  end
data/lib/puma/null_io.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Puma
2
4
  # Provides an IO-like object that always appears to contain no data.
3
5
  # Used as the value for rack.input when the request has no body.
data/lib/puma/plugin.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Puma
2
4
  class UnknownPlugin < RuntimeError; end
3
5
 
@@ -32,4 +32,3 @@ Puma::Plugin.create do
32
32
  end
33
33
  end
34
34
  end
35
-
@@ -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.match(/^(Host|Port)[^a-zA-Z]/) # ignore handler's host and port options, we do our own.
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,57 +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
- @sockets = [@ready]
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
- sockets = @sockets
131
+ monitors = @monitors
132
+ selector = @selector
26
133
 
27
134
  while true
28
135
  begin
29
- ready = IO.select sockets, nil, nil, @sleep_for
136
+ ready = selector.select @sleep_for
30
137
  rescue IOError => e
31
- if sockets.any? { |socket| socket.closed? }
138
+ Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
139
+ if monitors.any? { |mon| mon.value.closed? }
32
140
  STDERR.puts "Error in select: #{e.message} (#{e.class})"
33
141
  STDERR.puts e.backtrace
34
- sockets = sockets.reject { |socket| socket.closed? }
142
+
143
+ monitors.reject! do |mon|
144
+ if mon.value.closed?
145
+ selector.deregister mon.value
146
+ true
147
+ end
148
+ end
149
+
35
150
  retry
36
151
  else
37
152
  raise
38
153
  end
39
154
  end
40
155
 
41
- if ready and reads = ready[0]
42
- reads.each do |c|
43
- if c == @ready
156
+ if ready
157
+ ready.each do |mon|
158
+ if mon.value == @ready
44
159
  @mutex.synchronize do
45
160
  case @ready.read(1)
46
161
  when "*"
47
- sockets += @input
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
48
183
  @input.clear
184
+
185
+ @timeouts.sort! { |a,b| a.value.timeout_at <=> b.value.timeout_at }
186
+ calculate_sleep
49
187
  when "c"
50
- sockets.delete_if do |s|
51
- if s == @ready
188
+ monitors.reject! do |submon|
189
+ if submon.value == @ready
52
190
  false
53
191
  else
54
- s.close
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
55
199
  true
56
200
  end
57
201
  end
@@ -60,19 +204,21 @@ module Puma
60
204
  end
61
205
  end
62
206
  else
207
+ c = mon.value
208
+
63
209
  # We have to be sure to remove it from the timeout
64
210
  # list or we'll accidentally close the socket when
65
211
  # it's in use!
66
212
  if c.timeout_at
67
213
  @mutex.synchronize do
68
- @timeouts.delete c
214
+ @timeouts.delete mon
69
215
  end
70
216
  end
71
217
 
72
218
  begin
73
219
  if c.try_to_finish
74
220
  @app_pool << c
75
- sockets.delete c
221
+ clear_monitor mon
76
222
  end
77
223
 
78
224
  # Don't report these to the lowlevel_error handler, otherwise
@@ -82,18 +228,23 @@ module Puma
82
228
  c.write_500
83
229
  c.close
84
230
 
85
- sockets.delete c
231
+ clear_monitor mon
86
232
 
87
233
  # SSL handshake failure
88
234
  rescue MiniSSL::SSLError => e
89
235
  @server.lowlevel_error(e, c.env)
90
236
 
91
237
  ssl_socket = c.io
92
- addr = ssl_socket.peeraddr.last
238
+ begin
239
+ addr = ssl_socket.peeraddr.last
240
+ rescue IOError
241
+ addr = "<unknown>"
242
+ end
243
+
93
244
  cert = ssl_socket.peercert
94
245
 
95
246
  c.close
96
- sockets.delete c
247
+ clear_monitor mon
97
248
 
98
249
  @events.ssl_error @server, addr, cert, e
99
250
 
@@ -104,7 +255,7 @@ module Puma
104
255
  c.write_400
105
256
  c.close
106
257
 
107
- sockets.delete c
258
+ clear_monitor mon
108
259
 
109
260
  @events.parse_error @server, c.env, e
110
261
  rescue StandardError => e
@@ -113,7 +264,7 @@ module Puma
113
264
  c.write_500
114
265
  c.close
115
266
 
116
- sockets.delete c
267
+ clear_monitor mon
117
268
  end
118
269
  end
119
270
  end
@@ -123,11 +274,13 @@ module Puma
123
274
  @mutex.synchronize do
124
275
  now = Time.now
125
276
 
126
- while @timeouts.first.timeout_at < now
127
- c = @timeouts.shift
277
+ while @timeouts.first.value.timeout_at < now
278
+ mon = @timeouts.shift
279
+ c = mon.value
128
280
  c.write_408 if c.in_data_phase
129
281
  c.close
130
- sockets.delete c
282
+
283
+ clear_monitor mon
131
284
 
132
285
  break if @timeouts.empty?
133
286
  end
@@ -138,6 +291,11 @@ module Puma
138
291
  end
139
292
  end
140
293
 
294
+ def clear_monitor(mon)
295
+ @selector.deregister mon.value
296
+ @monitors.delete mon
297
+ end
298
+
141
299
  public
142
300
 
143
301
  def run
@@ -162,11 +320,21 @@ module Puma
162
320
  end
163
321
  end
164
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.
165
333
  def calculate_sleep
166
334
  if @timeouts.empty?
167
335
  @sleep_for = DefaultSleepFor
168
336
  else
169
- diff = @timeouts.first.timeout_at.to_f - Time.now.to_f
337
+ diff = @timeouts.first.value.timeout_at.to_f - Time.now.to_f
170
338
 
171
339
  if diff < 0.0
172
340
  @sleep_for = 0
@@ -176,17 +344,35 @@ module Puma
176
344
  end
177
345
  end
178
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`
179
372
  def add(c)
180
373
  @mutex.synchronize do
181
374
  @input << c
182
375
  @trigger << "*"
183
-
184
- if c.timeout_at
185
- @timeouts << c
186
- @timeouts.sort! { |a,b| a.timeout_at <=> b.timeout_at }
187
-
188
- calculate_sleep
189
- end
190
376
  end
191
377
  end
192
378
 
@@ -195,6 +381,7 @@ module Puma
195
381
  begin
196
382
  @trigger << "c"
197
383
  rescue IOError
384
+ Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
198
385
  end
199
386
  end
200
387
 
@@ -202,6 +389,7 @@ module Puma
202
389
  begin
203
390
  @trigger << "!"
204
391
  rescue IOError
392
+ Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
205
393
  end
206
394
 
207
395
  @thread.join