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.
require "gcloud/backoff"
Gcloud::Backoff.retries = 5 # Set a maximum of five retries per call
Class Attribute Summary collapse
-
.backoff ⇒ Object
The code to run when a backoff is handled.
-
.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 ⇒ Object
:nodoc:.
-
#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
75 76 77 78 79 80 |
# File 'lib/gcloud/backoff.rb', line 75 def initialize = {} #:nodoc: @max_retries = ([:retries] || Backoff.retries).to_i @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.
58 59 60 |
# File 'lib/gcloud/backoff.rb', line 58 def backoff @backoff end |
.http_codes ⇒ Object
The HTTP Status Codes that should be retried.
The default values are 500 and 503.
43 44 45 |
# File 'lib/gcloud/backoff.rb', line 43 def http_codes @http_codes end |
.reasons ⇒ Object
The Google API error reasons that should be retried.
The default values are rateLimitExceeded and userRateLimitExceeded.
50 51 52 |
# File 'lib/gcloud/backoff.rb', line 50 def reasons @reasons end |
.retries ⇒ Object
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 ⇒ Object
:nodoc:
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/gcloud/backoff.rb', line 82 def execute #:nodoc: current_retries = 0 loop do result = yield # Expecting Google::APIClient::Result return result if result.success? break result unless retry? result, current_retries current_retries += 1 @backoff.call current_retries end end |