remove connect call from selector, making it implicit when selecting interests

This commit is contained in:
HoneyryderChuck 2020-04-19 15:21:58 +01:00
parent f977db76ef
commit ec6e39d7b9
6 changed files with 50 additions and 38 deletions

View File

@ -181,9 +181,9 @@ module HTTPX
def interests
# connecting
if connecting?
return :w unless @io
connect
return @io.interests
return @io.interests if connecting?
end
# if the write buffer is full, we drain it
@ -194,10 +194,6 @@ module HTTPX
nil
end
def connect
transition(:open)
end
def to_io
@io.to_io
end
@ -251,6 +247,10 @@ module HTTPX
private
def connect
transition(:open)
end
def exhausted?
@parser && parser.exhausted?
end

View File

@ -93,6 +93,7 @@ module HTTPX
buffer.bytesize
rescue ::IO::WaitReadable,
::IO::WaitWritable
buffer.clear
0
rescue EOFError
nil

View File

@ -179,17 +179,6 @@ module HTTPX
super || @state == :connecting || @state == :connected
end
def connect
return super unless @options.proxy
case @state
when :idle
transition(:connecting)
when :connected
transition(:open)
end
end
def call
super
@ -210,6 +199,19 @@ module HTTPX
emit(:close)
end
private
def connect
return super unless @options.proxy
case @state
when :idle
transition(:connecting)
when :connected
transition(:open)
end
end
def transition(nextstate)
return super unless @options.proxy

View File

@ -25,7 +25,7 @@ module HTTPX
def_delegator :@connections, :empty?
def_delegators :@resolver_connection, :connect, :connecting?, :to_io, :call, :interests, :close
def_delegators :@resolver_connection, :connecting?, :to_io, :call, :close
def initialize(options)
@options = Options.new(options)
@ -62,8 +62,20 @@ module HTTPX
resolver_connection.closed?
end
def interests
return if @queries.empty?
resolver_connection.__send__(__method__)
end
private
def connect
return if @queries.empty?
resolver_connection.__send__(__method__)
end
def pool
Thread.current[:httpx_connection_pool] ||= Pool.new
end

View File

@ -72,21 +72,6 @@ module HTTPX
@state == :closed
end
def connecting?
@state == :idle
end
def connect
case @state
when :idle
transition(:open)
when :closed
transition(:idle)
transition(:open)
end
resolve if @queries.empty?
end
def to_io
@io.to_io
end
@ -110,6 +95,14 @@ module HTTPX
end
def interests
case @state
when :idle
transition(:open)
when :closed
transition(:idle)
transition(:open)
end
!@write_buffer.empty? || @queries.empty? ? :w : :r
end
@ -279,8 +272,11 @@ module HTTPX
return unless @state == :idle
build_socket
@io.connect
return unless @io.connected?
resolve if @queries.empty?
when :closed
return unless @state == :open

View File

@ -84,16 +84,19 @@ class HTTPX::Selector
private
READ_INTERESTS = %i[r rw].freeze
WRITE_INTERESTS = %i[w rw].freeze
def select_many(interval)
begin
r = nil
w = nil
@selectables.each_key do |io|
io.connect if io.connecting?
interests = io.interests
(r ||= []) << io if io.interests == :r || io.interests == :rw
(w ||= []) << io if io.interests == :w || io.interests == :rw
(r ||= []) << io if READ_INTERESTS.include?(interests)
(w ||= []) << io if WRITE_INTERESTS.include?(interests)
end
readers, writers = IO.select(r, w, nil, interval)
@ -125,8 +128,6 @@ class HTTPX::Selector
def select_one(interval)
io, monitor = @selectables.first
io.connect if io.connecting?
result = case io.interests
when :r then io.to_io.wait_readable(interval)
when :w then io.to_io.wait_writable(interval)