lib/httpx/resolver/native.rb in httpx-0.7.0 vs lib/httpx/resolver/native.rb in httpx-0.8.0

- old
+ new

@@ -71,18 +71,10 @@ def closed? @state == :closed end def to_io - case @state - when :idle - transition(:open) - when :closed - transition(:idle) - transition(:open) - end - resolve if @queries.empty? @io.to_io end def call case @state @@ -91,24 +83,28 @@ end nil 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 + 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 def interests + case @state + when :idle + transition(:open) + when :closed + transition(:idle) + transition(:open) + end + !@write_buffer.empty? || @queries.empty? ? :w : :r end def <<(connection) return if early_resolve(connection) @@ -158,46 +154,37 @@ @timeouts.delete(host) @connections.delete(connection) raise NativeResolveError.new(connection, host) else connections << connection - log(label: "resolver: ") do - # :nocov: - "timeout after #{prev_timeout}s, retry(#{timeouts.first}) #{host}..." - # :nocov: - end + log { "resolver: timeout after #{prev_timeout}s, retry(#{timeouts.first}) #{host}..." } end end @queries = queries connections.each { |ch| resolve(ch) } end 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? + return unless siz && siz.positive? - log(label: "resolver: ") { "READ: #{siz} bytes..." } + log { "resolver: READ: #{siz} bytes..." } parse(@read_buffer) + return if @state == :closed end end def dwrite loop do return if @write_buffer.empty? siz = @io.write(@write_buffer) - unless siz - emit(:close) - return - end - log(label: "resolver: ") { "WRITE: #{siz} bytes..." } - return if siz.zero? + return unless siz && siz.positive? + + log { "resolver: WRITE: #{siz} bytes..." } + return if @state == :closed end end def parse(buffer) begin @@ -251,11 +238,11 @@ return unless @write_buffer.empty? hostname = hostname || @queries.key(connection) || connection.origin.host @queries[hostname] = connection type = @_record_types[hostname].first - log(label: "resolver: ") { "query #{type} for #{hostname}" } + log { "resolver: query #{type} for #{hostname}" } begin @write_buffer << Resolver.encode_dns_query(hostname, type: RECORD_TYPES[type]) rescue Resolv::DNS::EncodeError => e emit_resolve_error(connection, hostname, e) end @@ -267,11 +254,11 @@ ip, port = @nameserver[@ns_index] port ||= DNS_PORT uri = URI::Generic.build(scheme: "udp", port: port) uri.hostname = ip type = IO.registry(uri.scheme) - log(label: "resolver: ") { "server: #{uri}..." } + log { "resolver: server: #{uri}..." } @io = type.new(uri, [IPAddr.new(ip)], @options) end def transition(nextstate) case nextstate @@ -283,11 +270,14 @@ @timeouts.clear when :open return unless @state == :idle build_socket + @io.connect return unless @io.connected? + + resolve if @queries.empty? when :closed return unless @state == :open @io.close if @io end