passing a flag for resolvers, where you can choose to use cache or not, which is helpful for tests

This commit is contained in:
HoneyryderChuck 2020-02-02 09:44:39 +01:00
parent 992f485967
commit 60e30f3f10
5 changed files with 10 additions and 6 deletions

View File

@ -61,6 +61,7 @@ module HTTPX
:transport_options => nil,
:persistent => false,
:resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym,
:resolver_options => { cache: true },
}
defaults.merge!(options)

View File

@ -158,7 +158,7 @@ module HTTPX
next unless connection # probably a retried query for which there's an answer
@connections.delete(connection)
Resolver.cached_lookup_set(hostname, addresses)
Resolver.cached_lookup_set(hostname, addresses) if @resolver_options.cache
emit_addresses(connection, addresses.map { |addr| addr["data"] })
end
end

View File

@ -237,7 +237,7 @@ module HTTPX
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

View File

@ -38,7 +38,7 @@ module HTTPX
def early_resolve(connection, hostname: connection.origin.host)
addresses = connection.addresses ||
ip_resolve(hostname) ||
Resolver.cached_lookup(hostname) ||
(@resolver_options.cache && Resolver.cached_lookup(hostname)) ||
system_resolve(hostname)
return unless addresses

View File

@ -14,10 +14,13 @@ module HTTPX
def initialize(options)
@options = Options.new(options)
roptions = @options.resolver_options
@resolver_options = Resolver::Options.new(@options.resolver_options)
@state = :idle
@resolver = Resolv::DNS.new(roptions.nil? ? nil : roptions)
@resolver.timeouts = roptions[:timeouts] if roptions
resolv_options = @resolver_options.to_h
timeouts = resolv_options.delete(:timeouts)
resolv_options.delete(:cache)
@resolver = Resolv::DNS.new(resolv_options.empty? ? nil : resolv_options)
@resolver.timeouts = timeouts if timeouts
end
def closed?