Class: HTTPX::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/pool.rb

Instance Method Summary collapse

Constructor Details

#initializePool

Returns a new instance of Pool.



9
10
11
12
13
14
15
# File 'lib/httpx/pool.rb', line 9

def initialize
  @resolvers = {}
  @_resolver_monitors = {}
  @selector = Selector.new
  @connections = []
  @connected_connections = 0
end

Instance Method Details

#close(connections = @connections) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/httpx/pool.rb', line 36

def close(connections = @connections)
  connections = connections.reject(&:inflight?)
  connections.each(&:close)
  next_tick until connections.none? { |c| @connections.include?(c) }
  @resolvers.each_value do |resolver|
    resolver.close unless resolver.closed?
  end if @connections.empty?
end

#empty?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/httpx/pool.rb', line 17

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.



61
62
63
64
65
# File 'lib/httpx/pool.rb', line 61

def find_connection(uri, options)
  @connections.find do |connection|
    connection.match?(uri, options)
  end
end

#init_connection(connection, _options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/httpx/pool.rb', line 45

def init_connection(connection, _options)
  resolve_connection(connection)
  connection.on(:open) do
    @connected_connections += 1
  end
  connection.on(:unreachable) do
    resolver = find_resolver_for(connection)
    resolver.uncache(connection) if resolver
    resolve_connection(connection)
  end
end

#next_tick(timeout = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/httpx/pool.rb', line 21

def next_tick(timeout = nil)
  catch(:jump_tick) do
    tout = timeout.total_timeout if timeout

    @selector.select(next_timeout || tout) do |monitor|
      monitor.io.call
      monitor.interests = monitor.io.interests
    end
  end
rescue StandardError => ex
  @connections.each do |connection|
    connection.emit(:error, ex)
  end
end