hiding monotonic time funcs under the utils API

This commit is contained in:
HoneyryderChuck 2021-09-20 14:06:11 +01:00
parent fcc29b2fb1
commit 13e865e488
10 changed files with 30 additions and 17 deletions

View File

@ -10,14 +10,14 @@ module HTTPX
module_function
def cached_altsvc(origin)
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
now = Utils.now
@altsvc_mutex.synchronize do
lookup(origin, now)
end
end
def cached_altsvc_set(origin, entry)
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
now = Utils.now
@altsvc_mutex.synchronize do
return if @altsvcs[origin].any? { |altsvc| altsvc["origin"] == entry["origin"] }

View File

@ -214,7 +214,7 @@ module HTTPX
request.headers["alt-used"] = @origin.authority if match_altsvcs?(request.uri)
if @response_received_at && @keep_alive_timeout &&
(Process.clock_gettime(Process::CLOCK_MONOTONIC) - @response_received_at) > @keep_alive_timeout
Utils.elapsed_time(@response_received_at) > @keep_alive_timeout
# when pushing a request into an existing connection, we have to check whether there
# is the possibility that the connection might have extended the keep alive timeout.
# for such cases, we want to ping for availability before deciding to shovel requests.
@ -233,7 +233,7 @@ module HTTPX
if @total_timeout
return @total_timeout unless @connected_at
elapsed_time = @total_timeout - (Process.clock_gettime(Process::CLOCK_MONOTONIC) - @connected_at)
elapsed_time = @total_timeout - Utils.elapsed_time(@connected_at)
if elapsed_time.negative?
ex = TotalTimeoutError.new(@total_timeout, "Timed out after #{@total_timeout} seconds")
@ -481,7 +481,7 @@ module HTTPX
@io.connect
return unless @io.connected?
@connected_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
@connected_at = Utils.now
send_pending
@ -517,7 +517,7 @@ module HTTPX
end
def handle_response
@response_received_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
@response_received_at = Utils.now
@inflight -= 1
end
@ -525,7 +525,7 @@ module HTTPX
if error.instance_of?(TimeoutError)
if @total_timeout && @connected_at &&
(Process.clock_gettime(Process::CLOCK_MONOTONIC) - @connected_at) > @total_timeout
Utils.elapsed_time(@connected_at) > @total_timeout
ex = TotalTimeoutError.new(@total_timeout, "Timed out after #{@total_timeout} seconds")
ex.set_backtrace(error.backtrace)
error = ex

View File

@ -26,14 +26,14 @@ module HTTPX
module_function
def cached_lookup(hostname)
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
now = Utils.now
@lookup_mutex.synchronize do
lookup(hostname, now)
end
end
def cached_lookup_set(hostname, entries)
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
now = Utils.now
entries.each do |entry|
entry["TTL"] += now
end

View File

@ -120,7 +120,7 @@ module HTTPX
def timeout
return if @connections.empty?
@start_timeout = Process.clock_gettime(Process::CLOCK_MONOTONIC)
@start_timeout = Utils.now
hosts = @queries.keys
@timeouts.values_at(*hosts).reject(&:empty?).map(&:first).min
end
@ -140,7 +140,7 @@ module HTTPX
def do_retry
return if @queries.empty?
loop_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @start_timeout
loop_time = Utils.elapsed_time(@start_timeout)
connections = []
queries = {}
while (query = @queries.shift)

View File

@ -25,7 +25,7 @@ module HTTPX
def wait_interval
return if @intervals.empty?
@next_interval_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
@next_interval_at = Utils.now
@intervals.first.interval
end
@ -34,7 +34,7 @@ module HTTPX
raise error if error && error.timeout != @intervals.first
return if @intervals.empty? || !@next_interval_at
elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @next_interval_at
elapsed_time = Utils.elapsed_time(@next_interval_at)
@intervals.delete_if { |interval| interval.elapse(elapsed_time) <= 0 }
end

View File

@ -6,6 +6,14 @@ module HTTPX
module_function
def now
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
def elapsed_time(monotonic_timestamp)
Process.clock_gettime(Process::CLOCK_MONOTONIC) - monotonic_timestamp
end
# The value of this field can be either an HTTP-date or a number of
# seconds to delay after the response is received.
def parse_retry_after(retry_after)

View File

@ -9,6 +9,7 @@ module HTTPX
def fire: (?StandardError error) -> void
def cancel: () -> void
private
def initialize: () -> void

View File

@ -4,6 +4,10 @@ module HTTPX
def self?.parse_retry_after: (String) -> Numeric
def self?.now: () -> Numeric
def self?.elapsed_time: (Numeric monotonic_time) -> Numeric
def self?.to_uri: (generic_uri uri) -> URI::Generic
end
end

View File

@ -118,10 +118,10 @@ class HTTPSTest < Minitest::Test
trailered = false
request = http.build_request(:post, uri, body: %w[this is chunked])
request.on(:headers) do |_written_request|
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
start_time = HTTPX::Utils.now
end
request.on(:trailers) do |written_request|
total_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
total_time = HTTPX::Utils.elapsed_time(start_time)
written_request.trailers["x-time-spent"] = total_time
trailered = true
end

View File

@ -24,10 +24,10 @@ module Requests
trailered = false
request = http.build_request(meth, uri, headers: { "trailer" => "X-Time-Spent" }, body: %w[this is a chunked response])
request.on(:headers) do |_written_request|
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
start_time = HTTPX::Utils.now
end
request.on(:trailers) do |written_request|
total_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
total_time = HTTPX::Utils.elapsed_time(start_time)
written_request.trailers["x-time-spent"] = total_time
trailered = true
end