Class: HTTPX::Resolver::Native

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

Constant Summary collapse

RESOLVE_TIMEOUT =
5
RECORD_TYPES =
{
  "A" => Resolv::DNS::Resource::IN::A,
  "AAAA" => Resolv::DNS::Resource::IN::AAAA,
}.freeze
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

Constants included from ResolverMixin

ResolverMixin::CHECK_IF_IP

Constants included from Loggable

Loggable::COLORS

Instance Method Summary collapse

Methods included from ResolverMixin

#uncache

Methods included from Loggable

#log

Methods included from Callbacks

#emit, #on, #once

Constructor Details

#initialize(_, options) ⇒ Native

Returns a new instance of Native.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/httpx/resolver/native.rb', line 38

def initialize(_, options)
  @options = Options.new(options)
  @ns_index = 0
  @resolver_options = Resolver::Options.new(DEFAULTS.merge(@options.resolver_options || {}))
  @nameserver = @resolver_options.nameserver
  @_timeouts = Array(@resolver_options.timeouts)
  @timeouts = Hash.new { |timeouts, host| timeouts[host] = @_timeouts.dup }
  @_record_types = Hash.new { |types, host| types[host] = @resolver_options.record_types.dup }
  @channels = []
  @queries = {}
  @read_buffer = Buffer.new(@resolver_options.packet_size)
  @write_buffer = Buffer.new(@resolver_options.packet_size)
  @state = :idle
end

Instance Method Details

#<<(channel) ⇒ Object



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

def <<(channel)
  return if early_resolve(channel)
  if @nameserver.nil?
    ex = ResolveError.new("Can't resolve #{channel.uri.host}")
    ex.set_backtrace(caller)
    emit(:error, channel, ex)
  else
    @channels << channel
  end
end

#callObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/httpx/resolver/native.rb', line 73

def call
  case @state
  when :open
    consume
  end
  nil
rescue Errno::EHOSTUNREACH => e
  @ns_index += 1
  if @ns_index < @nameserver.size
    transition(:idle)
  else
    ex = ResolvError.new(e.message)
    ex.set_backtrace(e.backtrace)
    raise ex
  end
end

#closeObject



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

def close
  transition(:closed)
end

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  @state == :closed
end

#interestsObject



90
91
92
93
94
95
96
97
98
# File 'lib/httpx/resolver/native.rb', line 90

def interests
  readable = !@read_buffer.full?
  writable = !@write_buffer.empty?
  if readable
    writable ? :rw : :r
  else
    writable ? :w : :r
  end
end

#timeoutObject



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

def timeout
  @start_timeout = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  hosts = @queries.keys
  @timeouts.values_at(*hosts).reject(&:empty?).map(&:first).min
end

#to_ioObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/httpx/resolver/native.rb', line 61

def to_io
  case @state
  when :idle
    transition(:open)
  when :closed
    transition(:idle)
    transition(:open)
  end
  resolve if @queries.empty?
  @io.to_io
end