class HTTPX::Selector

  1. lib/httpx/selector.rb
Superclass: Object

Methods

Public Class

  1. new

Public Instance

  1. close
  2. deregister
  3. register
  4. select
  5. wakeup

Classes and Modules

  1. HTTPX::Selector::Monitor

Public Class methods

new ()
[show source]
   # File lib/httpx/selector.rb
50 def initialize
51   @selectables = {}
52   @__r__, @__w__ = IO.pipe
53   @closed = false
54 end

Public Instance methods

close ()

Closes the selector.

[show source]
    # File lib/httpx/selector.rb
121 def close
122   return if @closed
123 
124   @__r__.close
125   @__w__.close
126 rescue IOError
127 ensure
128   @closed = true
129 end
deregister (io)

deregisters io from selectables.

[show source]
   # File lib/httpx/selector.rb
57 def deregister(io)
58   monitor = @selectables.delete(io)
59   monitor.close(false) if monitor
60 end
register (io, interests)

register io for interests events.

[show source]
   # File lib/httpx/selector.rb
63 def register(io, interests)
64   monitor = @selectables[io]
65   if monitor
66     monitor.interests = interests
67   else
68     monitor = Monitor.new(io, interests, self)
69     @selectables[io] = monitor
70   end
71   monitor
72 end
select (interval)

waits for read/write events for interval. Yields for monitors of selected IO objects.

[show source]
    # File lib/httpx/selector.rb
 77 def select(interval)
 78   begin
 79     r = [@__r__]
 80     w = []
 81 
 82     @selectables.each do |io, monitor|
 83       r << io if monitor.interests == :r || monitor.interests == :rw
 84       w << io if monitor.interests == :w || monitor.interests == :rw
 85       monitor.readiness = nil
 86     end
 87 
 88     readers, writers = IO.select(r, w, nil, interval)
 89 
 90     raise HTTPX::TimeoutError.new(interval, "timed out while waiting on select") if readers.nil? && writers.nil?
 91   rescue IOError, SystemCallError
 92     @selectables.reject! { |io, _| io.closed? }
 93     retry
 94   end
 95 
 96   readers.each do |io|
 97     if io == @__r__
 98       # clean up wakeups
 99       @__r__.read(@__r__.stat.size)
100     else
101       monitor = io.closed? ? @selectables.delete(io) : @selectables[io]
102       next unless monitor
103 
104       monitor.readiness = writers.delete(io) ? :rw : :r
105       yield monitor
106     end
107   end if readers
108 
109   writers.each do |io|
110     monitor = io.closed? ? @selectables.delete(io) : @selectables[io]
111     next unless monitor
112 
113     # don't double run this, the last iteration might have run this task already
114     monitor.readiness = :w
115     yield monitor
116   end if writers
117 end
wakeup ()

interrupts the select call.

[show source]
    # File lib/httpx/selector.rb
132 def wakeup
133   @__w__.write_nonblock("\0", exception: false)
134 end