introducing :addresses option

(This deprecates the :transport_options option)

This should be given an array of peer addresses to connect to. It can be
used when the destination IPs are known and you want to avoid resolving
them, and when the destination is a unix socket (and the destination is
a FS path)
This commit is contained in:
HoneyryderChuck 2021-03-08 12:46:20 +00:00
parent 108837e25e
commit 0fb343056d
5 changed files with 26 additions and 8 deletions

View File

@ -71,6 +71,8 @@ module HTTPX
@inflight = 0
@keep_alive_timeout = options.timeout.keep_alive_timeout
@keep_alive_timer = nil
self.addresses = options.addresses if options.addresses
end
# this is a semi-private method, to be used by the resolver
@ -105,6 +107,8 @@ module HTTPX
return false if exhausted?
return false unless connection.addresses
!(@io.addresses & connection.addresses).empty? && @options == connection.options
end

View File

@ -13,7 +13,6 @@ module HTTPX
@addresses = addresses
@state = :idle
@options = Options.new(options)
@path = @options.transport_options[:path]
@fallback_protocol = @options.fallback_protocol
if @options.io
@io = case @options.io
@ -22,18 +21,22 @@ module HTTPX
else
@options.io
end
unless @io.nil?
@keep_open = true
@state = :connected
raise Error, "Given IO objects do not match the request authority" unless @io
@path = @io.path
@keep_open = true
@state = :connected
else
if @options.transport_options
warn ":#{__method__} is deprecated, use :addresses instead"
@path = @options.transport_options[:path]
else
@path = addresses.first
end
end
@io ||= build_socket
end
def hostname
@uri.host
end
def connect
return unless closed?

View File

@ -69,6 +69,7 @@ module HTTPX
:connection_class => Class.new(Connection),
:transport => nil,
:transport_options => nil,
:addresses => nil,
:persistent => false,
:resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym,
:resolver_options => { cache: true },
@ -121,6 +122,10 @@ module HTTPX
transport
end
def_option(:addresses) do |addrs|
Array(addrs)
end
%w[
params form json body ssl http2_settings
request_class response_class headers_class request_body_class response_body_class connection_class

View File

@ -48,6 +48,11 @@ module HTTPX
def transport_options=: (Hash[untyped, untyped]) -> void
def with_transport_options: (Hash[untyped, untyped]) -> instance
# addresses
attr_reader addresses: _ToAry[untyped]?
def addresses=: (_ToAry[untyped]) -> void
def with_addresses: (_ToAry[untyped]) -> instance
# params
attr_reader params: Transcoder::urlencoded_input?
def params=: (Transcoder::urlencoded_input) -> void

View File

@ -78,6 +78,7 @@ class OptionsTest < Minitest::Test
:connection_class => bar.connection_class,
:transport => nil,
:transport_options => nil,
:addresses => nil,
:persistent => false,
:resolver_class => bar.resolver_class,
:resolver_options => bar.resolver_options,