pebblebed 0.4.7 → 0.4.8

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: d9db9a533cd4003a5226ef344b853e5829f11a7d
4
- data.tar.gz: 509e88d84a3216dadccf4f59587ae172a22b90e5
3
+ metadata.gz: 2c4f02f22d36e635fa1b9e4cd2ad2147e4109838
4
+ data.tar.gz: dccd0074c577c00bf41bab362c87d2e7160775cd
5
5
  SHA512:
6
- metadata.gz: 331dab24969bf78c97e848e6b5959fe848606c557fe6331df85043aed19ae7469d52aad70e121c69347d28f321c97b60826e917091e3f3a515c88c35e8436f39
7
- data.tar.gz: 61bb3e6482904ff6bda0f3a6f2cb176182d4d11a25c0529a0d8ed664025ec434d8e1b1278b472b616ad2b499e894f72a86ae5ef864007ac606ae0dff7f0bd089
6
+ metadata.gz: 176311055e595f13967ca1e717c5b2cefd30bcb5540ac4c8b044dd21bc0e62de4e7d65a06ac7636aec135742faad4bca937b8ccaaa1de5b3e90cd9d9fd0d02ae
7
+ data.tar.gz: 38df5252016a82d768f2ed22d94d59d7d43180add7ee719d7d19dff3fe7b34a1e5d827b828a739954b3009dd15066763c0f1a940e60af658d9c2d5ba4437b943
@@ -48,6 +48,7 @@ module Pebblebed
48
48
 
49
49
  module Http
50
50
 
51
+ DEFAULT_REQUEST_TIMEOUT = 30
51
52
  DEFAULT_CONNECT_TIMEOUT = 30
52
53
  DEFAULT_READ_TIMEOUT = 30
53
54
  DEFAULT_WRITE_TIMEOUT = 60
@@ -93,7 +94,7 @@ module Pebblebed
93
94
  def self.post(url, params, &block)
94
95
  url, params, query = url_and_params_from_args(url, params, &block)
95
96
  content_type, body = serialize_params(params)
96
- return do_request(url, idempotent: false) { |connection|
97
+ return do_request(url) { |connection|
97
98
  connection.post(
98
99
  :host => url.host,
99
100
  :path => url.path,
@@ -153,7 +154,7 @@ module Pebblebed
153
154
 
154
155
  def self.stream_get(url = nil, params = nil, headers: {}, on_data:)
155
156
  url, params, query = url_and_params_from_args(url, params)
156
- return do_request(url) { |connection|
157
+ return do_request(url, share: false) { |connection|
157
158
  connection.get(
158
159
  :host => url.host,
159
160
  :path => url.path,
@@ -168,8 +169,7 @@ module Pebblebed
168
169
  def self.stream_post(url, params, headers: {}, on_data:)
169
170
  url, params, query = url_and_params_from_args(url, params)
170
171
  content_type, body = serialize_params(params)
171
-
172
- return do_request(url) { |connection|
172
+ return do_request(url, share: false) { |connection|
173
173
  connection.post(
174
174
  :host => url.host,
175
175
  :path => url.path,
@@ -185,13 +185,10 @@ module Pebblebed
185
185
  }
186
186
  end
187
187
 
188
- def self.stream_put(url, params, options = {})
189
- on_data = options[:on_data] or raise "Option :on_data must be specified"
190
-
188
+ def self.stream_put(url, params, on_data:)
191
189
  url, params, query = url_and_params_from_args(url, params)
192
190
  content_type, body = serialize_params(params)
193
-
194
- return do_request(url) { |connection|
191
+ return do_request(url, share: false) { |connection|
195
192
  connection.put(
196
193
  :host => url.host,
197
194
  :path => url.path,
@@ -242,37 +239,47 @@ module Pebblebed
242
239
  response
243
240
  end
244
241
 
245
- def self.do_request(url, idempotent: true, &block)
246
- with_connection(url, idempotent) { |connection|
247
- begin
248
- request = block.call(connection)
249
- response = Response.new(url, request.status, request.body)
250
- return handle_http_errors(response)
251
- rescue Excon::Errors::Timeout => error
252
- raise HttpTimeoutError.new(error)
253
- rescue Excon::Errors::SocketError => error
254
- raise HttpSocketError.new(error) unless idempotent
255
- # Connection failed, close the connection and try again
256
- connection.reset
242
+ def self.do_request(url, share: true, idempotent: false, &block)
243
+ reset = false
244
+ return with_retries(timeout: (idempotent ? -1 : self.read_timeout) || DEFAULT_REQUEST_TIMEOUT) {
245
+ return with_connection(url, share: share) { |connection|
246
+ connection.reset if reset
247
+ reset = true # On next retry
248
+
257
249
  begin
258
250
  request = block.call(connection)
259
251
  response = Response.new(url, request.status, request.body)
260
252
  return handle_http_errors(response)
253
+ rescue Excon::Errors::Timeout => error
254
+ raise HttpTimeoutError.new(error)
261
255
  rescue Excon::Errors::SocketError => error
262
256
  raise HttpSocketError.new(error)
263
257
  end
264
- end
258
+ }
265
259
  }
266
260
  end
267
261
 
268
- def self.with_connection(url, idempotent, &block)
269
- connection = self.current_connection(url)
270
- if connection
271
- connection.reset unless idempotent
272
- else
273
- connection = new_connection(url)
262
+ def self.with_retries(timeout: 0, &block)
263
+ deadline = Time.now + timeout
264
+ interval = 0.1
265
+ begin
266
+ return yield
267
+ rescue HttpTimeoutError, HttpSocketError => e
268
+ raise if Time.now >= deadline
269
+ sleep(interval)
270
+ interval = [30, interval * 2].min
271
+ retry
272
+ end
273
+ end
274
+
275
+ def self.with_connection(url, share: false, &block)
276
+ unless share
277
+ return yield new_connection(url)
274
278
  end
275
- self.current_connection={:url => url, :connection => connection}
279
+
280
+ connection = self.current_connection(url)
281
+ connection ||= new_connection(url)
282
+ self.current_connection = {url: url, connection: connection}
276
283
  yield connection
277
284
  end
278
285
 
@@ -1,3 +1,3 @@
1
1
  module Pebblebed
2
- VERSION = "0.4.7"
2
+ VERSION = "0.4.8"
3
3
  end
data/spec/http_spec.rb CHANGED
@@ -157,7 +157,7 @@ describe Pebblebed::Http do
157
157
  }
158
158
  Excon.stub({:method => :get}) { |params|
159
159
  run_count += 1
160
- if run_count <= 2
160
+ if run_count <= 10
161
161
  raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
162
162
  end
163
163
  {:body => params[:body], :headers => params[:headers], :status => 200}
@@ -178,7 +178,7 @@ describe Pebblebed::Http do
178
178
  expect {
179
179
  Pebblebed::Http.get(pebble_url, {hello: 'world'})
180
180
  }.to raise_error(Pebblebed::HttpSocketError)
181
- expect(run_count).to equal(2)
181
+ expect(run_count).to be > 2
182
182
  end
183
183
 
184
184
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pebblebed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katrina Owen