hydrafetch 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -3
- data/lib/hydrafetch/client.rb +41 -5
- data/lib/hydrafetch/errors.rb +4 -3
- data/lib/hydrafetch/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 965080a26c2ea8d7dff1ba22650813befcb8550e1ff34f24a35f797498414e3e
|
|
4
|
+
data.tar.gz: 953227cd85d06a7a7e57c7f36a7ed890648b7fe48478f60bff4ce07adac1fb2d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7847bb450f9dda5693519f82e3899326508d398c7ace1055a10671699013f7589b2944f2bad0abc374207a0fcfdbb87e2e341c5972dd7e947ef8ac3c657aea22
|
|
7
|
+
data.tar.gz: c9fddce74fb9b98987147d85cbac5a00650cbe2578348531f31025c016545cf75984c8cbc533c45a927096ef560f8371d6702f025880099fa1ccaca759e28c2d
|
data/README.md
CHANGED
|
@@ -189,7 +189,7 @@ rescue Hydrafetch::Error => e
|
|
|
189
189
|
return refresh_key if e.auth?
|
|
190
190
|
return top_up if e.out_of_credits?
|
|
191
191
|
return report(e.message) if e.invalid_request?
|
|
192
|
-
return enqueue(url) if e.retryable?
|
|
192
|
+
return enqueue(url, e.retry_after) if e.retryable?
|
|
193
193
|
|
|
194
194
|
warn [e.code, e.status, e.request_id].join(" ")
|
|
195
195
|
raise
|
|
@@ -202,8 +202,10 @@ end
|
|
|
202
202
|
| 401, 403 | invalid or missing key | no |
|
|
203
203
|
| 402 | out of credits | no |
|
|
204
204
|
| 404 | page does not exist | no |
|
|
205
|
-
| 429 | rate limited | yes, twice
|
|
206
|
-
| 5xx | upstream failure | yes, twice
|
|
205
|
+
| 429 | rate limited | yes, twice |
|
|
206
|
+
| 5xx | upstream failure | yes, twice |
|
|
207
|
+
|
|
208
|
+
Retries wait as long as the response asks through `Retry-After` or `retryAfterSeconds`, and otherwise back off with jitter. When the response asks for more than 30 seconds the client does not wait; it raises at once and `e.retry_after` gives the seconds to wait before asking again. A dropped connection is retried too, including while the client follows a job, and a job that fails is never started a second time. Every POST carries an Idempotency-Key that its own retries reuse, so a retry after a dropped connection returns the first answer instead of running and charging again.
|
|
207
209
|
|
|
208
210
|
A 503 from `scrape` means the origin is unreachable, usually a dead domain or a broken certificate.
|
|
209
211
|
|
data/lib/hydrafetch/client.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require "json"
|
|
2
|
+
require "securerandom"
|
|
2
3
|
require "net/http"
|
|
3
4
|
require "time"
|
|
4
5
|
require "uri"
|
|
@@ -14,6 +15,9 @@ module Hydrafetch
|
|
|
14
15
|
ACCEPTED = 202
|
|
15
16
|
CALL_JOB_TIMEOUT = 330
|
|
16
17
|
CALL_JOB_POLL = 2
|
|
18
|
+
MAX_RETRY_WAIT = 30
|
|
19
|
+
BACKOFF_CAP = 8.0
|
|
20
|
+
TRANSPORT_ERRORS = [SocketError, IOError, SystemCallError].freeze
|
|
17
21
|
|
|
18
22
|
attr_writer :call_job_poll
|
|
19
23
|
|
|
@@ -182,10 +186,12 @@ module Hydrafetch
|
|
|
182
186
|
uri.query = URI.encode_www_form(query) unless query.nil? || query.empty?
|
|
183
187
|
|
|
184
188
|
attempt = 0
|
|
189
|
+
idempotency_key = verb == Net::HTTP::Post ? SecureRandom.uuid : nil
|
|
185
190
|
begin
|
|
186
191
|
req = verb.new(uri)
|
|
187
192
|
req["X-API-Key"] = @api_key
|
|
188
193
|
req["Accept"] = "application/json"
|
|
194
|
+
req["Idempotency-Key"] = idempotency_key if idempotency_key
|
|
189
195
|
if body
|
|
190
196
|
req["Content-Type"] = "application/json"
|
|
191
197
|
req.body = JSON.generate(body)
|
|
@@ -194,20 +200,49 @@ module Hydrafetch
|
|
|
194
200
|
res = http_for(uri).request(req)
|
|
195
201
|
@last_request_id = res["x-request-id"] || @last_request_id
|
|
196
202
|
result = unwrap(res)
|
|
197
|
-
poll = res.code.to_i == ACCEPTED && result.is_a?(Hash) ? result["poll"] : nil
|
|
198
|
-
poll ? follow_call_job(poll) : result
|
|
199
203
|
rescue Error => e
|
|
200
|
-
|
|
204
|
+
wait = retry_wait(e, attempt)
|
|
205
|
+
raise unless e.retryable? && attempt < @max_retries && wait
|
|
201
206
|
|
|
202
207
|
attempt += 1
|
|
203
|
-
sleep
|
|
208
|
+
sleep wait
|
|
204
209
|
retry
|
|
205
210
|
rescue Net::OpenTimeout, Net::ReadTimeout => e
|
|
206
211
|
raise TimeoutError.new(
|
|
207
212
|
"Request to #{path} timed out after #{@timeout}s. Raise timeout:, " \
|
|
208
213
|
"or use the crawl and batch job endpoints for long work. (#{e.class})"
|
|
209
214
|
)
|
|
215
|
+
rescue *TRANSPORT_ERRORS
|
|
216
|
+
raise unless attempt < @max_retries
|
|
217
|
+
|
|
218
|
+
sleep backoff(attempt)
|
|
219
|
+
attempt += 1
|
|
220
|
+
retry
|
|
221
|
+
end
|
|
222
|
+
poll = res.code.to_i == ACCEPTED && result.is_a?(Hash) ? result["poll"] : nil
|
|
223
|
+
poll ? follow_call_job(poll) : result
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def retry_wait(error, attempt)
|
|
227
|
+
return backoff(attempt) if error.retry_after.nil?
|
|
228
|
+
|
|
229
|
+
error.retry_after > MAX_RETRY_WAIT ? nil : error.retry_after
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def backoff(attempt)
|
|
233
|
+
rand * [BACKOFF_CAP, 0.5 * (2**attempt)].min
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def retry_after(res, details)
|
|
237
|
+
header = res["retry-after"].to_s.strip
|
|
238
|
+
return header.to_i if header.match?(/\A\d+\z/)
|
|
239
|
+
return nil unless details.is_a?(Hash)
|
|
240
|
+
|
|
241
|
+
%w[retryAfterSeconds retryAfter].each do |key|
|
|
242
|
+
value = details[key]
|
|
243
|
+
return value if value.is_a?(Integer) && value >= 0
|
|
210
244
|
end
|
|
245
|
+
nil
|
|
211
246
|
end
|
|
212
247
|
|
|
213
248
|
def follow_call_job(poll)
|
|
@@ -261,7 +296,8 @@ module Hydrafetch
|
|
|
261
296
|
code: (error && error["code"]) || "HTTP_#{status}",
|
|
262
297
|
status: status,
|
|
263
298
|
request_id: meta && meta["requestId"],
|
|
264
|
-
details: error && error["details"]
|
|
299
|
+
details: error && error["details"],
|
|
300
|
+
retry_after: retry_after(res, error && error["details"])
|
|
265
301
|
)
|
|
266
302
|
end
|
|
267
303
|
|
data/lib/hydrafetch/errors.rb
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
module Hydrafetch
|
|
2
2
|
class Error < StandardError
|
|
3
|
-
attr_reader :code, :status, :request_id, :details
|
|
3
|
+
attr_reader :code, :status, :request_id, :details, :retry_after
|
|
4
4
|
|
|
5
|
-
def initialize(message, code: "UNKNOWN", status: 500, request_id: nil, details: nil)
|
|
5
|
+
def initialize(message, code: "UNKNOWN", status: 500, request_id: nil, details: nil, retry_after: nil)
|
|
6
6
|
super(message)
|
|
7
7
|
@code = code
|
|
8
8
|
@status = status
|
|
9
9
|
@request_id = request_id
|
|
10
10
|
@details = details
|
|
11
|
+
@retry_after = retry_after
|
|
11
12
|
end
|
|
12
13
|
|
|
13
14
|
def to_s
|
|
@@ -30,7 +31,7 @@ module Hydrafetch
|
|
|
30
31
|
end
|
|
31
32
|
|
|
32
33
|
def retryable?
|
|
33
|
-
status == 429 || status >= 500
|
|
34
|
+
status == 429 || status >= 500 || code == "IDEMPOTENCY_IN_PROGRESS"
|
|
34
35
|
end
|
|
35
36
|
|
|
36
37
|
def timeout?
|
data/lib/hydrafetch/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hydrafetch
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Hydrafetch
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: Turn any URL into clean Markdown and structured data. Scrape, map, search,
|
|
14
14
|
extract, brand and bulk crawl through one API.
|