From d13864df4efa09ae6fd841d956607e4257182db9 Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Wed, 21 Nov 2018 18:32:37 +0000 Subject: [PATCH] transitioning the timeout between open and idle, depending of connection having channels connecting --- lib/httpx/channel.rb | 1 + lib/httpx/connection.rb | 10 +++++++--- lib/httpx/timeout.rb | 22 +++++++++++++++++----- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/httpx/channel.rb b/lib/httpx/channel.rb index bfde051c..1930fd5f 100644 --- a/lib/httpx/channel.rb +++ b/lib/httpx/channel.rb @@ -292,6 +292,7 @@ module HTTPX transition(:closing) unless parser.empty? transition(:closed) + emit(:reset) transition(:idle) transition(:open) end diff --git a/lib/httpx/connection.rb b/lib/httpx/connection.rb index f26c006f..8c20bf43 100644 --- a/lib/httpx/connection.rb +++ b/lib/httpx/connection.rb @@ -54,8 +54,12 @@ module HTTPX def build_channel(uri, **options) channel = Channel.by(uri, @options.merge(options)) resolve_channel(channel) - channel.once(:open) do + channel.on(:open) do @connected_channels += 1 + @timeout.transition(:open) if @channels.size == @connected_channels + end + channel.on(:reset) do + @timeout.transition(:idle) end channel.once(:unreachable) do @resolver.uncache(channel) @@ -114,6 +118,7 @@ module HTTPX end def register_channel(channel) + @timeout.transition(:idle) monitor = @selector.register(channel, :w) monitor.value = channel channel.on(:close) do @@ -137,8 +142,7 @@ module HTTPX end def next_timeout - connecting = @channels.empty? || (@channels.size != @connected_channels) - timeout = @timeout.timeout(connecting: connecting) # force log time + timeout = @timeout.timeout return (@resolver.timeout || timeout) unless @resolver.closed? timeout end diff --git a/lib/httpx/timeout.rb b/lib/httpx/timeout.rb index 6f60dbf3..fc5ee98b 100644 --- a/lib/httpx/timeout.rb +++ b/lib/httpx/timeout.rb @@ -4,7 +4,6 @@ require "timeout" module HTTPX class Timeout - include Loggable CONNECT_TIMEOUT = 60 OPERATION_TIMEOUT = 60 @@ -23,15 +22,16 @@ module HTTPX @operation_timeout = operation_timeout @total_timeout = total_timeout if loop_timeout - log { ":loop_timeout is deprecated, use :operation_timeout instead" } + warn ":loop_timeout is deprecated, use :operation_timeout instead" @operation_timeout = loop_timeout end reset_counter + @state = :idle # this is here not to trigger the log + transition(:idle) end - def timeout(connecting: false) - tout = connecting ? @connect_timeout : @operation_timeout - tout || @total_timeout + def timeout + @timeout || @total_timeout ensure log_time end @@ -63,6 +63,18 @@ module HTTPX end end + def transition(nextstate) + return if @state == nextstate + case nextstate + # when :idle + when :idle + @timeout = @connect_timeout + when :open + @timeout = @operation_timeout + end + @state = nextstate + end + private def reset_counter