Module: GRPC::Kit::Communication::Resilient

Defined in:
lib/grpc/kit/communication/resilient.rb

Constant Summary collapse

ERRORS =
[
  GRPC::BadStatus,
  Google::Cloud::UnavailableError,
  Google::Cloud::InternalError
].freeze

Instance Method Summary collapse

Instance Method Details

#exponential_backoff(tries, limit:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# 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