hydrafetch 0.6.0 → 0.7.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 +38 -5
- data/lib/hydrafetch/errors.rb +3 -2
- 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: bdf2b68d39f76d921042d0995c8686d58716b288884c919b6302943ee852f591
|
|
4
|
+
data.tar.gz: 61cfab340278a3bbae9c5af93bd7d3a492e28ca3d2ff8ccb6a40dcfd6d9f9c5b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4da1f0021b7f34e10f403ef305e560d0f6f0b2782fb280d2b96ef92fe70d8176402cc91fe5308ed799d03cbfcec8162942aec5dc05edbe553c8e6ecd470df411
|
|
7
|
+
data.tar.gz: 2d1a22c4113af9597730eeaa99470cd7e505b35d8985869e1ee9449778c56bf38b14737e7be8d011a80715d14e45704a9df6f8186fe7d0611812441ed20d78c9
|
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.
|
|
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
|
@@ -14,6 +14,9 @@ module Hydrafetch
|
|
|
14
14
|
ACCEPTED = 202
|
|
15
15
|
CALL_JOB_TIMEOUT = 330
|
|
16
16
|
CALL_JOB_POLL = 2
|
|
17
|
+
MAX_RETRY_WAIT = 30
|
|
18
|
+
BACKOFF_CAP = 8.0
|
|
19
|
+
TRANSPORT_ERRORS = [SocketError, IOError, SystemCallError].freeze
|
|
17
20
|
|
|
18
21
|
attr_writer :call_job_poll
|
|
19
22
|
|
|
@@ -194,20 +197,49 @@ module Hydrafetch
|
|
|
194
197
|
res = http_for(uri).request(req)
|
|
195
198
|
@last_request_id = res["x-request-id"] || @last_request_id
|
|
196
199
|
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
200
|
rescue Error => e
|
|
200
|
-
|
|
201
|
+
wait = retry_wait(e, attempt)
|
|
202
|
+
raise unless e.retryable? && attempt < @max_retries && wait
|
|
201
203
|
|
|
202
204
|
attempt += 1
|
|
203
|
-
sleep
|
|
205
|
+
sleep wait
|
|
204
206
|
retry
|
|
205
207
|
rescue Net::OpenTimeout, Net::ReadTimeout => e
|
|
206
208
|
raise TimeoutError.new(
|
|
207
209
|
"Request to #{path} timed out after #{@timeout}s. Raise timeout:, " \
|
|
208
210
|
"or use the crawl and batch job endpoints for long work. (#{e.class})"
|
|
209
211
|
)
|
|
212
|
+
rescue *TRANSPORT_ERRORS
|
|
213
|
+
raise unless attempt < @max_retries
|
|
214
|
+
|
|
215
|
+
sleep backoff(attempt)
|
|
216
|
+
attempt += 1
|
|
217
|
+
retry
|
|
218
|
+
end
|
|
219
|
+
poll = res.code.to_i == ACCEPTED && result.is_a?(Hash) ? result["poll"] : nil
|
|
220
|
+
poll ? follow_call_job(poll) : result
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def retry_wait(error, attempt)
|
|
224
|
+
return backoff(attempt) if error.retry_after.nil?
|
|
225
|
+
|
|
226
|
+
error.retry_after > MAX_RETRY_WAIT ? nil : error.retry_after
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def backoff(attempt)
|
|
230
|
+
rand * [BACKOFF_CAP, 0.5 * (2**attempt)].min
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def retry_after(res, details)
|
|
234
|
+
header = res["retry-after"].to_s.strip
|
|
235
|
+
return header.to_i if header.match?(/\A\d+\z/)
|
|
236
|
+
return nil unless details.is_a?(Hash)
|
|
237
|
+
|
|
238
|
+
%w[retryAfterSeconds retryAfter].each do |key|
|
|
239
|
+
value = details[key]
|
|
240
|
+
return value if value.is_a?(Integer) && value >= 0
|
|
210
241
|
end
|
|
242
|
+
nil
|
|
211
243
|
end
|
|
212
244
|
|
|
213
245
|
def follow_call_job(poll)
|
|
@@ -261,7 +293,8 @@ module Hydrafetch
|
|
|
261
293
|
code: (error && error["code"]) || "HTTP_#{status}",
|
|
262
294
|
status: status,
|
|
263
295
|
request_id: meta && meta["requestId"],
|
|
264
|
-
details: error && error["details"]
|
|
296
|
+
details: error && error["details"],
|
|
297
|
+
retry_after: retry_after(res, error && error["details"])
|
|
265
298
|
)
|
|
266
299
|
end
|
|
267
300
|
|
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
|
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.7.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.
|