Class: HTTPX::Resolver::Native

Inherits:
Resolver
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/httpx/resolver/native.rb

Constant Summary collapse

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

Constants inherited from Resolver

Resolver::FAMILY_TYPES, Resolver::RECORD_TYPES

Constants included from Loggable

Loggable::COLORS

Instance Attribute Summary collapse

Attributes inherited from Resolver

#family, #pool

Instance Method Summary collapse

Methods inherited from Resolver

#emit_addresses, #empty?, multi?

Methods included from Loggable

#log, #log_exception

Methods included from Callbacks

#callbacks_for?, #emit, #on, #once, #only

Constructor Details

#initialize(_, options) ⇒ Native

Returns a new instance of Native.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/httpx/resolver/native.rb', line 32

def initialize(_, options)
  super
  @ns_index = 0
  @resolver_options = DEFAULTS.merge(@options.resolver_options)
  @socket_type = @resolver_options.fetch(:socket_type, :udp)
  @nameserver = Array(@resolver_options[:nameserver]) if @resolver_options[:nameserver]
  @ndots = @resolver_options[:ndots]
  @search = Array(@resolver_options[:search]).map { |srch| srch.scan(/[^.]+/) }
  @_timeouts = Array(@resolver_options[:timeouts])
  @timeouts = Hash.new { |timeouts, host| timeouts[host] = @_timeouts.dup }
  @connections = []
  @queries = {}
  @read_buffer = "".b
  @write_buffer = Buffer.new(@resolver_options[:packet_size])
  @state = :idle
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



30
31
32
# File 'lib/httpx/resolver/native.rb', line 30

def state
  @state
end

Instance Method Details

#<<(connection) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/httpx/resolver/native.rb', line 92

def <<(connection)
  if @nameserver.nil?
    ex = ResolveError.new("No available nameserver")
    ex.set_backtrace(caller)
    throw(:resolve_error, ex)
  else
    @connections << connection
    resolve
  end
end

#callObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/httpx/resolver/native.rb', line 61

def call
  case @state
  when :open
    consume
  end
  nil
rescue Errno::EHOSTUNREACH => e
  @ns_index += 1
  nameserver = @nameserver
  if nameserver && @ns_index < nameserver.size
    log { "resolver: failed resolving on nameserver #{@nameserver[@ns_index - 1]} (#{e.message})" }
    transition(:idle)
  else
    handle_error(e)
  end
rescue NativeResolveError => e
  handle_error(e)
end

#closeObject



49
50
51
# File 'lib/httpx/resolver/native.rb', line 49

def close
  transition(:closed)
end

#closed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/httpx/resolver/native.rb', line 53

def closed?
  @state == :closed
end

#interestsObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/httpx/resolver/native.rb', line 80

def interests
  case @state
  when :idle
    transition(:open)
  when :closed
    transition(:idle)
    transition(:open)
  end

  calculate_interests
end

#raise_timeout_error(interval) ⇒ Object



111
112
113
# File 'lib/httpx/resolver/native.rb', line 111

def raise_timeout_error(interval)
  do_retry(interval)
end

#timeoutObject



103
104
105
106
107
108
109
# File 'lib/httpx/resolver/native.rb', line 103

def timeout
  return if @connections.empty?

  @start_timeout = Utils.now
  hosts = @queries.keys
  @timeouts.values_at(*hosts).reject(&:empty?).map(&:first).min
end

#to_ioObject



57
58
59
# File 'lib/httpx/resolver/native.rb', line 57

def to_io
  @io.to_io
end