class Aws::Plugins::Retries::ClockSkew

@api private

Constants

CLOCK_SKEW_THRESHOLD

Public Class Methods

new() click to toggle source
# File lib/aws-sdk-core/plugins/retries/clock_skew.rb, line 12
def initialize
  @mutex = Mutex.new
  # clock_corrections are recorded only on errors
  # and only when time difference is greater than the
  # CLOCK_SKEW_THRESHOLD
  @endpoint_clock_corrections = Hash.new(0)

  # estimated_skew is calculated on every request
  # and is used to estimate a TTL for requests
  @endpoint_estimated_skews = Hash.new(nil)
end

Public Instance Methods

clock_correction(endpoint) click to toggle source

Gets the clock_correction in seconds to apply to a given endpoint @param endpoint [URI / String]

# File lib/aws-sdk-core/plugins/retries/clock_skew.rb, line 26
def clock_correction(endpoint)
  @mutex.synchronize { @endpoint_clock_corrections[endpoint.to_s] }
end
clock_skewed?(context) click to toggle source

Determines whether a request has clock skew by comparing the current time against the server’s time in the response @param context [Seahorse::Client::RequestContext]

# File lib/aws-sdk-core/plugins/retries/clock_skew.rb, line 44
def clock_skewed?(context)
  server_time = server_time(context.http_response)
  !!server_time &&
    (Time.now.utc - server_time).abs > CLOCK_SKEW_THRESHOLD
end
estimated_skew(endpoint) click to toggle source

The estimated skew factors in any clock skew from the service along with any network latency. This provides a more accurate value for the ttl, which should represent when the client will stop waiting for a request. Estimated Skew should not be used to correct clock skew errors it should only be used to estimate TTL for a request

# File lib/aws-sdk-core/plugins/retries/clock_skew.rb, line 37
def estimated_skew(endpoint)
  @mutex.synchronize { @endpoint_estimated_skews[endpoint.to_s] }
end
update_clock_correction(context) click to toggle source

Called only on clock skew related errors Update the stored clock skew correction value for an endpoint from the server’s time in the response @param context [Seahorse::Client::RequestContext]

# File lib/aws-sdk-core/plugins/retries/clock_skew.rb, line 54
def update_clock_correction(context)
  endpoint = context.http_request.endpoint
  now_utc = Time.now.utc
  server_time = server_time(context.http_response)
  if server_time && (now_utc - server_time).abs > CLOCK_SKEW_THRESHOLD
    set_clock_correction(endpoint, server_time - now_utc)
  end
end
update_estimated_skew(context) click to toggle source

Called for every request Update our estimated clock skew for the endpoint from the servers time in the response @param context [Seahorse::Client::RequestContext]

# File lib/aws-sdk-core/plugins/retries/clock_skew.rb, line 67
def update_estimated_skew(context)
  endpoint = context.http_request.endpoint
  now_utc = Time.now.utc
  server_time = server_time(context.http_response)
  return unless server_time
  @mutex.synchronize do
    @endpoint_estimated_skews[endpoint.to_s] = server_time - now_utc
  end
end

Private Instance Methods

server_time(response) click to toggle source

@param response [Seahorse::Client::Http::Response:]

# File lib/aws-sdk-core/plugins/retries/clock_skew.rb, line 80
def server_time(response)
  begin
    Time.parse(response.headers['date']).utc
  rescue
    nil
  end
end
set_clock_correction(endpoint, correction) click to toggle source

Sets the clock correction for an endpoint @param endpoint [URI / String] @param correction [Number]

# File lib/aws-sdk-core/plugins/retries/clock_skew.rb, line 91
def set_clock_correction(endpoint, correction)
  @mutex.synchronize do
    @endpoint_clock_corrections[endpoint.to_s] = correction
  end
end