module GRPC::Kit::Communication::Resilient

Constants

ERRORS

Public Instance Methods

exponential_backoff(tries, limit:) click to toggle source

See: en.wikipedia.org/wiki/Exponential_backoff

# File lib/grpc/kit/communication/resilient.rb, line 28
def exponential_backoff(tries, limit:)
  # Retry few times before going exponential
  return true if tries <= 3

  # Check whether it's reached the ceiling
  if tries < limit
    retry_time = 0.1 * rand(1 << tries) # random number between 0 and 2**N − 1
    sleep(retry_time)
  end
end
resilient(limit: 16) { || ... } click to toggle source
# File lib/grpc/kit/communication/resilient.rb, line 14
def resilient(limit: 16)
  tries ||= 0
  yield
# From Datastore documentation:
# - UNAVAILABLE;
# - Server returned an error;
# - Retry using exponential backoff.
rescue *ERRORS => e
  tries += 1
  exponential_backoff(tries, limit: limit) && retry
  raise
end