Class: HTTPX::Pool
Instance Method Summary collapse
- #close(connections = @connections) ⇒ Object
- #deactivate(connections) ⇒ Object
- #empty? ⇒ Boolean
-
#find_connection(uri, options) ⇒ Object
opens a connection to the IP reachable through
uri
. - #init_connection(connection, _options) ⇒ Object
-
#initialize ⇒ Pool
constructor
A new instance of Pool.
- #next_tick ⇒ Object
Constructor Details
Instance Method Details
#close(connections = @connections) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/httpx/pool.rb', line 52 def close(connections = @connections) return if connections.empty? @timers.cancel connections = connections.reject(&:inflight?) connections.each(&:close) next_tick until connections.none? { |c| c.state != :idle && @connections.include?(c) } @resolvers.each_value do |resolver| resolver.close unless resolver.closed? end if @connections.empty? end |
#deactivate(connections) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/httpx/pool.rb', line 75 def deactivate(connections) connections.each do |connection| connection.deactivate deselect_connection(connection) if connection.state == :inactive end end |
#empty? ⇒ Boolean
24 25 26 |
# File 'lib/httpx/pool.rb', line 24 def empty? @connections.empty? end |
#find_connection(uri, options) ⇒ Object
opens a connection to the IP reachable through uri
. Many hostnames are reachable through the same IP, so we try to maximize pipelining by opening as few connections as possible.
86 87 88 89 90 |
# File 'lib/httpx/pool.rb', line 86 def find_connection(uri, ) @connections.find do |connection| connection.match?(uri, ) end end |
#init_connection(connection, _options) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/httpx/pool.rb', line 64 def init_connection(connection, ) resolve_connection(connection) connection.timers = @timers connection.on(:open) do @connected_connections += 1 end connection.on(:activate) do select_connection(connection) end end |
#next_tick ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/httpx/pool.rb', line 28 def next_tick catch(:jump_tick) do timeout = next_timeout if timeout && timeout.negative? @timers.fire throw(:jump_tick) end begin @selector.select(timeout, &:call) @timers.fire rescue TimeoutError => e @timers.fire(e) end end rescue StandardError => e @connections.each do |connection| connection.emit(:error, e) end rescue Exception # rubocop:disable Lint/RescueException @connections.each(&:reset) raise end |