Class: Gcloud::Backoff
- Inherits:
-
Object
- Object
- Gcloud::Backoff
- 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.
Class Attribute Summary collapse
-
.backoff ⇒ Object
The code to run when a backoff is handled.
-
.grpc_codes ⇒ Object
The GRPC Status Codes that should be retried.
-
.http_codes ⇒ Object
The HTTP Status Codes that should be retried.
-
.reasons ⇒ Object
The Google API error reasons that should be retried.
-
.retries ⇒ Object
The number of times a retriable API call should be retried.
Instance Method Summary collapse
- #execute_gapi ⇒ Object
- #execute_grpc ⇒ Object
-
#initialize(options = {}) ⇒ Backoff
constructor
Creates a new Backoff object to catch common errors when calling the Google API and handle the error by retrying the call.
Constructor Details
#initialize(options = {}) ⇒ Backoff
87 88 89 90 91 92 93 |
# File 'lib/gcloud/backoff.rb', line 87 def initialize = {} @retries = ([:retries] || Backoff.retries).to_i @grpc_codes = ([:grpc_codes] || Backoff.grpc_codes).to_a @http_codes = ([:http_codes] || Backoff.http_codes).to_a @reasons = ([:reasons] || Backoff.reasons).to_a @backoff = [:backoff] || Backoff.backoff end |
Class Attribute Details
.backoff ⇒ Object
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.
68 69 70 |
# File 'lib/gcloud/backoff.rb', line 68 def backoff @backoff end |
.grpc_codes ⇒ Object
The GRPC Status Codes that should be retried.
The default values are ‘14`.
47 48 49 |
# File 'lib/gcloud/backoff.rb', line 47 def grpc_codes @grpc_codes end |
.http_codes ⇒ Object
The HTTP Status Codes that should be retried.
The default values are ‘500` and `503`.
53 54 55 |
# File 'lib/gcloud/backoff.rb', line 53 def http_codes @http_codes end |
.reasons ⇒ Object
The Google API error reasons that should be retried.
The default values are ‘rateLimitExceeded` and `userRateLimitExceeded`.
60 61 62 |
# File 'lib/gcloud/backoff.rb', line 60 def reasons @reasons end |
.retries ⇒ Object
The number of times a retriable API call should be retried.
The default value is ‘3`.
38 39 40 |
# File 'lib/gcloud/backoff.rb', line 38 def retries @retries end |
Instance Method Details
#execute_gapi ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/gcloud/backoff.rb', line 96 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_grpc ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/gcloud/backoff.rb', line 108 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 |