diff --git a/lib/httpx/options.rb b/lib/httpx/options.rb index 2f02e59a..8be5a00d 100644 --- a/lib/httpx/options.rb +++ b/lib/httpx/options.rb @@ -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) diff --git a/lib/httpx/resolver/https.rb b/lib/httpx/resolver/https.rb index 1696cc34..6afd4ffc 100644 --- a/lib/httpx/resolver/https.rb +++ b/lib/httpx/resolver/https.rb @@ -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 diff --git a/lib/httpx/resolver/native.rb b/lib/httpx/resolver/native.rb index 32c4b7cc..eec884fa 100644 --- a/lib/httpx/resolver/native.rb +++ b/lib/httpx/resolver/native.rb @@ -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 diff --git a/lib/httpx/resolver/resolver_mixin.rb b/lib/httpx/resolver/resolver_mixin.rb index 67b0afca..46b1a89e 100644 --- a/lib/httpx/resolver/resolver_mixin.rb +++ b/lib/httpx/resolver/resolver_mixin.rb @@ -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 diff --git a/lib/httpx/resolver/system.rb b/lib/httpx/resolver/system.rb index 938ce5e6..d945258e 100644 --- a/lib/httpx/resolver/system.rb +++ b/lib/httpx/resolver/system.rb @@ -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?