class Gapic::CallOptions::RetryPolicy

The policy for retrying failed RPC calls using an incremental backoff. A new object instance should be used for every RpcCall invocation.

Only errors orginating from GRPC will be retried.

Constants

ERROR_CODE_MAPPING

@private See grpc.github.io/grpc/core/md_doc_statuscodes.html for a list of error codes.

ERROR_STRING_MAPPING

@private

Public Class Methods

new(retry_codes: nil, initial_delay: nil, multiplier: nil, max_delay: nil) click to toggle source

Create new API Call RetryPolicy.

@param initial_delay [Numeric] client-side timeout @param multiplier [Numeric] client-side timeout @param max_delay [Numeric] client-side timeout

# File lib/gapic/call_options/retry_policy.rb, line 31
def initialize retry_codes: nil, initial_delay: nil, multiplier: nil, max_delay: nil
  @retry_codes   = convert_codes retry_codes
  @initial_delay = initial_delay
  @multiplier    = multiplier
  @max_delay     = max_delay
  @delay         = nil
end

Public Instance Methods

apply_defaults(retry_policy) click to toggle source

@private Apply default values to the policy object. This does not replace user-provided values, it only overrides empty values.

@param retry_policy [Hash] The policy for error retry. keys must match the arguments for

{RpcCall::RetryPolicy.new}.
# File lib/gapic/call_options/retry_policy.rb, line 77
def apply_defaults retry_policy
  return unless retry_policy.is_a? Hash

  @retry_codes   ||= convert_codes retry_policy[:retry_codes]
  @initial_delay ||= retry_policy[:initial_delay]
  @multiplier    ||= retry_policy[:multiplier]
  @max_delay     ||= retry_policy[:max_delay]

  self
end
call(error) click to toggle source
# File lib/gapic/call_options/retry_policy.rb, line 61
def call error
  return false unless retry? error

  delay!
  increment_delay!

  true
end
delay() click to toggle source

The current delay value.

# File lib/gapic/call_options/retry_policy.rb, line 57
def delay
  @delay || initial_delay
end
initial_delay() click to toggle source
# File lib/gapic/call_options/retry_policy.rb, line 43
def initial_delay
  @initial_delay || 1
end
max_delay() click to toggle source
# File lib/gapic/call_options/retry_policy.rb, line 51
def max_delay
  @max_delay || 15
end
multiplier() click to toggle source
# File lib/gapic/call_options/retry_policy.rb, line 47
def multiplier
  @multiplier || 1.3
end
retry_codes() click to toggle source
# File lib/gapic/call_options/retry_policy.rb, line 39
def retry_codes
  @retry_codes || []
end

Private Instance Methods

convert_codes(input_codes) click to toggle source
# File lib/gapic/call_options/retry_policy.rb, line 127
def convert_codes input_codes
  return nil if input_codes.nil?
  Array(input_codes).map do |obj|
    case obj
    when String
      ERROR_STRING_MAPPING[obj]
    when Integer
      obj
    end
  end.compact
end
delay!() click to toggle source
# File lib/gapic/call_options/retry_policy.rb, line 122
def delay!
  # Call Kernel.sleep so we can stub it.
  Kernel.sleep delay
end
increment_delay!() click to toggle source

Calculate and set the next delay value.

# File lib/gapic/call_options/retry_policy.rb, line 141
def increment_delay!
  @delay = [delay * multiplier, max_delay].min
end
retry?(error) click to toggle source
# File lib/gapic/call_options/retry_policy.rb, line 118
def retry? error
  error.is_a?(GRPC::BadStatus) && retry_codes.include?(error.code)
end