Class: Gcloud::Backoff

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/backoff.rb

Overview

Backoff allows users to control how Google API calls are retried. If an API call fails the response will be checked to see if the call can be retried. If the response matches the criteria, then it will be retried with an incremental backoff. This means that an increasing delay will be added between each retried call. The first retry will be delayed one second, the second retry will be delayed two seconds, and so on.

Examples:

require "gcloud/backoff"

Gcloud::Backoff.retries = 5 # Set a maximum of five retries per call

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Backoff

Creates a new Backoff object to catch common errors when calling the Google API and handle the error by retrying the call.

Gcloud::Backoff.new(options).execute_gapi do
  client.execute api_method: service.things.insert,
                 parameters: { thing: @thing },
                 body_object: { name: thing_name }
end


83
84
85
86
87
88
89
# File 'lib/gcloud/backoff.rb', line 83

def initialize options = {}
  @retries    = (options[:retries]    || Backoff.retries).to_i
  @grpc_codes = (options[:grpc_codes] || Backoff.grpc_codes).to_a
  @http_codes = (options[:http_codes] || Backoff.http_codes).to_a
  @reasons    = (options[:reasons]    || Backoff.reasons).to_a
  @backoff    =  options[:backoff]    || Backoff.backoff
end

Class Attribute Details

.backoffObject

The code to run when a backoff is handled. This must be a Proc and must take the number of retries as an argument.

Note: This method is undocumented and may change.



64
65
66
# File 'lib/gcloud/backoff.rb', line 64

def backoff
  @backoff
end

.grpc_codesObject

The GRPC Status Codes that should be retried.

The default values are 14.



43
44
45
# File 'lib/gcloud/backoff.rb', line 43

def grpc_codes
  @grpc_codes
end

.http_codesObject

The HTTP Status Codes that should be retried.

The default values are 500 and 503.



49
50
51
# File 'lib/gcloud/backoff.rb', line 49

def http_codes
  @http_codes
end

.reasonsObject

The Google API error reasons that should be retried.

The default values are rateLimitExceeded and userRateLimitExceeded.



56
57
58
# File 'lib/gcloud/backoff.rb', line 56

def reasons
  @reasons
end

.retriesObject

The number of times a retriable API call should be retried.

The default value is 3.



37
38
39
# File 'lib/gcloud/backoff.rb', line 37

def retries
  @retries
end

Instance Method Details

#execute_gapiObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/gcloud/backoff.rb', line 92

def execute_gapi
  current_retries = 0
  loop do
    result = yield
    return result unless result.is_a? Google::APIClient::Result
    break result if result.success? || !retry?(result, current_retries)
    current_retries += 1
    @backoff.call current_retries
  end
end

#execute_grpcObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gcloud/backoff.rb', line 104

def execute_grpc
  current_retries = 0
  loop do
    begin
      return yield
    rescue GRPC::BadStatus => e
      raise e unless @grpc_codes.include?(e.code) &&
                     (current_retries < @retries)
      current_retries += 1
      @backoff.call current_retries
    end
  end
end