Class: HTTPX::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/httpx/connection.rb', line 9

def initialize(options)
  @options = Options.new(options)
  @timeout = options.timeout
  resolver_type = @options.resolver_class
  resolver_type = Resolver.registry(resolver_type) if resolver_type.is_a?(Symbol)
  @selector = Selector.new
  @channels = []
  @connected_channels = 0
  @resolver = resolver_type.new(self, @options)
  @resolver.on(:resolve, &method(:on_resolver_channel))
  @resolver.on(:error, &method(:on_resolver_error))
  @resolver.on(:close, &method(:on_resolver_close))
end

Instance Method Details

#build_channel(uri, **options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/httpx/connection.rb', line 54

def build_channel(uri, **options)
  channel = Channel.by(uri, @options.merge(options))
  resolve_channel(channel)
  channel.on(:open) do
    @connected_channels += 1
    @timeout.transition(:open) if @channels.size == @connected_channels
  end
  channel.on(:reset) do
    @timeout.transition(:idle)
  end
  channel.on(:unreachable) do
    @resolver.uncache(channel)
    resolve_channel(channel)
  end
  channel
end

#closeObject



48
49
50
51
52
# File 'lib/httpx/connection.rb', line 48

def close
  @resolver.close unless @resolver.closed?
  @channels.each(&:close)
  next_tick until @channels.empty?
end

#find_channel(uri) ⇒ Object

opens a channel to the IP reachable through uri. Many hostnames are reachable through the same IP, so we try to maximize pipelining by opening as few channels as possible.



75
76
77
78
79
# File 'lib/httpx/connection.rb', line 75

def find_channel(uri)
  @channels.find do |channel|
    channel.match?(uri)
  end
end

#next_tickObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/httpx/connection.rb', line 27

def next_tick
  catch(:jump_tick) do
    @selector.select(next_timeout) do |monitor|
      if (channel = monitor.value)
        channel.call
      end
      monitor.interests = channel.interests
    end
  end
rescue TimeoutError => timeout_error
  @channels.each do |ch|
    ch.handle_timeout_error(timeout_error)
  end
rescue Errno::ECONNRESET,
       Errno::ECONNABORTED,
       Errno::EPIPE => ex
  @channels.each do |ch|
    ch.emit(:error, ex)
  end
end

#running?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/httpx/connection.rb', line 23

def running?
  !@channels.empty?
end