class HTTPX::Resolver::Native

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

Implements a pure ruby name resolver, which abides by the Selectable API. It delegates DNS payload encoding/decoding to the resolv stlid gem.

Constants

DEFAULTS = { nameserver: nil, **Resolv::DNS::Config.default_config_hash, packet_size: 512, timeouts: Resolver::RESOLVE_TIMEOUT, }.freeze  
DNS_PORT = 53  

Attributes

state [R]

Public Class methods

new(family, options)
[show source]
   # File lib/httpx/resolver/native.rb
28 def initialize(family, options)
29   super
30   @ns_index = 0
31   @resolver_options = DEFAULTS.merge(@options.resolver_options)
32   @socket_type = @resolver_options.fetch(:socket_type, :udp)
33   @nameserver = if (nameserver = @resolver_options[:nameserver])
34     nameserver = nameserver[family] if nameserver.is_a?(Hash)
35     Array(nameserver)
36   end
37   @ndots = @resolver_options.fetch(:ndots, 1)
38   @search = Array(@resolver_options[:search]).map { |srch| srch.scan(/[^.]+/) }
39   @_timeouts = Array(@resolver_options[:timeouts])
40   @timeouts = Hash.new { |timeouts, host| timeouts[host] = @_timeouts.dup }
41   @name = nil
42   @queries = {}
43   @read_buffer = "".b
44   @write_buffer = Buffer.new(@resolver_options[:packet_size])
45   @state = :idle
46   @timer = nil
47 end

Public Instance methods

<<(connection)
[show source]
    # File lib/httpx/resolver/native.rb
 95 def <<(connection)
 96   if @nameserver.nil?
 97     ex = ResolveError.new("No available nameserver")
 98     ex.set_backtrace(caller)
 99     connection.force_close
100     throw(:resolve_error, ex)
101   else
102     @connections << connection
103     resolve
104   end
105 end
call()
[show source]
   # File lib/httpx/resolver/native.rb
76 def call
77   case @state
78   when :open
79     consume
80   end
81 end
close()
[show source]
   # File lib/httpx/resolver/native.rb
49 def close
50   transition(:closed)
51 end
closed?()
[show source]
   # File lib/httpx/resolver/native.rb
68 def closed?
69   @state == :closed
70 end
force_close(*)
[show source]
   # File lib/httpx/resolver/native.rb
53 def force_close(*)
54   @timer.cancel if @timer
55   @timer = @name = nil
56   @queries.clear
57   @timeouts.clear
58   close
59   super
60 ensure
61   terminate
62 end
handle_error(error)
[show source]
    # File lib/httpx/resolver/native.rb
123 def handle_error(error)
124   if error.respond_to?(:connection) &&
125      error.respond_to?(:host)
126     reset_hostname(error.host, connection: error.connection)
127   else
128     @queries.each do |host, connection|
129       reset_hostname(host, connection: connection)
130     end
131   end
132 
133   super
134 end
handle_socket_timeout(interval)
[show source]
    # File lib/httpx/resolver/native.rb
121 def handle_socket_timeout(interval); end
interests()
[show source]
   # File lib/httpx/resolver/native.rb
83 def interests
84   case @state
85   when :idle
86     transition(:open)
87   when :closed
88     transition(:idle)
89     transition(:open)
90   end
91 
92   calculate_interests
93 end
terminate()
[show source]
   # File lib/httpx/resolver/native.rb
64 def terminate
65   disconnect
66 end
timeout()
[show source]
    # File lib/httpx/resolver/native.rb
107 def timeout
108   return unless @name
109 
110   @start_timeout = Utils.now
111 
112   timeouts = @timeouts[@name]
113 
114   return if timeouts.empty?
115 
116   log(level: 2) { "resolver #{FAMILY_TYPES[@record_type]}: next timeout #{timeouts.first} secs... (#{timeouts.size - 1} left)" }
117 
118   timeouts.first
119 end
to_io()
[show source]
   # File lib/httpx/resolver/native.rb
72 def to_io
73   @io.to_io
74 end