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.



8
9
10
11
12
13
# File 'lib/httpx/connection.rb', line 8

def initialize(options)
  @options = Options.new(options)
  @timeout = options.timeout
  @selector = Selector.new
  @channels = []
end

Instance Method Details

#build_channel(uri, **options) ⇒ Object



41
42
43
44
45
# File 'lib/httpx/connection.rb', line 41

def build_channel(uri, **options)
  channel = Channel.by(uri, @options.merge(options))
  register_channel(channel)
  channel
end

#closeObject



36
37
38
39
# File 'lib/httpx/connection.rb', line 36

def close
  @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.



51
52
53
54
55
# File 'lib/httpx/connection.rb', line 51

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

#next_tickObject



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

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

#running?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/httpx/connection.rb', line 15

def running?
  !@channels.empty?
end