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 +4 -4
- data/lib/pebblebed/http.rb +36 -29
- data/lib/pebblebed/version.rb +1 -1
- data/spec/http_spec.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c4f02f22d36e635fa1b9e4cd2ad2147e4109838
|
4
|
+
data.tar.gz: dccd0074c577c00bf41bab362c87d2e7160775cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 176311055e595f13967ca1e717c5b2cefd30bcb5540ac4c8b044dd21bc0e62de4e7d65a06ac7636aec135742faad4bca937b8ccaaa1de5b3e90cd9d9fd0d02ae
|
7
|
+
data.tar.gz: 38df5252016a82d768f2ed22d94d59d7d43180add7ee719d7d19dff3fe7b34a1e5d827b828a739954b3009dd15066763c0f1a940e60af658d9c2d5ba4437b943
|
data/lib/pebblebed/http.rb
CHANGED
@@ -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
|
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,
|
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,
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
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
|
-
|
258
|
+
}
|
265
259
|
}
|
266
260
|
end
|
267
261
|
|
268
|
-
def self.
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
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
|
-
|
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
|
|
data/lib/pebblebed/version.rb
CHANGED
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 <=
|
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
|
181
|
+
expect(run_count).to be > 2
|
182
182
|
end
|
183
183
|
|
184
184
|
end
|