class Excon::Middleware::AWS::ExponentialBackoff

Constants

ERROR_CODE_REGEX
MILLISECOND
SERVER_ERROR_CLASSES
SLEEP_FACTOR
THROTTLING_ERROR_CODES
VALID_MIDDLEWARE_KEYS
VERSION

Public Instance Methods

do_backoff(datum) click to toggle source
# File lib/excon/middleware/aws/exponential_backoff.rb, line 46
def do_backoff(datum)
  do_sleep(sleep_time(datum), datum)
  datum[:backoff][:retry_count] += 1
  connection = datum.delete(:connection)
  datum.reject! { |key, _| !(Excon::VALID_REQUEST_KEYS + VALID_MIDDLEWARE_KEYS).include?(key)  }
  connection.request(datum)
end
do_handoff(datum) click to toggle source
# File lib/excon/middleware/aws/exponential_backoff.rb, line 42
def do_handoff(datum)
  @stack.error_call(datum)
end
do_sleep(sleep_time, datum) click to toggle source
# File lib/excon/middleware/aws/exponential_backoff.rb, line 54
def do_sleep(sleep_time, datum)
  if datum.has_key?(:instrumentor)
    datum[:instrumentor].instrument("#{datum[:instrumentor_name]}.backoff", datum) do
      sleep sleep_time
    end
  else
    sleep sleep_time
  end
end
error_call(datum) click to toggle source
# File lib/excon/middleware/aws/exponential_backoff.rb, line 29
def error_call(datum)
  datum[:backoff] ||= {}
  datum[:backoff][:max_retries] ||= 0
  datum[:backoff][:max_delay]   ||= 30
  datum[:backoff][:retry_count] ||= 0

  if (throttle?(datum) || server_error?(datum)) && should_retry?(datum)
    do_backoff(datum)
  else
    do_handoff(datum)
  end
end
extract_error_code(body) click to toggle source
# File lib/excon/middleware/aws/exponential_backoff.rb, line 89
def extract_error_code(body)
  match = ERROR_CODE_REGEX.match(body)
  if match && code = match[1]
    code.strip if code
  end
end
server_error?(datum) click to toggle source
# File lib/excon/middleware/aws/exponential_backoff.rb, line 85
def server_error?(datum)
  SERVER_ERROR_CLASSES.any? { |ex| datum[:error].kind_of?(ex) }
end
should_retry?(datum) click to toggle source
# File lib/excon/middleware/aws/exponential_backoff.rb, line 72
def should_retry?(datum)
  # Always retry if max_retries is 0.
  datum[:backoff][:max_retries] == 0 ||
    datum[:backoff][:retry_count] < datum[:backoff][:max_retries]
end
sleep_time(datum) click to toggle source
# File lib/excon/middleware/aws/exponential_backoff.rb, line 64
def sleep_time(datum)
  exponential_wait = (2 ** datum[:backoff][:retry_count] + rand(0.0)) * SLEEP_FACTOR
  [
   exponential_wait, 
   datum[:backoff][:max_delay]
  ].min.round(2)
end
throttle?(datum) click to toggle source
# File lib/excon/middleware/aws/exponential_backoff.rb, line 78
def throttle?(datum)
  datum[:error].kind_of?(Excon::Errors::BadRequest) &&
    THROTTLING_ERROR_CODES.include?(extract_error_code(datum[:error].response.body))


end