Class: HTTPX::Pool
- Inherits:
-
Object
- Object
- HTTPX::Pool
- Defined in:
- lib/httpx/pool.rb,
sig/pool.rbs
Constant Summary collapse
- POOL_TIMEOUT =
5
Instance Method Summary collapse
- #checkin_connection(connection) ⇒ void
- #checkin_resolver(resolver) ⇒ void
-
#checkout_connection(uri, options) ⇒ Connection
opens a connection to the IP reachable through
uri. - #checkout_mergeable_connection(connection) ⇒ Connection?
- #checkout_resolver(options) ⇒ resolver_manager
-
#inspect ⇒ Object
simplecov:disable.
-
#pop_connection ⇒ Connection?
connections returned by this function are not expected to return to the connection pool.
- #reset_resolvers ⇒ void
Instance Method Details
#checkin_connection(connection) ⇒ void
This method returns an undefined value.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/httpx/pool.rb', line 110 def checkin_connection(connection) return if connection..io @connection_mtx.synchronize do if connection.coalesced? || connection.state == :idle # when connections coalesce drop_connection(connection) return end @connections << connection @max_connections_cond.signal @origin_conds[connection.origin.to_s].signal end end |
#checkin_resolver(resolver) ⇒ void
This method returns an undefined value.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/httpx/pool.rb', line 156 def checkin_resolver(resolver) if resolver.is_a?(Resolver::Multi) resolver_class = resolver.resolvers.first.class else resolver_class = resolver.class resolver = resolver.multi end # a multi requires all sub-resolvers being closed in order to be # correctly checked back in. return unless resolver.closed? @resolver_mtx.synchronize do resolvers = @resolvers[resolver_class] resolvers << resolver unless resolvers.include?(resolver) end end |
#checkout_connection(uri, options) ⇒ Connection
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.
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 |
# File 'lib/httpx/pool.rb', line 46 def checkout_connection(uri, ) return checkout_new_connection(uri, ) if .io @connection_mtx.synchronize do acquire_connection(uri, ) || begin if @connections_counter == @max_connections # this takes precedence over per-origin expires_at = Utils.now + @pool_timeout loop do @max_connections_cond.wait(@connection_mtx, @pool_timeout) if (conn = acquire_connection(uri, )) return conn end # if one can afford to create a new connection, do it break unless @connections_counter == @max_connections # if no matching usable connection was found, the pool will make room and drop a closed connection. if (conn = @connections.find { |c| c.state == :closed }) drop_connection(conn) break end # happens when a condition was signalled, but another thread snatched the available connection before # context was passed back here. next if Utils.now < expires_at raise PoolTimeoutError.new(@pool_timeout, "Timed out after #{@pool_timeout} seconds while waiting for a connection") end end if @origin_counters[uri.origin] == @max_connections_per_origin expires_at = Utils.now + @pool_timeout loop do @origin_conds[uri.origin].wait(@connection_mtx, @pool_timeout) if (conn = acquire_connection(uri, )) return conn end # happens when a condition was signalled, but another thread snatched the available connection before # context was passed back here. next if Utils.now < expires_at raise(PoolTimeoutError.new(@pool_timeout, "Timed out after #{@pool_timeout} seconds while waiting for a connection to #{uri.origin}")) end end @connections_counter += 1 @origin_counters[uri.origin] += 1 checkout_new_connection(uri, ) end end end |
#checkout_mergeable_connection(connection) ⇒ Connection?
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/httpx/pool.rb', line 128 def checkout_mergeable_connection(connection) return if connection..io @connection_mtx.synchronize do idx = @connections.find_index do |ch| ch != connection && ch.mergeable?(connection) end @connections.delete_at(idx) if idx end end |
#checkout_resolver(options) ⇒ resolver_manager
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/httpx/pool.rb', line 143 def checkout_resolver() resolver_type = .resolver_class @resolver_mtx.synchronize do resolvers = @resolvers[resolver_type] idx = resolvers.find_index do |res| res..() end resolvers.delete_at(idx) if idx end || checkout_new_resolver(resolver_type, ) end |
#inspect ⇒ Object
simplecov:disable
177 178 179 180 181 182 183 |
# File 'lib/httpx/pool.rb', line 177 def inspect "#<#{self.class}:#{object_id} " \ "@max_connections=#{@max_connections} " \ "@max_connections_per_origin=#{@max_connections_per_origin} " \ "@pool_timeout=#{@pool_timeout} " \ "@connections=#{@connections.size}>" end |
#pop_connection ⇒ Connection?
connections returned by this function are not expected to return to the connection pool.
36 37 38 39 40 |
# File 'lib/httpx/pool.rb', line 36 def pop_connection @connection_mtx.synchronize do drop_connection end end |
#reset_resolvers ⇒ void
This method returns an undefined value.
139 140 141 |
# File 'lib/httpx/pool.rb', line 139 def reset_resolvers @resolver_mtx.synchronize { @resolvers.clear } end |