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
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.new() @timeout = .timeout resolver_type = .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, ) @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, **) channel = Channel.by(uri, .merge()) 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 |
#close ⇒ Object
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_tick ⇒ Object
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
23 24 25 |
# File 'lib/httpx/connection.rb', line 23 def running? !@channels.empty? end |