Class: HTTPX::Selector
- Inherits:
-
Object
- Object
- HTTPX::Selector
- Extended by:
- Forwardable
- Defined in:
- lib/httpx/selector.rb
Overview
Implements the selector loop, where it registers and monitors “Selectable” objects.
A Selectable object is an object which can calculate the interests (:r, :w or :rw, respectively “read”, “write” or “read-write”) it wants to monitor for, and returns (via to_io 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
:closedwhen 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.
Instance Method Summary collapse
-
#deregister(io) ⇒ Object
deregisters
iofrom selectables. - #each(&blk) ⇒ Object
- #each_connection(&block) ⇒ Object
- #find_connection(request_uri, options) ⇒ Object
- #find_mergeable_connection(connection) ⇒ Object
- #find_resolver(options) ⇒ Object
-
#initialize ⇒ Selector
constructor
A new instance of Selector.
- #next_tick ⇒ Object
-
#register(io) ⇒ Object
register
io. - #terminate ⇒ Object
Constructor Details
Instance Method Details
#deregister(io) ⇒ Object
deregisters io from selectables.
116 117 118 |
# File 'lib/httpx/selector.rb', line 116 def deregister(io) @selectables.delete(io) end |
#each(&blk) ⇒ Object
40 41 42 |
# File 'lib/httpx/selector.rb', line 40 def each(&blk) @selectables.each(&blk) end |
#each_connection(&block) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/httpx/selector.rb', line 90 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 |
#find_connection(request_uri, options) ⇒ Object
103 104 105 106 107 |
# File 'lib/httpx/selector.rb', line 103 def find_connection(request_uri, ) each_connection.find do |connection| connection.match?(request_uri, ) end end |
#find_mergeable_connection(connection) ⇒ Object
109 110 111 112 113 |
# File 'lib/httpx/selector.rb', line 109 def find_mergeable_connection(connection) each_connection.find do |ch| ch != connection && ch.mergeable?(connection) end end |
#find_resolver(options) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/httpx/selector.rb', line 82 def find_resolver() res = @selectables.find do |c| c.is_a?(Resolver::Resolver) && == c. end res.multi if res end |
#next_tick ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/httpx/selector.rb', line 44 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 |
#register(io) ⇒ Object
register io.
121 122 123 124 125 |
# File 'lib/httpx/selector.rb', line 121 def register(io) return if @selectables.include?(io) @selectables << io end |
#terminate ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/httpx/selector.rb', line 66 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 |