lib/httpx/connection.rb in httpx-0.4.1 vs lib/httpx/connection.rb in httpx-0.5.0

- old
+ new

@@ -44,12 +44,10 @@ def_delegator :@write_buffer, :empty? attr_reader :origin, :state, :pending, :options - attr_reader :timeout - def initialize(type, uri, options) @type = type @origins = [uri.origin] @origin = URI(uri.origin) @options = Options.new(options) @@ -146,17 +144,11 @@ end def interests return :w if @state == :idle - readable = !@read_buffer.full? - writable = !@write_buffer.empty? - if readable - writable ? :rw : :r - else - writable ? :w : :r - end + :rw end def to_io case @state when :idle @@ -175,22 +167,19 @@ transition(:closed) emit(:close) end def send(request) - if @error - emit(:response, request, ErrorResponse.new(request, @error, @options)) - elsif @parser && !@write_buffer.full? + if @parser && !@write_buffer.full? request.headers["alt-used"] = @origin.authority if match_altsvcs?(request.uri) parser.send(request) else @pending << request end end def call - @timeout = @timeout_threshold case @state when :closed return when :closing dwrite @@ -200,10 +189,18 @@ consume end nil end + def timeout + return @timeout if defined?(@timeout) + + return @options.timeout.connect_timeout if @state == :idle + + @options.timeout.operation_timeout + end + private def consume catch(:called) do dread @@ -287,12 +284,12 @@ emit(:reset) transition(:idle) transition(:open) end end - parser.on(:timeout) do |timeout| - @timeout = timeout + parser.on(:timeout) do |tout| + @timeout = tout end parser.on(:error) do |request, ex| case ex when MisdirectedRequestError emit(:uncoalesce, request.uri) @@ -303,37 +300,30 @@ end end def transition(nextstate) case nextstate - when :idle - @error = nil - @timeout_threshold = @options.timeout.connect_timeout - @timeout = @timeout_threshold when :open return if @state == :closed @io.connect return unless @io.connected? send_pending - @timeout_threshold = @options.timeout.operation_timeout - @timeout = @timeout_threshold emit(:open) when :closing return unless @state == :open when :closed return unless @state == :closing return unless @write_buffer.empty? @io.close @read_buffer.clear + remove_instance_variable(:@timeout) if defined?(@timeout) when :already_open nextstate = :open send_pending - @timeout_threshold = @options.timeout.operation_timeout - @timeout = @timeout_threshold end @state = nextstate rescue Errno::EHOSTUNREACH # at this point, all addresses from the IO object have failed reset @@ -352,21 +342,20 @@ def on_error(ex) handle_error(ex) reset end - def handle_error(e) - if e.instance_of?(TimeoutError) && @timeout - @timeout -= e.timeout + def handle_error(error) + if error.instance_of?(TimeoutError) && @timeout + @timeout -= error.timeout return unless @timeout <= 0 - e = e.to_connection_error if connecting? + error = error.to_connection_error if connecting? end - parser.handle_error(e) if @parser && parser.respond_to?(:handle_error) - @error = e + parser.handle_error(error) if @parser && parser.respond_to?(:handle_error) @pending.each do |request, _| - request.emit(:response, ErrorResponse.new(request, @error, @options)) + request.emit(:response, ErrorResponse.new(request, error, @options)) end end end end