transitioning the timeout between open and idle, depending of connection having channels connecting

This commit is contained in:
HoneyryderChuck 2018-11-21 18:32:37 +00:00
parent befbd818b8
commit d13864df4e
3 changed files with 25 additions and 8 deletions

View File

@ -292,6 +292,7 @@ module HTTPX
transition(:closing)
unless parser.empty?
transition(:closed)
emit(:reset)
transition(:idle)
transition(:open)
end

View File

@ -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

View File

@ -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