puma 8.1.3-java → 8.2.0.pre1-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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +120 -0
  3. data/README.md +4 -3
  4. data/docs/GLOSSARY.md +160 -0
  5. data/docs/architecture.md +41 -42
  6. data/docs/binding.md +3 -0
  7. data/docs/cluster_mode.md +21 -21
  8. data/docs/control_server.md +4 -4
  9. data/docs/deployment/nginx.md +2 -0
  10. data/docs/deployment/systemd.md +15 -0
  11. data/docs/puma_support.md +12 -0
  12. data/docs/restart.md +2 -2
  13. data/docs/signals.md +3 -3
  14. data/docs/ssl.md +65 -0
  15. data/docs/upgrading/5.0-Upgrade.md +1 -1
  16. data/docs/upgrading/6.0-Upgrade.md +1 -1
  17. data/docs/upgrading/7.0-Upgrade.md +1 -1
  18. data/docs/upgrading/8.0-Upgrade.md +1 -1
  19. data/lib/puma/app/status.rb +22 -7
  20. data/lib/puma/binder.rb +0 -1
  21. data/lib/puma/cli.rb +1 -3
  22. data/lib/puma/client.rb +133 -57
  23. data/lib/puma/client_env.rb +9 -11
  24. data/lib/puma/cluster/worker.rb +0 -6
  25. data/lib/puma/cluster.rb +3 -14
  26. data/lib/puma/cluster_accept_loop_delay.rb +18 -7
  27. data/lib/puma/commonlogger.rb +3 -4
  28. data/lib/puma/configuration.rb +4 -1
  29. data/lib/puma/const.rb +5 -5
  30. data/lib/puma/control_cli.rb +5 -7
  31. data/lib/puma/detect.rb +1 -1
  32. data/lib/puma/dsl.rb +70 -14
  33. data/lib/puma/http_parser_error.rb +8 -0
  34. data/lib/puma/launcher/bundle_pruner.rb +3 -21
  35. data/lib/puma/launcher.rb +55 -25
  36. data/lib/puma/minissl/context_builder.rb +3 -2
  37. data/lib/puma/minissl.rb +12 -16
  38. data/lib/puma/plugin/systemd.rb +69 -3
  39. data/lib/puma/puma_http11.jar +0 -0
  40. data/lib/puma/rack/urlmap.rb +2 -2
  41. data/lib/puma/reactor.rb +138 -25
  42. data/lib/puma/response.rb +65 -38
  43. data/lib/puma/runner.rb +4 -1
  44. data/lib/puma/sd_notify.rb +21 -0
  45. data/lib/puma/server.rb +14 -14
  46. data/lib/puma/server_plugin_control.rb +2 -1
  47. data/lib/puma/single.rb +0 -1
  48. data/lib/puma/thread_pool.rb +87 -14
  49. data/lib/puma/util.rb +5 -16
  50. data/lib/puma.rb +22 -5
  51. data/lib/rack/handler/puma.rb +1 -5
  52. metadata +5 -2
data/lib/puma/launcher.rb CHANGED
@@ -18,6 +18,9 @@ module Puma
18
18
  class Launcher
19
19
  autoload :BundlePruner, 'puma/launcher/bundle_pruner'
20
20
 
21
+ class JRubySigterm < SignalException; end
22
+ private_constant :JRubySigterm
23
+
21
24
  # Returns an instance of Launcher
22
25
  #
23
26
  # +conf+ A Puma::Configuration object indicating how to run the server.
@@ -60,8 +63,7 @@ module Puma
60
63
 
61
64
  @events = launcher_args[:events] || Events.new
62
65
 
63
- @argv = launcher_args[:argv] || []
64
- @original_argv = @argv.dup
66
+ @original_argv = (launcher_args[:argv] || []).dup
65
67
 
66
68
  ## End minimal initialization
67
69
 
@@ -100,7 +102,6 @@ module Puma
100
102
  unsupported "worker mode not supported on #{RUBY_ENGINE} on this platform"
101
103
  end
102
104
 
103
- @environment = @options[:environment] if @options[:environment]
104
105
  set_rack_environment
105
106
 
106
107
  if clustered?
@@ -199,17 +200,18 @@ module Puma
199
200
 
200
201
  # Run the server. This blocks until the server is stopped
201
202
  def run
202
- previous_env = get_env
203
-
204
- @config.plugins.fire_starts self
205
-
206
- setup_signals
207
- set_process_title
208
-
209
- # This blocks until the server is stopped
210
- @runner.run
203
+ return run_server unless Puma.jruby? && Thread.current == Thread.main
211
204
 
212
- do_run_finished(previous_env)
205
+ Thread.handle_interrupt(JRubySigterm => :never) do
206
+ begin
207
+ Thread.handle_interrupt(JRubySigterm => :immediate) { run_server(jruby_sigterm: true) }
208
+ rescue JRubySigterm
209
+ do_graceful_stop
210
+ raise
211
+ end
212
+ end
213
+ rescue JRubySigterm
214
+ raise SignalException, "SIGTERM"
213
215
  end
214
216
 
215
217
  # Return all tcp ports the launcher may be using, TCP or SSL
@@ -244,7 +246,7 @@ module Puma
244
246
  Thread.list.each do |thread|
245
247
  name = "Thread: TID-#{thread.object_id.to_s(36)}"
246
248
  name += " #{thread['label']}" if thread['label']
247
- name += " #{thread.name}" if thread.respond_to?(:name) && thread.name
249
+ name += " #{thread.name}" if thread.name
248
250
  backtrace = thread.backtrace || ["<no backtrace available>"]
249
251
 
250
252
  yield name, backtrace
@@ -253,6 +255,20 @@ module Puma
253
255
 
254
256
  private
255
257
 
258
+ def run_server(jruby_sigterm: false)
259
+ previous_env = get_env
260
+
261
+ @config.plugins.fire_starts self
262
+
263
+ setup_signals(jruby_sigterm)
264
+ set_process_title
265
+
266
+ # This blocks until the server is stopped
267
+ @runner.run
268
+
269
+ do_run_finished(previous_env)
270
+ end
271
+
256
272
  def get_env
257
273
  if defined?(Bundler)
258
274
  env = Bundler::ORIGINAL_ENV.dup
@@ -351,7 +367,7 @@ module Puma
351
367
  end
352
368
 
353
369
  def set_process_title
354
- Process.respond_to?(:setproctitle) ? Process.setproctitle(title) : $0 = title
370
+ Process.setproctitle(title)
355
371
  end
356
372
 
357
373
  # @!attribute [r] title
@@ -425,13 +441,13 @@ module Puma
425
441
  end
426
442
  end
427
443
 
428
- def setup_signals
444
+ def setup_signals(jruby_sigterm)
429
445
  unless ENV["PUMA_SKIP_SIGUSR2"]
430
446
  begin
431
447
  Signal.trap "SIGUSR2" do
432
448
  restart
433
449
  end
434
- rescue Exception
450
+ rescue StandardError, NotImplementedError
435
451
  log "*** SIGUSR2 not implemented, signal based restart unavailable!"
436
452
  end
437
453
  end
@@ -441,19 +457,33 @@ module Puma
441
457
  Signal.trap "SIGUSR1" do
442
458
  phased_restart
443
459
  end
444
- rescue Exception
460
+ rescue StandardError, NotImplementedError
445
461
  log "*** SIGUSR1 not implemented, signal based restart unavailable!"
446
462
  end
447
463
  end
448
464
 
449
465
  begin
450
466
  Signal.trap "SIGTERM" do
451
- # Shortcut the control flow in case raise_exception_on_sigterm is true
452
- do_graceful_stop
467
+ if jruby_sigterm && @options[:raise_exception_on_sigterm]
468
+ Thread.main.raise(JRubySigterm, "SIGTERM")
469
+ else
470
+ # Shortcut the control flow in case raise_exception_on_sigterm is true
471
+ do_graceful_stop
453
472
 
454
- raise(SignalException, "SIGTERM") if @options[:raise_exception_on_sigterm]
473
+ raise(SignalException, "SIGTERM") if @options[:raise_exception_on_sigterm]
474
+ end
475
+ end
476
+
477
+ if @options[:raise_exception_on_sigterm] && Process.pid == 1
478
+ # PID 1 cannot die from its own SIGTERM (the kernel drops default-action
479
+ # signals sent to PID 1) and Ruby falls back to exit status 1.
480
+ # Convert an unhandled SIGTERM to exit 143 (128 + SIGTERM) after app
481
+ # at_exit hooks run so rescue blocks and app hooks still see SignalException.
482
+ at_exit do
483
+ exit 143 if Process.pid == 1 && $!.is_a?(SignalException) && $!.signm == "SIGTERM"
484
+ end
455
485
  end
456
- rescue Exception
486
+ rescue StandardError, NotImplementedError
457
487
  log "*** SIGTERM not implemented, signal based gracefully stopping unavailable!"
458
488
  end
459
489
 
@@ -461,7 +491,7 @@ module Puma
461
491
  Signal.trap "SIGINT" do
462
492
  stop
463
493
  end
464
- rescue Exception
494
+ rescue StandardError, NotImplementedError
465
495
  log "*** SIGINT not implemented, signal based gracefully stopping unavailable!"
466
496
  end
467
497
 
@@ -473,7 +503,7 @@ module Puma
473
503
  stop
474
504
  end
475
505
  end
476
- rescue Exception
506
+ rescue StandardError, NotImplementedError
477
507
  log "*** SIGHUP not implemented, signal based logs reopening unavailable!"
478
508
  end
479
509
 
@@ -486,7 +516,7 @@ module Puma
486
516
  end
487
517
  end
488
518
  end
489
- rescue Exception
519
+ rescue StandardError, NotImplementedError
490
520
  log "*** SIGINFO/SIGPWR not implemented, signal based backtrace unavailable!"
491
521
  end
492
522
  end
@@ -51,8 +51,6 @@ module Puma
51
51
  unless params['ca']
52
52
  log_writer.error "Please specify the SSL ca via 'ca='"
53
53
  end
54
- # needed for Puma::MiniSSL::Socket#peercert, env['puma.peercert']
55
- require 'openssl'
56
54
  end
57
55
 
58
56
  ctx.ca = params['ca'] if params['ca']
@@ -77,6 +75,9 @@ module Puma
77
75
  log_writer.error "Please specify a valid verify_mode="
78
76
  MiniSSL::VERIFY_NONE
79
77
  end
78
+
79
+ # needed for Puma::MiniSSL::Socket#peercert, env['puma.peercert']
80
+ require 'openssl' if ctx.verify_mode != MiniSSL::VERIFY_NONE
80
81
  end
81
82
 
82
83
  if params['verification_flags']
data/lib/puma/minissl.rb CHANGED
@@ -1,9 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- begin
4
- require 'io/wait' unless Puma::HAS_NATIVE_IO_WAIT
5
- rescue LoadError
6
- end
3
+ require 'io/wait' unless Puma::HAS_NATIVE_IO_WAIT
7
4
 
8
5
  require 'open3'
9
6
  # need for Puma::MiniSSL::OPENSSL constants used in `HAS_TLS1_3`
@@ -24,7 +21,6 @@ module Puma
24
21
  @socket = socket
25
22
  @engine = engine
26
23
  @peercert = nil
27
- @reuse = nil
28
24
  @write_buffer = +''
29
25
  @write_owed = 0
30
26
  @write_owed_data = nil
@@ -84,13 +80,15 @@ module Puma
84
80
  output
85
81
  end
86
82
 
87
- def read_nonblock(size, *_)
88
- # *_ is to deal with keyword args that were added
89
- # at some point (and being used in the wild)
83
+ def read_nonblock(size, *_, exception: true)
84
+ # *_ absorbs the optional positional outbuf argument passed by IO callers
90
85
  output = engine_read_all
91
86
  return output if output
92
87
 
93
- raise IO::EAGAINWaitReadable unless flush_write_buffer_nonblock
88
+ unless flush_write_buffer_nonblock
89
+ raise IO::EAGAINWaitReadable if exception
90
+ return :wait_readable
91
+ end
94
92
 
95
93
  data = @socket.read_nonblock(size, exception: false)
96
94
  if data == :wait_readable || data == :wait_writable
@@ -103,7 +101,8 @@ module Puma
103
101
  # In the meantime, we can emulate the correct behavior by
104
102
  # capturing :wait_readable & :wait_writable and raising EAGAIN
105
103
  # ourselves.
106
- raise IO::EAGAINWaitReadable
104
+ raise IO::EAGAINWaitReadable if exception
105
+ return :wait_readable
107
106
  elsif data.nil?
108
107
  raise SSLError.exception "HTTP connection?" if bad_tlsv1_3?
109
108
  return nil
@@ -115,7 +114,8 @@ module Puma
115
114
 
116
115
  queue_ciphertext
117
116
  flush_write_buffer_nonblock
118
- raise IO::EAGAINWaitReadable
117
+ raise IO::EAGAINWaitReadable if exception
118
+ :wait_readable
119
119
  end
120
120
 
121
121
  def write_pending?
@@ -237,15 +237,11 @@ module Puma
237
237
  raw = @engine.peercert
238
238
  return nil unless raw
239
239
 
240
+ require 'openssl' unless defined?(OpenSSL::X509::Certificate)
240
241
  @peercert = OpenSSL::X509::Certificate.new raw
241
242
  end
242
243
  end
243
244
 
244
- if IS_JRUBY
245
- OPENSSL_NO_SSL3 = false
246
- OPENSSL_NO_TLS1 = false
247
- end
248
-
249
245
  class Context
250
246
  attr_accessor :verify_mode
251
247
  attr_reader :no_tlsv1, :no_tlsv1_1
@@ -13,9 +13,54 @@ Puma::Plugin.create do
13
13
 
14
14
  launcher.log_writer.log "* Enabling systemd notification integration"
15
15
 
16
+ extend_timeout_usec = Puma::SdNotify.extend_timeout_usec
17
+ if extend_timeout_usec.positive?
18
+ extend_timeout_max_usec = Puma::SdNotify.extend_timeout_max_usec
19
+ extend_sleep_time = extend_timeout_usec / usec_per_sec / 2
20
+ extend_deadline = monotonic_time + extend_timeout_max_usec / usec_per_sec if extend_timeout_max_usec.positive?
21
+
22
+ pipe_r, @extend_stop = IO.pipe
23
+
24
+ launcher.log_writer.log "Extending systemd startup timeout every #{extend_sleep_time.round(1)} sec"
25
+
26
+ @extend_thread = Thread.new do
27
+ Puma.set_thread_name "systemd extend"
28
+ begin
29
+ loop do
30
+ break if IO.select([pipe_r], nil, nil, 0)
31
+
32
+ usec = extend_timeout_usec
33
+ if extend_deadline
34
+ remaining_usec = ((extend_deadline - monotonic_time) * usec_per_sec).ceil
35
+ break unless remaining_usec.positive?
36
+
37
+ usec = [usec, remaining_usec].min
38
+ end
39
+
40
+ begin
41
+ Puma::SdNotify.extend_timeout(usec)
42
+ rescue Puma::SdNotify::NotifyError
43
+ break
44
+ end
45
+ break if usec < extend_timeout_usec
46
+
47
+ break if IO.select([pipe_r], nil, nil, extend_sleep_time)
48
+ end
49
+ ensure
50
+ pipe_r.close
51
+ end
52
+ end
53
+ end
54
+
16
55
  # hook_events
17
- launcher.events.after_booted { Puma::SdNotify.ready }
18
- launcher.events.after_stopped { Puma::SdNotify.stopping }
56
+ launcher.events.after_booted do
57
+ stop_extending
58
+ Puma::SdNotify.ready
59
+ end
60
+ launcher.events.after_stopped do
61
+ stop_extending
62
+ Puma::SdNotify.stopping
63
+ end
19
64
  launcher.events.before_restart { Puma::SdNotify.reloading }
20
65
 
21
66
  # start watchdog
@@ -59,10 +104,31 @@ Puma::Plugin.create do
59
104
 
60
105
  private
61
106
 
107
+ def usec_per_sec
108
+ 1_000_000.0
109
+ end
110
+
111
+ def stop_extending
112
+ stop = @extend_stop
113
+ thread = @extend_thread
114
+ @extend_stop = nil
115
+ @extend_thread = nil
116
+
117
+ begin
118
+ stop&.close
119
+ rescue IOError
120
+ end
121
+ thread&.join
122
+ end
123
+
124
+ def monotonic_time
125
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
126
+ end
127
+
62
128
  def watchdog_sleep_time
63
129
  usec = Integer(ENV["WATCHDOG_USEC"])
64
130
 
65
- sec_f = usec / 1_000_000.0
131
+ sec_f = usec / usec_per_sec
66
132
  # "It is recommended that a daemon sends a keep-alive notification message
67
133
  # to the service manager every half of the time returned here."
68
134
  sec_f / 2
Binary file
@@ -14,8 +14,8 @@ module Puma::Rack
14
14
  # first, since they are most specific.
15
15
 
16
16
  class URLMap
17
- NEGATIVE_INFINITY = -1.0 / 0.0
18
- INFINITY = 1.0 / 0.0
17
+ NEGATIVE_INFINITY = -Float::INFINITY
18
+ INFINITY = Float::INFINITY
19
19
 
20
20
  def initialize(map = {})
21
21
  remap(map)
data/lib/puma/reactor.rb CHANGED
@@ -33,8 +33,13 @@ module Puma
33
33
 
34
34
  @selector = ::NIO::Selector.new(NIO::Selector.backends.delete(backend))
35
35
  @input = Queue.new
36
- @timeouts = []
37
- @timeout_keys = {}.compare_by_identity
36
+ @timeouts = {}.compare_by_identity
37
+ @early_timeouts = {}.compare_by_identity
38
+ @earliest = nil
39
+ @early_earliest = nil
40
+ @last_timeout_at = 0.0
41
+ @early_last_timeout_at = 0.0
42
+ @needs_early_sort = false
38
43
  @block = block
39
44
  @reactor_size = 0
40
45
  @reactor_max = 0
@@ -78,25 +83,51 @@ module Puma
78
83
  def select_loop
79
84
  begin
80
85
  until @input.closed? && @input.empty?
81
- requeue_moved_timeouts
86
+ @earliest ||= @timeouts.each_key { |k| break k } unless @timeouts.empty?
87
+ @early_earliest ||= @early_timeouts.each_key { |k| break k } unless @early_timeouts.empty?
88
+
89
+ if @needs_early_sort ||
90
+ (@earliest && @timeouts[@earliest] < @earliest.timeout_at) ||
91
+ (@early_earliest && @early_timeouts[@early_earliest] < @early_earliest.timeout_at)
92
+ if !@needs_early_sort && @early_earliest.nil? && @timeouts.size == 1
93
+ @timeouts[@earliest] = @last_timeout_at = @earliest.timeout_at
94
+ else
95
+ requeue_moved_timeouts
96
+ @earliest ||= @timeouts.each_key { |k| break k } unless @timeouts.empty?
97
+ @early_earliest ||= @early_timeouts.each_key { |k| break k } unless @early_timeouts.empty?
98
+ end
99
+ end
82
100
 
83
101
  # Wakeup any registered object that receives incoming data.
84
102
  # Block until the earliest timeout or Selector#wakeup is called.
85
- timeout = (earliest = @timeouts.first) && earliest.timeout
103
+ timeout = if @early_earliest
104
+ t1 = @earliest&.timeout
105
+ t2 = @early_earliest.timeout
106
+ t1 && t1 < t2 ? t1 : t2
107
+ else
108
+ @earliest&.timeout
109
+ end
110
+
86
111
  @selector.select(timeout) do |monitor|
87
112
  monitor.interests = interests(monitor.value) unless wakeup!(monitor.value)
88
113
  end
89
114
 
90
115
  # Wakeup all objects that timed out.
91
- timed_out = @timeouts.take_while { |client| client.timeout == 0 }
92
- timed_out.each { |client| wakeup!(client) }
116
+ unless @early_timeouts.empty?
117
+ @early_earliest ||= @early_timeouts.each_key { |k| break k }
118
+ expire_timeouts(@early_timeouts) if @early_earliest.timeout == 0
119
+ end
120
+ unless @timeouts.empty?
121
+ @earliest ||= @timeouts.each_key { |k| break k }
122
+ expire_timeouts(@timeouts) if @earliest.timeout == 0
123
+ end
93
124
 
94
125
  unless @input.empty?
95
126
  until @input.empty?
96
127
  client = @input.pop
97
128
  register(client) if client.io_ok?
98
129
  end
99
- @timeouts.sort_by! { |client| @timeout_keys[client] }
130
+ sort_early_timeouts! if @needs_early_sort
100
131
  end
101
132
  end
102
133
  rescue StandardError => e
@@ -107,21 +138,88 @@ module Puma
107
138
  end
108
139
 
109
140
  # Wakeup all remaining objects on shutdown.
110
- @timeouts.each(&@block)
141
+ @early_timeouts.each_key(&@block)
142
+ @timeouts.each_key(&@block)
111
143
  @selector.close
112
144
  end
113
145
 
146
+ def expire_timeouts(map)
147
+ timed_out = []
148
+ map.each_key do |client|
149
+ break unless client.timeout == 0
150
+ timed_out << client
151
+ end
152
+ timed_out.each { |client| wakeup!(client) }
153
+ if map.equal?(@timeouts)
154
+ @earliest = (@timeouts.each_key { |k| break k } unless @timeouts.empty?)
155
+ else
156
+ @early_earliest = (@early_timeouts.each_key { |k| break k } unless @early_timeouts.empty?)
157
+ end
158
+ end
159
+
114
160
  # Start monitoring the object.
115
161
  def register(client)
116
162
  @selector.register(client.to_io, interests(client)).value = client
117
163
  @reactor_size += 1
118
164
  @reactor_max = @reactor_size if @reactor_max < @reactor_size
119
- @timeouts << client
120
- @timeout_keys[client] = client.timeout_at
165
+ t_at = client.timeout_at
166
+ if @timeouts.empty? || t_at >= @last_timeout_at
167
+ @earliest = client if @timeouts.empty?
168
+ @last_timeout_at = t_at
169
+ @timeouts[client] = t_at
170
+ else
171
+ if @early_timeouts.empty?
172
+ @early_earliest = client
173
+ @early_last_timeout_at = t_at
174
+ elsif t_at < @early_last_timeout_at
175
+ @needs_early_sort = true
176
+ else
177
+ @early_last_timeout_at = t_at
178
+ end
179
+ @early_timeouts[client] = t_at
180
+ end
121
181
  rescue ArgumentError
122
182
  # unreadable clients raise error when processed by NIO
123
183
  end
124
184
 
185
+ def insert_timeout(client)
186
+ t_at = client.timeout_at
187
+ if @timeouts.empty? || t_at >= @last_timeout_at
188
+ @earliest = client if @timeouts.empty?
189
+ @last_timeout_at = t_at
190
+ @timeouts[client] = t_at
191
+ else
192
+ if @early_timeouts.empty?
193
+ @early_earliest = client
194
+ @early_last_timeout_at = t_at
195
+ elsif t_at < @early_last_timeout_at
196
+ @needs_early_sort = true
197
+ else
198
+ @early_last_timeout_at = t_at
199
+ end
200
+ @early_timeouts[client] = t_at
201
+ end
202
+ end
203
+
204
+ def delete_timeout(client)
205
+ if @timeouts.delete(client)
206
+ @earliest = nil if @earliest.equal?(client)
207
+ @last_timeout_at = 0.0 if @timeouts.empty?
208
+ elsif @early_timeouts.delete(client)
209
+ @early_earliest = nil if @early_earliest.equal?(client)
210
+ @early_last_timeout_at = 0.0 if @early_timeouts.empty?
211
+ end
212
+ end
213
+
214
+ def sort_early_timeouts!
215
+ sorted = @early_timeouts.sort_by { |_client, t_at| t_at }
216
+ @early_timeouts.clear
217
+ sorted.each { |c, t_at| @early_timeouts[c] = t_at }
218
+ @early_earliest = sorted.first&.first
219
+ @early_last_timeout_at = sorted.last&.last || 0.0
220
+ @needs_early_sort = false
221
+ end
222
+
125
223
  def interests(client)
126
224
  client.write_pending? ? :w : :r
127
225
  end
@@ -132,32 +230,47 @@ module Puma
132
230
  if @block.call client
133
231
  @selector.deregister client.to_io
134
232
  @reactor_size -= 1
135
- @timeouts.delete client
136
- @timeout_keys.delete client
233
+ if @timeouts.delete(client)
234
+ @earliest = nil if @earliest.equal?(client)
235
+ @last_timeout_at = 0.0 if @timeouts.empty?
236
+ elsif @early_timeouts.delete(client)
237
+ @early_earliest = nil if @early_earliest.equal?(client)
238
+ @early_last_timeout_at = 0.0 if @early_timeouts.empty?
239
+ end
137
240
  true
138
241
  else
139
- if client.timeout_at < @timeout_keys[client]
140
- @timeouts.delete client
141
- requeue client
242
+ queued_at = @timeouts[client] || @early_timeouts[client]
243
+ if queued_at && client.timeout_at < queued_at
244
+ delete_timeout(client)
245
+ insert_timeout(client)
246
+ sort_early_timeouts! if @needs_early_sort
142
247
  end
143
248
  false
144
249
  end
145
250
  end
146
251
 
147
- # `@timeouts` is sorted by the timeout each client had when it was queued.
252
+ # Timeouts are ordered by the timeout each client had when it was queued.
148
253
  # Timeouts that moved earlier are requeued in `wakeup!`. Timeouts that moved
149
- # later are requeued here, once the client reaches the front.
254
+ # later are requeued here, once the client reaches the front of its lane.
150
255
  def requeue_moved_timeouts
151
- while (client = @timeouts.first) && @timeout_keys[client] < client.timeout_at
152
- @timeouts.shift
153
- requeue client
256
+ sort_early_timeouts! if @needs_early_sort
257
+
258
+ unless @timeouts.empty?
259
+ while (client = (@earliest ||= @timeouts.each_key { |k| break k })) && @timeouts[client] < client.timeout_at
260
+ delete_timeout(client)
261
+ insert_timeout(client)
262
+ break if @timeouts.empty?
263
+ end
154
264
  end
155
- end
156
265
 
157
- def requeue(client)
158
- key = @timeout_keys[client] = client.timeout_at
159
- index = @timeouts.bsearch_index { |c| @timeout_keys[c] > key } || @timeouts.size
160
- @timeouts.insert index, client
266
+ unless @early_timeouts.empty?
267
+ while (client = (@early_earliest ||= @early_timeouts.each_key { |k| break k })) && @early_timeouts[client] < client.timeout_at
268
+ delete_timeout(client)
269
+ insert_timeout(client)
270
+ break if @early_timeouts.empty?
271
+ end
272
+ sort_early_timeouts! if @needs_early_sort
273
+ end
161
274
  end
162
275
  end
163
276
  end