class HTTPX::Pool

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

Methods

Public Class

  1. new

Public Instance

  1. close
  2. deactivate
  3. empty?
  4. find_connection
  5. init_connection
  6. next_tick
  7. wrap

Public Class methods

new()
[show source]
   # File lib/httpx/pool.rb
15 def initialize
16   @resolvers = {}
17   @timers = Timers.new
18   @selector = Selector.new
19   @connections = []
20 end

Public Instance methods

close(connections = @connections)
[show source]
   # File lib/httpx/pool.rb
61 def close(connections = @connections)
62   return if connections.empty?
63 
64   connections = connections.reject(&:inflight?)
65   connections.each(&:terminate)
66   next_tick until connections.none? { |c| c.state != :idle && @connections.include?(c) }
67 
68   # close resolvers
69   outstanding_connections = @connections
70   resolver_connections = @resolvers.each_value.flat_map(&:connections).compact
71   outstanding_connections -= resolver_connections
72 
73   return unless outstanding_connections.empty?
74 
75   @resolvers.each_value do |resolver|
76     resolver.close unless resolver.closed?
77   end
78   # for https resolver
79   resolver_connections.each(&:terminate)
80   next_tick until resolver_connections.none? { |c| c.state != :idle && @connections.include?(c) }
81 end
deactivate(*connections)
[show source]
    # File lib/httpx/pool.rb
111 def deactivate(*connections)
112   connections.each do |connection|
113     connection.deactivate
114     deselect_connection(connection) if connection.state == :inactive
115   end
116 end
empty?()
[show source]
   # File lib/httpx/pool.rb
33 def empty?
34   @connections.empty?
35 end
find_connection(uri, options)

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.

[show source]
    # File lib/httpx/pool.rb
122 def find_connection(uri, options)
123   conn = @connections.find do |connection|
124     connection.match?(uri, options)
125   end
126 
127   return unless conn
128 
129   case conn.state
130   when :closed
131     conn.idling
132     select_connection(conn)
133   when :closing
134     conn.once(:close) do
135       conn.idling
136       select_connection(conn)
137     end
138   end
139 
140   conn
141 end
init_connection(connection, _options)
[show source]
    # File lib/httpx/pool.rb
 83 def init_connection(connection, _options)
 84   connection.timers = @timers
 85   connection.on(:activate) do
 86     select_connection(connection)
 87   end
 88   connection.on(:exhausted) do
 89     case connection.state
 90     when :closed
 91       connection.idling
 92       @connections << connection
 93       select_connection(connection)
 94     when :closing
 95       connection.once(:close) do
 96         connection.idling
 97         @connections << connection
 98         select_connection(connection)
 99       end
100     end
101   end
102   connection.on(:close) do
103     unregister_connection(connection)
104   end
105   connection.on(:terminate) do
106     unregister_connection(connection, true)
107   end
108   resolve_connection(connection) unless connection.family
109 end
next_tick()
[show source]
   # File lib/httpx/pool.rb
37 def next_tick
38   catch(:jump_tick) do
39     timeout = next_timeout
40     if timeout && timeout.negative?
41       @timers.fire
42       throw(:jump_tick)
43     end
44 
45     begin
46       @selector.select(timeout, &:call)
47       @timers.fire
48     rescue TimeoutError => e
49       @timers.fire(e)
50     end
51   end
52 rescue StandardError => e
53   @connections.each do |connection|
54     connection.emit(:error, e)
55   end
56 rescue Exception # rubocop:disable Lint/RescueException
57   @connections.each(&:force_reset)
58   raise
59 end
wrap()
[show source]
   # File lib/httpx/pool.rb
22 def wrap
23   connections = @connections
24   @connections = []
25 
26   begin
27     yield self
28   ensure
29     @connections.unshift(*connections)
30   end
31 end