lib/httpx/timeout.rb in httpx-0.3.1 vs lib/httpx/timeout.rb in httpx-0.4.0
- old
+ new
@@ -7,10 +7,11 @@
CONNECT_TIMEOUT = 60
OPERATION_TIMEOUT = 60
def self.new(opts = {})
return opts if opts.is_a?(Timeout)
+
super
end
attr_reader :connect_timeout, :operation_timeout
@@ -24,16 +25,14 @@
if loop_timeout
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
- @timeout || @total_timeout
+ def total_timeout
+ @total_timeout
ensure
log_time
end
def ==(other)
@@ -61,20 +60,12 @@
else
raise ArgumentError, "can't merge with #{other.class}"
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
+ def no_time_left?
+ @time_left <= 0
end
private
def reset_counter
@@ -86,11 +77,12 @@
end
def log_time
return unless @time_left
return reset_timer unless @started
+
@time_left -= (Process.clock_gettime(Process::CLOCK_MONOTONIC) - @started)
- raise TimeoutError.new(@total_timeout, "Timed out after #{@total_timeout} seconds") if @time_left <= 0
+ raise TotalTimeoutError.new(@total_timeout, "Timed out after #{@total_timeout} seconds") if no_time_left?
reset_timer
end
end
end