ocean-rails 3.3.3 → 3.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91cff40a2e3883b3d68b9b54b1640dfe6b5c9fb7
4
- data.tar.gz: 6fa15184b9e0eecf15b0b666bdf494a51b0cd0f9
3
+ metadata.gz: e35b6a26426816dec30071f7fd2c87272e4530d7
4
+ data.tar.gz: ac1e42b243d0985e4aba91f2b2939ec0e3b3d0c4
5
5
  SHA512:
6
- metadata.gz: 6e69bbaeb59be763814f41cfc3f9413af265f24da76e1109b995c6bdc82b90fdcb6dc40a527c901a9ed17078a2d253fc8a46c3443d4ed24531246e560a4b4cf3
7
- data.tar.gz: c4c6bcdc5890377111480a8b8e9b697d10dc72f0f34bc63ffb0feb3a6714d35a442d14edfb58f2a4825e947d86069254851537188451dabd8a70eeb5fc4d7c1e
6
+ metadata.gz: 00265cc72834b55c5c5c9e4e4d2f672dddd14ebbbd20baf65c9d28d2898138054d7fd9f08a36c5eacfdc025a04cda1f83373ffe0cda7306fd195cd6db8dde6f1
7
+ data.tar.gz: 191b0d669a58c0efcac288da5a6605f6d97fcbc62d89a650333f4083fec9da7938fe2b037412a1b20a28f4d2c03ec55582064ceb3855ac820ae71e118f064b9f
data/lib/ocean/api.rb CHANGED
@@ -145,11 +145,25 @@ class Api
145
145
  # +ssl_verifypeer+ (true by default), controls SSL peer verification.
146
146
  # +ssl_verifyhost+ (2 by default), controls SSL host verification.
147
147
  #
148
+ # Automatic retries for GET requests are available:
149
+ #
150
+ # +retries+, if given and > 0, specifies the number of retries to perform for GET requests.
151
+ # Defaults to 0, meaning no retries.
152
+ # +backoff_time+ (default 1) the initial time to wait between retries.
153
+ # +backoff_rate+ (default 0.9) the rate at which the time is increased.
154
+ # +backoff_max+ (default 30) the maximum time to wait between retries.
155
+ #
156
+ # The backoff time is increased after each wait period by the product of itself and
157
+ # backoff_rate. The backoff time is capped by backoff_max. The default time and rate
158
+ # settings will generate the progression 1, 1.9, 3.61, 6.859, 13.0321, 24.76099, etc.
159
+ # To disable waiting between retries, set +backoff_time+ to zero.
160
+ #
148
161
  def self.request(url, http_method, args: nil, headers: {}, body: nil,
149
162
  credentials: nil,
150
163
  x_api_token: headers['X-API-Token'],
151
164
  reauthentication: true,
152
- ssl_verifypeer: true, ssl_verifyhost: 2)
165
+ ssl_verifypeer: true, ssl_verifyhost: 2,
166
+ retries: 0, backoff_time: 1, backoff_rate: 0.9, backoff_max: 30)
153
167
  # Set up the request
154
168
  headers['Accept'] = "application/json"
155
169
  headers['Content-Type'] = "application/json" if [:post, :put].include?(http_method)
@@ -157,39 +171,59 @@ class Api
157
171
  x_api_token = Api.authenticate(*Api.decode_credentials(credentials)) if x_api_token.blank? && credentials.present?
158
172
  headers['X-API-Token'] = x_api_token if x_api_token.present?
159
173
 
174
+ response = nil
175
+ tries_done = 0
176
+ max_tries = [:get, "GET", "get"].include?(http_method) ? retries + 1 : 1
160
177
  while (true) do
161
- request = Typhoeus::Request.new(url,
162
- method: http_method,
163
- headers: headers,
164
- params: args,
165
- body: body,
166
- ssl_verifypeer: ssl_verifypeer,
167
- ssl_verifyhost: ssl_verifyhost)
168
- # Run it
169
- response = Response.new(request.run)
170
- # If successful, return
171
- return response if response.success?
172
-
173
- # Not successful, deal with it
174
- if response.timed_out?
175
- raise Api::TimeoutError, "Api.request timed out"
176
- elsif response.status == 0
177
- # Could not get an http response, something's wrong.
178
- raise Api::NoResponseError, "Api.request could not obtain a response"
179
- elsif [400, 419].include?(response.status) && reauthentication && x_api_token.present?
180
- # Re-authenticate and retry
181
- if credentials
182
- x_api_token = Api.authenticate(*Api.decode_credentials(credentials))
183
- headers['X-API-Token'] = x_api_token
178
+ tries_done += 1
179
+ last_try = tries_done >= max_tries
180
+
181
+ while (true) do
182
+ request = Typhoeus::Request.new(url,
183
+ method: http_method,
184
+ headers: headers,
185
+ params: args,
186
+ body: body,
187
+ ssl_verifypeer: ssl_verifypeer,
188
+ ssl_verifyhost: ssl_verifyhost)
189
+ # Run it
190
+ response = Response.new(request.run)
191
+ # If successful, return
192
+ return response if response.success?
193
+
194
+ # Not successful, deal with it
195
+ if last_try
196
+ raise Api::TimeoutError, "Api.request timed out" if response.timed_out?
197
+ raise Api::NoResponseError, "Api.request could not obtain a response" if response.status == 0
184
198
  else
185
- Api.reset_service_token
186
- headers['X-API-Token'] = Api.service_token
199
+ break if response.timed_out?
200
+ break if response.status == 0
187
201
  end
188
- reauthentication = false # This prevents us from ending up here twice
189
- else
190
- # Failed
191
- break
202
+
203
+ # Reauthenticate if necessary
204
+ if [400, 419].include?(response.status) && reauthentication && x_api_token.present?
205
+ # Re-authenticate and retry
206
+ if credentials
207
+ x_api_token = Api.authenticate(*Api.decode_credentials(credentials))
208
+ headers['X-API-Token'] = x_api_token
209
+ else
210
+ Api.reset_service_token
211
+ headers['X-API-Token'] = Api.service_token
212
+ end
213
+ reauthentication = false # This prevents us from ending up here twice
214
+ elsif (400..499).include?(response.status)
215
+ return response
216
+ else
217
+ # Retry on 500s (will we ever get 300s?)
218
+ break
219
+ end
220
+
192
221
  end
222
+ # Retry
223
+ break if last_try
224
+ sleep backoff_time
225
+ seconds = [backoff_time + backoff_time * backoff_rate, 30].min
226
+ backoff_time = seconds
193
227
  end
194
228
  # Return the wrapped response to the failed request
195
229
  response
@@ -86,7 +86,7 @@ class Api
86
86
 
87
87
  def initialize(uri, type="application/json",
88
88
  credentials: nil, x_api_token: nil,
89
- retries: 3, backoff_time: 1, backoff_rate: 2, backoff_max: 30)
89
+ retries: 3, backoff_time: 1, backoff_rate: 0.9, backoff_max: 30)
90
90
  super()
91
91
  @uri = uri
92
92
  @content_type = type
@@ -171,7 +171,9 @@ class Api
171
171
  credentials = nil
172
172
  token = rr.x_api_token || Api.service_token
173
173
  end
174
- response = Api.request rr.uri, :get, headers: {}, credentials: credentials, x_api_token: token
174
+ response = Api.request rr.uri, :get, headers: {}, credentials: credentials, x_api_token: token,
175
+ retries: rr.retries, backoff_time: rr.backoff_time, backoff_rate: rr.backoff_rate,
176
+ backoff_max: rr.backoff_max
175
177
  rr.send :status=, response.status
176
178
  rr.send :status_message=, response.message
177
179
  rr.send :headers=, response.headers
data/lib/ocean/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ocean
2
- VERSION = "3.3.3"
2
+ VERSION = "3.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocean-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.3
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Bengtson