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  
DNS_PORT = 53  
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
50 def initialize(options)
51   @options = Options.new(options)
52   @ns_index = 0
53   @resolver_options = Resolver::Options.new(DEFAULTS.merge(@options.resolver_options || {}))
54   @nameserver = @resolver_options.nameserver
55   @_timeouts = Array(@resolver_options.timeouts)
56   @timeouts = Hash.new { |timeouts, host| timeouts[host] = @_timeouts.dup }
57   @_record_types = Hash.new { |types, host| types[host] = @resolver_options.record_types.dup }
58   @connections = []
59   @queries = {}
60   @read_buffer = Buffer.new(@resolver_options.packet_size)
61   @write_buffer = Buffer.new(@resolver_options.packet_size)
62   @state = :idle
63 end

Public Instance methods

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