Class: HTTPX::Resolver::Native
- Inherits:
-
Object
- Object
- HTTPX::Resolver::Native
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
ResolverMixin::CHECK_IF_IP
Constants included
from Loggable
Loggable::COLORS
Instance Method Summary
collapse
#uncache
Methods included from Loggable
#log, #log_exception
Methods included from Callbacks
#emit, #on, #once
Constructor Details
#initialize(options) ⇒ Native
Returns a new instance of Native.
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/httpx/resolver/native.rb', line 52
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 }
@connections = []
@queries = {}
@read_buffer = "".b
@write_buffer = Buffer.new(@resolver_options.packet_size)
@state = :idle
end
|
Instance Method Details
#<<(connection) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/httpx/resolver/native.rb', line 109
def <<(connection)
return if early_resolve(connection)
if @nameserver.nil?
ex = ResolveError.new("Can't resolve #{connection.origin.host}: no nameserver")
ex.set_backtrace(caller)
emit(:error, connection, ex)
else
@connections << connection
resolve
end
end
|
#call ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/httpx/resolver/native.rb', line 79
def call
case @state
when :open
consume
end
nil
rescue Errno::EHOSTUNREACH => e
@ns_index += 1
if @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
|
#close ⇒ Object
67
68
69
|
# File 'lib/httpx/resolver/native.rb', line 67
def close
transition(:closed)
end
|
#closed? ⇒ Boolean
71
72
73
|
# File 'lib/httpx/resolver/native.rb', line 71
def closed?
@state == :closed
end
|
#interests ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/httpx/resolver/native.rb', line 97
def interests
case @state
when :idle
transition(:open)
when :closed
transition(:idle)
transition(:open)
end
!@write_buffer.empty? || @queries.empty? ? :w : :r
end
|
#timeout ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/httpx/resolver/native.rb', line 122
def timeout
return if @connections.empty?
@start_timeout = Process.clock_gettime(Process::CLOCK_MONOTONIC)
hosts = @queries.keys
@timeouts.values_at(*hosts).reject(&:empty?).map(&:first).min
end
|
#to_io ⇒ Object
75
76
77
|
# File 'lib/httpx/resolver/native.rb', line 75
def to_io
@io.to_io
end
|