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
102 def <<(connection)
103   if @nameserver.nil?
104     ex = ResolveError.new("No available nameserver")
105     ex.set_backtrace(caller)
106     connection.force_close
107     throw(:resolve_error, ex)
108   else
109     @connections << connection
110     resolve
111   end
112 end
call()
[show source]
   # File lib/httpx/resolver/native.rb
76 def call
77   case @state
78   when :idle, :closed
79     return if @connections.empty?
80 
81     transition(:idle) if @state == :closed
82     transition(:open)
83 
84     consume if @state == :open
85   when :open
86     consume
87   end
88 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 == :idle || @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
130 def handle_error(error)
131   if error.respond_to?(:connection) &&
132      error.respond_to?(:host)
133     reset_hostname(error.host, connection: error.connection)
134   else
135     @queries.each do |host, connection|
136       reset_hostname(host, connection: connection)
137     end
138   end
139 
140   super
141 end
handle_socket_timeout(interval)
[show source]
    # File lib/httpx/resolver/native.rb
128 def handle_socket_timeout(interval); end
interests()
[show source]
    # File lib/httpx/resolver/native.rb
 90 def interests
 91   case @state
 92   when :idle
 93     transition(:open)
 94   when :closed
 95     transition(:idle)
 96     transition(:open)
 97   end
 98 
 99   calculate_interests
100 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
114 def timeout
115   return unless @name
116 
117   @start_timeout = Utils.now
118 
119   timeouts = @timeouts[@name]
120 
121   return if timeouts.empty?
122 
123   log(level: 2) { "resolver #{FAMILY_TYPES[@record_type]}: next timeout #{timeouts.first} secs... (#{timeouts.size - 1} left)" }
124 
125   timeouts.first
126 end
to_io()
[show source]
   # File lib/httpx/resolver/native.rb
72 def to_io
73   @io.to_io
74 end