class HTTPX::Resolver::Native

  1. lib/httpx/resolver/native.rb
Superclass: Object

Methods

Public Class

  1. new

Public Instance

  1. <<
  2. call
  3. close
  4. closed?
  5. interests
  6. timeout
  7. to_io

Included modules

  1. Resolver::ResolverMixin

Constants

DEFAULTS = if RUBY_VERSION < "2.2" { **Resolv::DNS::Config.default_config_hash, packet_size: 512, timeouts: RESOLVE_TIMEOUT, record_types: RECORD_TYPES.keys, }.freeze else { nameserver: nil, **Resolv::DNS::Config.default_config_hash, packet_size: 512, timeouts: RESOLVE_TIMEOUT, record_types: RECORD_TYPES.keys, }.freeze end  

:nocov:

DNS_PORT = 53  

:nocov:

RECORD_TYPES = { "A" => Resolv::DNS::Resource::IN::A, "AAAA" => Resolv::DNS::Resource::IN::AAAA, }.freeze  
RESOLVE_TIMEOUT = 5  

Public Class methods

new (options)
[show source]
   # File lib/httpx/resolver/native.rb
52 def initialize(options)
53   @options = Options.new(options)
54   @ns_index = 0
55   @resolver_options = Resolver::Options.new(DEFAULTS.merge(@options.resolver_options || {}))
56   @nameserver = @resolver_options.nameserver
57   @_timeouts = Array(@resolver_options.timeouts)
58   @timeouts = Hash.new { |timeouts, host| timeouts[host] = @_timeouts.dup }
59   @_record_types = Hash.new { |types, host| types[host] = @resolver_options.record_types.dup }
60   @connections = []
61   @queries = {}
62   @read_buffer = "".b
63   @write_buffer = Buffer.new(@resolver_options.packet_size)
64   @state = :idle
65 end

Public Instance methods

<< (connection)
[show source]
    # File lib/httpx/resolver/native.rb
113 def <<(connection)
114   return if early_resolve(connection)
115 
116   if @nameserver.nil?
117     ex = ResolveError.new("Can't resolve #{connection.origin.host}: no nameserver")
118     ex.set_backtrace(caller)
119     emit(:error, connection, ex)
120   else
121     @connections << connection
122     resolve
123   end
124 end
call ()
[show source]
    # File lib/httpx/resolver/native.rb
 87 def call
 88   case @state
 89   when :open
 90     consume
 91   end
 92   nil
 93 rescue Errno::EHOSTUNREACH => e
 94   @ns_index += 1
 95   if @ns_index < @nameserver.size
 96     log(label: "resolver: ") do
 97       # :nocov:
 98       "failed resolving on nameserver #{@nameserver[@ns_index - 1]} (#{e.message})"
 99       # :nocov:
100     end
101     transition(:idle)
102   else
103     handle_error(e)
104   end
105 rescue NativeResolveError => e
106   handle_error(e)
107 end
close ()
[show source]
   # File lib/httpx/resolver/native.rb
67 def close
68   transition(:closed)
69 end
closed? ()
[show source]
   # File lib/httpx/resolver/native.rb
71 def closed?
72   @state == :closed
73 end
interests ()
[show source]
    # File lib/httpx/resolver/native.rb
109 def interests
110   !@write_buffer.empty? || @queries.empty? ? :w : :r
111 end
timeout ()
[show source]
    # File lib/httpx/resolver/native.rb
126 def timeout
127   return if @connections.empty?
128 
129   @start_timeout = Process.clock_gettime(Process::CLOCK_MONOTONIC)
130   hosts = @queries.keys
131   @timeouts.values_at(*hosts).reject(&:empty?).map(&:first).min
132 end
to_io ()
[show source]
   # File lib/httpx/resolver/native.rb
75 def to_io
76   case @state
77   when :idle
78     transition(:open)
79   when :closed
80     transition(:idle)
81     transition(:open)
82   end
83   resolve if @queries.empty?
84   @io.to_io
85 end