lib/httpx/resolver/native.rb in httpx-0.6.4 vs lib/httpx/resolver/native.rb in httpx-0.6.5
- old
+ new
@@ -57,11 +57,11 @@
@_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 = Buffer.new(@resolver_options.packet_size)
+ @read_buffer = "".b
@write_buffer = Buffer.new(@resolver_options.packet_size)
@state = :idle
end
def close
@@ -88,40 +88,28 @@
case @state
when :open
consume
end
nil
- rescue Errno::EHOSTUNREACH,
- NativeResolveError => e
+ rescue Errno::EHOSTUNREACH => e
@ns_index += 1
if @ns_index < @nameserver.size
log(label: "resolver: ") do
# :nocov:
"failed resolving on nameserver #{@nameserver[@ns_index - 1]} (#{e.message})"
# :nocov:
end
transition(:idle)
else
- if e.respond_to?(:connection) &&
- e.respond_to?(:host)
- emit_resolve_error(e.connection, e.host, e)
- else
- @queries.each do |host, connection|
- emit_resolve_error(connection, host, e)
- end
- end
+ handle_error(e)
end
+ rescue NativeResolveError => e
+ handle_error(e)
end
def interests
- readable = !@read_buffer.full?
- writable = !@write_buffer.empty?
- if readable
- writable ? :rw : :r
- else
- writable ? :w : :r
- end
+ !@write_buffer.empty? || @queries.empty? ? :w : :r
end
def <<(connection)
return if early_resolve(connection)
@@ -166,10 +154,11 @@
next
end
@timeouts[host].shift
if @timeouts[host].empty?
@timeouts.delete(host)
+ @connections.delete(connection)
raise NativeResolveError.new(connection, host)
else
connections << connection
log(label: "resolver: ") do
# :nocov:
@@ -180,21 +169,21 @@
end
@queries = queries
connections.each { |ch| resolve(ch) }
end
- def dread(wsize = @read_buffer.limit)
+ def dread(wsize = @resolver_options.packet_size)
loop do
siz = @io.read(wsize, @read_buffer)
unless siz
emit(:close)
return
end
return if siz.zero?
log(label: "resolver: ") { "READ: #{siz} bytes..." }
- parse(@read_buffer.to_s)
+ parse(@read_buffer)
end
end
def dwrite
loop do
@@ -214,21 +203,25 @@
begin
addresses = Resolver.decode_dns_answer(buffer)
rescue Resolv::DNS::DecodeError => e
hostname, connection = @queries.first
if @_record_types[hostname].empty?
+ @queries.delete(hostname)
+ @connections.delete(connection)
ex = NativeResolveError.new(connection, hostname, e.message)
ex.set_backtrace(e.backtrace)
raise ex
end
end
if addresses.empty?
hostname, connection = @queries.first
@_record_types[hostname].shift
if @_record_types[hostname].empty?
+ @queries.delete(hostname)
@_record_types.delete(hostname)
+ @connections.delete(connection)
raise NativeResolveError.new(connection, hostname)
end
else
address = addresses.first
connection = @queries.delete(address["name"])
@@ -242,11 +235,11 @@
@queries.delete(address["name"])
return
end
else
@connections.delete(connection)
- Resolver.cached_lookup_set(connection.origin.host, addresses)
+ Resolver.cached_lookup_set(connection.origin.host, addresses) if @resolver_options.cache
emit_addresses(connection, addresses.map { |addr| addr["data"] })
end
end
return emit(:close) if @connections.empty?
@@ -298,8 +291,19 @@
return unless @state == :open
@io.close if @io
end
@state = nextstate
+ end
+
+ def handle_error(error)
+ if error.respond_to?(:connection) &&
+ error.respond_to?(:host)
+ emit_resolve_error(error.connection, error.host, error)
+ else
+ @queries.each do |host, connection|
+ emit_resolve_error(connection, host, error)
+ end
+ end
end
end
end