lib/httpx/selector.rb in httpx-0.17.0 vs lib/httpx/selector.rb in httpx-0.18.0
- old
+ new
@@ -1,31 +1,17 @@
# frozen_string_literal: true
require "io/wait"
-module IOExtensions
- refine IO do
- # provides a fallback for rubies where IO#wait isn't implemented,
- # but IO#wait_readable and IO#wait_writable are.
- def wait(timeout = nil, _mode = :read_write)
- r, w = IO.select([self], [self], nil, timeout)
-
- return unless r || w
-
- self
- end
- end
-end
-
class HTTPX::Selector
READABLE = %i[rw r].freeze
WRITABLE = %i[rw w].freeze
private_constant :READABLE
private_constant :WRITABLE
- using IOExtensions unless IO.method_defined?(:wait) && IO.instance_method(:wait).arity == 2
+ using HTTPX::IOExtensions
def initialize
@selectables = []
end
@@ -56,22 +42,24 @@
w = nil
selectables = @selectables
@selectables = []
- selectables.each do |io|
+ selectables.delete_if do |io|
interests = io.interests
(r ||= []) << io if READABLE.include?(interests)
(w ||= []) << io if WRITABLE.include?(interests)
+
+ io.state == :closed
end
if @selectables.empty?
@selectables = selectables
# do not run event loop if there's nothing to wait on.
# this might happen if connect failed and connection was unregistered.
- return if (!r || r.empty?) && (!w || w.empty?)
+ return if (!r || r.empty?) && (!w || w.empty?) && !selectables.empty?
break
else
@selectables.concat(selectables)
end