Class: HTTPX::Resolver::Native

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

Overview

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

Constant Summary collapse

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

Constants inherited from Resolver

Resolver::FAMILY_TYPES, Resolver::RECORD_TYPES

Constants included from Loggable

Loggable::COLORS, Loggable::USE_DEBUG_LOG

Instance Attribute Summary collapse

Attributes inherited from Resolver

#current_selector, #current_session, #family, #multi, #options

Instance Method Summary collapse

Methods inherited from Resolver

#each_connection, #early_resolve, #emit_addresses, #empty?, #inflight?, multi?, #on_error

Methods included from Loggable

#log, #log_exception, #log_redact, #log_redact_body, #log_redact_headers

Constructor Details

#initialize(family, options) ⇒ Native

Returns a new instance of Native.



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

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

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



25
26
27
# File 'lib/httpx/resolver/native.rb', line 25

def state
  @state
end

Instance Method Details

#<<(connection) ⇒ Object



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

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

#callObject



75
76
77
78
79
80
# File 'lib/httpx/resolver/native.rb', line 75

def call
  case @state
  when :open
    consume
  end
end

#closeObject



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

def close
  transition(:closed)
end

#closed?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/httpx/resolver/native.rb', line 67

def closed?
  @state == :closed
end

#force_closeObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/httpx/resolver/native.rb', line 52

def force_close(*)
  @timer.cancel if @timer
  @timer = @name = nil
  @queries.clear
  @timeouts.clear
  close
  super
ensure
  terminate
end

#handle_error(error) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/httpx/resolver/native.rb', line 122

def handle_error(error)
  if error.respond_to?(:connection) &&
     error.respond_to?(:host)
    reset_hostname(error.host, connection: error.connection)
  else
    @queries.each do |host, connection|
      reset_hostname(host, connection: connection)
    end
  end

  super
end

#handle_socket_timeout(interval) ⇒ Object



120
# File 'lib/httpx/resolver/native.rb', line 120

def handle_socket_timeout(interval); end

#interestsObject



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

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

  calculate_interests
end

#terminateObject



63
64
65
# File 'lib/httpx/resolver/native.rb', line 63

def terminate
  disconnect
end

#timeoutObject



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/httpx/resolver/native.rb', line 106

def timeout
  return unless @name

  @start_timeout = Utils.now

  timeouts = @timeouts[@name]

  return if timeouts.empty?

  log(level: 2) { "resolver #{FAMILY_TYPES[@record_type]}: next timeout #{timeouts.first} secs... (#{timeouts.size - 1} left)" }

  timeouts.first
end

#to_ioObject



71
72
73
# File 'lib/httpx/resolver/native.rb', line 71

def to_io
  @io.to_io
end