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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/httpx/pool.rb', line 50 def close(connections = @connections) return if connections.empty? connections = connections.reject(&:inflight?) connections.each(&:terminate) next_tick until connections.none? { |c| c.state != :idle && @connections.include?(c) } # close resolvers outstanding_connections = @connections resolver_connections = @resolvers.each_value.flat_map(&:connections).compact outstanding_connections -= resolver_connections return unless outstanding_connections.empty? @resolvers.each_value do |resolver| resolver.close unless resolver.closed? end # for https resolver resolver_connections.each(&:terminate) next_tick until resolver_connections.none? { |c| c.state != :idle && @connections.include?(c) } end |
#deactivate(connections) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/httpx/pool.rb', line 100 def deactivate(connections) connections.each do |connection| connection.deactivate deselect_connection(connection) if connection.state == :inactive end end |
#empty? ⇒ Boolean
22 23 24 |
# File 'lib/httpx/pool.rb', line 22 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.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/httpx/pool.rb', line 111 def find_connection(uri, ) conn = @connections.find do |connection| connection.match?(uri, ) end return unless conn case conn.state when :closed conn.idling select_connection(conn) when :closing conn.once(:close) do conn.idling select_connection(conn) end end conn end |
#init_connection(connection, _options) ⇒ Object
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 |
# File 'lib/httpx/pool.rb', line 72 def init_connection(connection, ) connection.timers = @timers connection.on(:activate) do select_connection(connection) end connection.on(:exhausted) do case connection.state when :closed connection.idling @connections << connection select_connection(connection) when :closing connection.once(:close) do connection.idling @connections << connection select_connection(connection) end end end connection.on(:close) do unregister_connection(connection) end connection.on(:terminate) do unregister_connection(connection, true) end resolve_connection(connection) unless connection.family end |
#next_tick ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/httpx/pool.rb', line 26 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(&:force_reset) raise end |