1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
# frozen_string_literal: true
require "io/wait"
module HTTPX
#
# Implements the selector loop, where it registers and monitors "Selectable" objects.
#
# A Selectable object is an object which can calculate the **interests** (<tt>:r</tt>, <tt>:w</tt> or <tt>:rw</tt>,
# respectively "read", "write" or "read-write") it wants to monitor for, and returns (via <tt>to_io</tt> method) an
# IO object which can be passed to functions such as IO.select . More exhaustively, a Selectable **must** implement
# the following methods:
#
# state :: returns the state as a Symbol, must return <tt>:closed</tt> when disposed of resources.
# to_io :: returns the IO object.
# call :: gets called when the IO is ready.
# interests :: returns the current interests to monitor for, as described above.
# timeout :: returns nil or an integer, representing how long to wait for interests.
# handle_socket_timeout(Numeric) :: called when waiting for interest times out.
#
class Selector
extend Forwardable
READABLE = %i[rw r].freeze
WRITABLE = %i[rw w].freeze
private_constant :READABLE
private_constant :WRITABLE
def_delegator :@timers, :after
def_delegator :@selectables, :empty?
def initialize
@timers = Timers.new
@selectables = []
@is_timer_interval = false
end
def each(&blk)
@selectables.each(&blk)
end
def next_tick
catch(:jump_tick) do
timeout = next_timeout
if timeout && timeout.negative?
@timers.fire
throw(:jump_tick)
end
begin
select(timeout) do |c|
c.log(level: 2) { "[#{c.state}] selected from selector##{object_id} #{" after #{timeout} secs" unless timeout.nil?}..." }
c.call
end
@timers.fire
rescue TimeoutError => e
@timers.fire(e)
end
end
end
def terminate
# array may change during iteration
selectables = @selectables.reject(&:inflight?)
selectables.delete_if do |sel|
sel.terminate
sel.state == :closed
end
until selectables.empty?
next_tick
selectables &= @selectables
end
end
def find_resolver(options)
res = @selectables.find do |c|
c.is_a?(Resolver::Resolver) && options == c.options
end
res.multi if res
end
def each_connection(&block)
return enum_for(__method__) unless block
@selectables.each do |c|
case c
when Resolver::Resolver
c.each_connection(&block)
when Connection
yield c
end
end
end
def find_connection(request_uri, options)
each_connection.find do |connection|
connection.match?(request_uri, options)
end
end
def find_mergeable_connection(connection)
each_connection.find do |ch|
ch != connection && ch.mergeable?(connection)
end
end
# deregisters +io+ from selectables.
def deregister(io)
@selectables.delete(io)
end
# register +io+.
def register(io)
return if @selectables.include?(io)
@selectables << io
end
private
def select(interval, &block)
# do not cause an infinite loop here.
#
# this may happen if timeout calculation actually triggered an error which causes
# the connections to be reaped (such as the total timeout error) before #select
# gets called.
if @selectables.empty?
sleep(interval) if interval
return
end
# @type var r: (selectable | Array[selectable])?
# @type var w: (selectable | Array[selectable])?
r, w = nil
@selectables.delete_if do |io|
interests = io.interests
is_closed = io.state == :closed
next(is_closed) if is_closed
io.log(level: 2) do
"[#{io.state}] registering in selector##{object_id} for select (#{interests})#{" for #{interval} seconds" unless interval.nil?}"
end
if READABLE.include?(interests)
r = r.nil? ? io : (Array(r) << io)
end
if WRITABLE.include?(interests)
w = w.nil? ? io : (Array(w) << io)
end
is_closed
end
case r
when Array
w = Array(w) unless w.nil?
select_many(r, w, interval, &block)
when nil
case w
when Array
select_many(r, w, interval, &block)
when nil
return unless interval && @selectables.any?
# no selectables
# TODO: replace with sleep?
select_many(r, w, interval, &block)
else
select_one(w, :w, interval, &block)
end
else
case w
when Array
select_many(Array(r), w, interval, &block)
when nil
select_one(r, :r, interval, &block)
else
if r == w
select_one(r, :rw, interval, &block)
else
select_many(Array(r), Array(w), interval, &block)
end
end
end
end
def select_many(r, w, interval, &block)
begin
readers, writers = ::IO.select(r, w, nil, interval)
rescue IOError => e
(Array(r) + Array(w)).each do |sel|
# TODO: is there a way to cheaply find the IO associated with the error?
sel.on_error(e)
sel.force_close(true)
end
rescue StandardError => e
(Array(r) + Array(w)).each do |sel|
sel.on_error(e)
end
return
rescue Exception => e # rubocop:disable Lint/RescueException
(Array(r) + Array(w)).each do |sel|
sel.force_close(true)
end
raise e
end
if readers.nil? && writers.nil? && interval
[*r, *w].each { |io| io.handle_socket_timeout(interval) }
return
end
if writers
readers.each do |io|
yield io
# so that we don't yield 2 times
writers.delete(io)
end if readers
writers.each(&block)
else
readers.each(&block) if readers
end
end
def select_one(io, interests, interval)
begin
result =
case interests
when :r then io.to_io.wait_readable(interval)
when :w then io.to_io.wait_writable(interval)
when :rw then rw_wait(io, interval)
end
rescue IOError => e
io.on_error(e)
io.force_close(true)
rescue StandardError => e
io.on_error(e)
return
rescue Exception => e # rubocop:disable Lint/RescueException
io.force_close(true)
raise e
end
unless result || interval.nil?
io.handle_socket_timeout(interval) unless @is_timer_interval
return
end
yield io
end
def next_timeout
@is_timer_interval = false
timer_interval = @timers.wait_interval
connection_interval = @selectables.filter_map(&:timeout).min
return connection_interval unless timer_interval
if connection_interval.nil? || timer_interval <= connection_interval
@is_timer_interval = true
return timer_interval
end
connection_interval
end
if RUBY_ENGINE == "jruby"
def rw_wait(io, interval)
io.to_io.wait(interval, :read_write)
end
elsif IO.const_defined?(:READABLE)
def rw_wait(io, interval)
io.to_io.wait(IO::READABLE | IO::WRITABLE, interval)
end
else
def rw_wait(io, interval)
if interval
io.to_io.wait(interval, :read_write)
else
io.to_io.wait(:read_write)
end
end
end
end
end
|