Skip to content

Commit

Permalink
Use monotonic clock to measure elapsed time (#2566)
Browse files Browse the repository at this point in the history
The monotonic clock can only move forward and is not subject to changes of the system clock, e.g. corrections due to NTP
  • Loading branch information
bdewater authored Oct 24, 2017
1 parent d0e1b4c commit 36792b5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/active_merchant/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def initialize(endpoint)
end

def request(method, body, headers = {})
request_start = Time.now.to_f
request_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

retry_exceptions(:max_retries => max_retries, :logger => logger, :tag => tag) do
begin
Expand Down Expand Up @@ -89,7 +89,7 @@ def request(method, body, headers = {})
end

ensure
info "connection_request_total_time=%.4fs" % [Time.now.to_f - request_start], tag
info "connection_request_total_time=%.4fs" % [Process.clock_gettime(Process::CLOCK_MONOTONIC) - request_start], tag
end

private
Expand Down
8 changes: 4 additions & 4 deletions lib/active_merchant/network_connection_retries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ def retry_network_exceptions(options = {})
request_start = nil

begin
request_start = Time.now.to_f
request_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = yield
log_with_retry_details(options[:logger], initial_retries-retries + 1, Time.now.to_f - request_start, "success", options[:tag])
log_with_retry_details(options[:logger], initial_retries-retries + 1, Process.clock_gettime(Process::CLOCK_MONOTONIC) - request_start, "success", options[:tag])
result
rescue ActiveMerchant::RetriableConnectionError => e
retries -= 1

log_with_retry_details(options[:logger], initial_retries-retries, Time.now.to_f - request_start, e.message, options[:tag])
log_with_retry_details(options[:logger], initial_retries-retries, Process.clock_gettime(Process::CLOCK_MONOTONIC) - request_start, e.message, options[:tag])
retry unless retries.zero?
raise ActiveMerchant::ConnectionError.new(e.message, e)
rescue ActiveMerchant::ConnectionError, ActiveMerchant::InvalidResponseError => e
retries -= 1
log_with_retry_details(options[:logger], initial_retries-retries, Time.now.to_f - request_start, e.message, options[:tag])
log_with_retry_details(options[:logger], initial_retries-retries, Process.clock_gettime(Process::CLOCK_MONOTONIC) - request_start, e.message, options[:tag])
retry if (options[:retry_safe] || retry_safe) && !retries.zero?
raise
end
Expand Down

0 comments on commit 36792b5

Please sign in to comment.