Class: HTTPX::Connection
- Inherits:
-
Object
- Object
- HTTPX::Connection
- Defined in:
- lib/httpx/connection.rb
Instance Method Summary collapse
- #build_channel(uri, **options) ⇒ Object
- #close ⇒ Object
-
#find_channel(uri) ⇒ Object
opens a channel to the IP reachable through
uri. -
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #next_tick ⇒ Object
- #running? ⇒ Boolean
Constructor Details
#initialize(options) ⇒ Connection
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/httpx/connection.rb', line 9 def initialize() = Options.new() @timeout = .timeout resolver_type = .resolver_class resolver_type = Resolver.registry(resolver_type) if resolver_type.is_a?(Symbol) @selector = Selector.new @channels = [] @resolver = resolver_type.new(self, ) @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
50 51 52 53 54 55 56 57 58 |
# File 'lib/httpx/connection.rb', line 50 def build_channel(uri, **) channel = Channel.by(uri, .merge()) resolve_channel(channel) channel.once(:unreachable) do @resolver.uncache(channel) resolve_channel(channel) end channel end |
#close ⇒ Object
44 45 46 47 48 |
# File 'lib/httpx/connection.rb', line 44 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.
64 65 66 67 68 |
# File 'lib/httpx/connection.rb', line 64 def find_channel(uri) @channels.find do |channel| channel.match?(uri) end end |
#next_tick ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/httpx/connection.rb', line 26 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, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE => ex @channels.each do |ch| ch.emit(:error, ex) end end |
#running? ⇒ Boolean
22 23 24 |
# File 'lib/httpx/connection.rb', line 22 def running? !@channels.empty? end |