scrapely-python 0.1.11__py3-none-any.whl → 0.1.12__py3-none-any.whl
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.
- scrapely/__init__.py +1 -1
- scrapely/actor.py +4 -1
- scrapely/charging.py +58 -25
- scrapely/storage/api_client.py +132 -98
- {scrapely_python-0.1.11.dist-info → scrapely_python-0.1.12.dist-info}/METADATA +1 -1
- {scrapely_python-0.1.11.dist-info → scrapely_python-0.1.12.dist-info}/RECORD +11 -11
- {scrapely_python-0.1.11.dist-info → scrapely_python-0.1.12.dist-info}/WHEEL +0 -0
- {scrapely_python-0.1.11.dist-info → scrapely_python-0.1.12.dist-info}/licenses/LICENSE +0 -0
- {scrapely_python-0.1.11.dist-info → scrapely_python-0.1.12.dist-info}/licenses/LICENSE-APACHE +0 -0
- {scrapely_python-0.1.11.dist-info → scrapely_python-0.1.12.dist-info}/licenses/NOTICE +0 -0
- {scrapely_python-0.1.11.dist-info → scrapely_python-0.1.12.dist-info}/top_level.txt +0 -0
scrapely/__init__.py
CHANGED
scrapely/actor.py
CHANGED
|
@@ -491,7 +491,7 @@ class Actor(metaclass=_ActorMeta):
|
|
|
491
491
|
if flush is not None:
|
|
492
492
|
deadline_ms = min(max(1, timeout_secs) * 1000, 30_000)
|
|
493
493
|
# The flush's own deadline ends its retries (charge POSTs back off
|
|
494
|
-
# for up to ~
|
|
494
|
+
# for up to ~2 min) a little before wait_for would cancel it.
|
|
495
495
|
await asyncio.wait_for(
|
|
496
496
|
flush(max_intents=64, deadline_ms=max(500, deadline_ms - 2_000)),
|
|
497
497
|
timeout=max(1.0, deadline_ms / 1000.0),
|
|
@@ -1067,12 +1067,15 @@ class Actor(metaclass=_ActorMeta):
|
|
|
1067
1067
|
return None
|
|
1068
1068
|
|
|
1069
1069
|
client = cls.new_client(token=token, base_url=api_url)
|
|
1070
|
+
# Setting the message is idempotent: a 429 (run API rate limit), 5xx or
|
|
1071
|
+
# dropped connection is retried with backoff instead of failing the run.
|
|
1070
1072
|
response = await client.put(
|
|
1071
1073
|
f'/v2/actor-runs/{run_id}',
|
|
1072
1074
|
json_data={
|
|
1073
1075
|
'statusMessage': message,
|
|
1074
1076
|
'isStatusMessageTerminal': is_terminal,
|
|
1075
1077
|
},
|
|
1078
|
+
idempotent=True,
|
|
1076
1079
|
)
|
|
1077
1080
|
body: Any = {}
|
|
1078
1081
|
if response.content:
|
scrapely/charging.py
CHANGED
|
@@ -49,16 +49,26 @@ CHARGE_IDEMPOTENCY_SAFE_AGE_SECS = 30 * 24 * 60 * 60
|
|
|
49
49
|
# Soft cap on open unresolved intents. Refuse NEW billable charges when full.
|
|
50
50
|
_PENDING_MAX_ENTRIES = 1024
|
|
51
51
|
|
|
52
|
-
# Same-key POST attempts before leaving an intent pending /
|
|
53
|
-
# client itself retries 5xx answers and refused or reset
|
|
54
|
-
# POSTs with
|
|
52
|
+
# Same-key POST attempts before leaving an intent pending / re-queueing a void.
|
|
53
|
+
# The API client itself retries 429 and 5xx answers and refused or reset
|
|
54
|
+
# connections of these POSTs with exponential backoff (honouring Retry-After)
|
|
55
|
+
# for its retry window (2 minutes, SCRAPELY_API_RETRY_WINDOW_SECS): re-sending
|
|
55
56
|
# is safe because every attempt carries the same Idempotency-Key. These outer
|
|
56
57
|
# attempts cover what the client gives up on quickly (timeouts, other
|
|
57
|
-
# transport errors),
|
|
58
|
+
# transport errors), within _RETRY_BUDGET_SECS. Then the charge stays pending
|
|
59
|
+
# (the void is re-queued) and is retried before the next push and at exit,
|
|
60
|
+
# the same as the JS SDK.
|
|
58
61
|
_POST_ATTEMPTS = 8
|
|
59
62
|
|
|
60
|
-
# Outer retries stop once this much time has passed since the first attempt
|
|
61
|
-
|
|
63
|
+
# Outer retries stop once this much time has passed since the first attempt
|
|
64
|
+
# (the API retry window; JS SDK parity).
|
|
65
|
+
_RETRY_BUDGET_SECS = 120.0
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _is_transient_status(status: Optional[int]) -> bool:
|
|
69
|
+
"""408/429 and 5xx: the platform could not take the request now; try again later."""
|
|
70
|
+
return status is not None and (status in (408, 429) or status >= 500)
|
|
71
|
+
|
|
62
72
|
|
|
63
73
|
# Local-only dataset collecting one entry per logical charge operation
|
|
64
74
|
# (JS ChargingManager LOCAL_CHARGING_LOG_DATASET_NAME parity).
|
|
@@ -563,7 +573,12 @@ class ChargingManager:
|
|
|
563
573
|
return await self._void_charge(event_name, operation_id, reserved, not_found_means_voided=True)
|
|
564
574
|
|
|
565
575
|
async def _void_charge(
|
|
566
|
-
self,
|
|
576
|
+
self,
|
|
577
|
+
event_name: str,
|
|
578
|
+
operation_id: str,
|
|
579
|
+
reserved: int,
|
|
580
|
+
not_found_means_voided: bool = False,
|
|
581
|
+
deadline: Optional[float] = None,
|
|
567
582
|
) -> bool:
|
|
568
583
|
"""Reverse an accepted charge whose items were never stored, then unwind locally.
|
|
569
584
|
|
|
@@ -573,6 +588,8 @@ class ChargingManager:
|
|
|
573
588
|
if self._client is None or not self._is_at_home or not self._actor_run_id:
|
|
574
589
|
return False
|
|
575
590
|
started = time.monotonic()
|
|
591
|
+
budget_end = started + self._RETRY_BUDGET_SECS
|
|
592
|
+
deadline = budget_end if deadline is None else min(deadline, budget_end)
|
|
576
593
|
for attempt in range(self._POST_ATTEMPTS):
|
|
577
594
|
try:
|
|
578
595
|
await self._client.post(
|
|
@@ -580,6 +597,7 @@ class ChargingManager:
|
|
|
580
597
|
json_data={},
|
|
581
598
|
headers={'Idempotency-Key': operation_id},
|
|
582
599
|
idempotent=True,
|
|
600
|
+
retry_deadline=deadline,
|
|
583
601
|
)
|
|
584
602
|
self._unwind_reservation(event_name, reserved)
|
|
585
603
|
return True
|
|
@@ -592,24 +610,37 @@ class ChargingManager:
|
|
|
592
610
|
self._unwind_reservation(event_name, reserved)
|
|
593
611
|
return True
|
|
594
612
|
# 404: nothing to void; 409: already settled; 4xx auth: permanent.
|
|
595
|
-
|
|
613
|
+
# 408/429 (rate limited) and 5xx are not answers: re-queue below.
|
|
614
|
+
if status is not None and 400 <= status < 500 and not _is_transient_status(status):
|
|
596
615
|
return False
|
|
597
|
-
if not await self._backoff_before_retry(attempt, started, err):
|
|
616
|
+
if not await self._backoff_before_retry(attempt, started, err, deadline):
|
|
598
617
|
break
|
|
599
618
|
# Outcome unknown: keep the reservation and retry before the next push / at exit.
|
|
600
619
|
self._pending_voids.append((event_name, operation_id, reserved, not_found_means_voided))
|
|
601
620
|
logger.error('Void of charge %s (%r) not confirmed yet; will retry', operation_id, event_name)
|
|
602
621
|
return False
|
|
603
622
|
|
|
604
|
-
async def _retry_pending_voids(self) -> bool:
|
|
623
|
+
async def _retry_pending_voids(self, deadline: Optional[float] = None) -> bool:
|
|
605
624
|
"""Retry unconfirmed voids (caller holds the operation lock).
|
|
606
625
|
|
|
607
|
-
|
|
626
|
+
With ``deadline`` (``time.monotonic()``, the exit flush) keeps retrying
|
|
627
|
+
with backoff while time is left. Returns False while any earlier void
|
|
628
|
+
is still unconfirmed.
|
|
608
629
|
"""
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
630
|
+
from .storage.api_client import server_retry_delay
|
|
631
|
+
|
|
632
|
+
round_ = 0
|
|
633
|
+
while True:
|
|
634
|
+
pending, self._pending_voids = self._pending_voids, []
|
|
635
|
+
for event_name, operation_id, reserved, not_found_means_voided in pending:
|
|
636
|
+
await self._void_charge(event_name, operation_id, reserved, not_found_means_voided, deadline)
|
|
637
|
+
if not self._pending_voids:
|
|
638
|
+
return True
|
|
639
|
+
left = 0.0 if deadline is None else deadline - time.monotonic()
|
|
640
|
+
if left <= 0:
|
|
641
|
+
return False
|
|
642
|
+
await asyncio.sleep(min(server_retry_delay(round_, None), left))
|
|
643
|
+
round_ += 1
|
|
613
644
|
|
|
614
645
|
def _quarantine_stale_pending(self) -> None:
|
|
615
646
|
"""Remove intents older than the safe replay window (fail closed, no replay).
|
|
@@ -656,7 +687,7 @@ class ChargingManager:
|
|
|
656
687
|
},
|
|
657
688
|
headers={'Idempotency-Key': idempotency_key},
|
|
658
689
|
idempotent=True,
|
|
659
|
-
|
|
690
|
+
retry_deadline=deadline,
|
|
660
691
|
)
|
|
661
692
|
if isinstance(response, dict):
|
|
662
693
|
return response
|
|
@@ -674,17 +705,17 @@ class ChargingManager:
|
|
|
674
705
|
) -> bool:
|
|
675
706
|
"""Sleep before the next same-key attempt; False when no attempt is left.
|
|
676
707
|
|
|
677
|
-
A 5xx answer reaching this point was already retried by the API
|
|
678
|
-
|
|
679
|
-
errors
|
|
680
|
-
|
|
708
|
+
A 429/5xx answer reaching this point was already retried by the API
|
|
709
|
+
client for the whole budget, so it ends the attempts. Timeouts, 408 and
|
|
710
|
+
other errors back off the same way while attempts and the retry budget
|
|
711
|
+
last.
|
|
681
712
|
"""
|
|
682
713
|
from .storage.api_client import server_retry_delay
|
|
683
714
|
|
|
684
715
|
if attempt + 1 >= self._POST_ATTEMPTS:
|
|
685
716
|
return False
|
|
686
717
|
status = _http_status_of(err)
|
|
687
|
-
if status is not None and status >= 500:
|
|
718
|
+
if status is not None and (status == 429 or status >= 500):
|
|
688
719
|
return False
|
|
689
720
|
delay = server_retry_delay(attempt, None)
|
|
690
721
|
if time.monotonic() - started + delay > self._RETRY_BUDGET_SECS:
|
|
@@ -714,6 +745,9 @@ class ChargingManager:
|
|
|
714
745
|
"""
|
|
715
746
|
last_error: Optional[BaseException] = None
|
|
716
747
|
started = time.monotonic()
|
|
748
|
+
# The client's own retries count against the same budget.
|
|
749
|
+
budget_end = started + self._RETRY_BUDGET_SECS
|
|
750
|
+
deadline = budget_end if deadline is None else min(deadline, budget_end)
|
|
717
751
|
for attempt in range(self._POST_ATTEMPTS):
|
|
718
752
|
try:
|
|
719
753
|
body = await self._post_charge_once(
|
|
@@ -762,11 +796,10 @@ class ChargingManager:
|
|
|
762
796
|
"""Caller must hold _operation_lock."""
|
|
763
797
|
if not self._is_at_home or self._client is None or not self._actor_run_id:
|
|
764
798
|
return 0
|
|
765
|
-
await self._retry_pending_voids()
|
|
766
|
-
|
|
767
|
-
import time
|
|
768
|
-
|
|
769
799
|
deadline = None if deadline_ms is None else (time.monotonic() + max(0.0, deadline_ms) / 1000.0)
|
|
800
|
+
# Voids first: an unconfirmed one means the user may be billed for items
|
|
801
|
+
# that were never stored. At exit (with a deadline) keep retrying them.
|
|
802
|
+
await self._retry_pending_voids(deadline)
|
|
770
803
|
confirmed_total = 0
|
|
771
804
|
async with self._flush_lock:
|
|
772
805
|
while True:
|
scrapely/storage/api_client.py
CHANGED
|
@@ -6,6 +6,7 @@ from __future__ import annotations
|
|
|
6
6
|
|
|
7
7
|
import asyncio
|
|
8
8
|
import logging
|
|
9
|
+
import os
|
|
9
10
|
import random
|
|
10
11
|
import time
|
|
11
12
|
from typing import Any, Dict, Optional, Union
|
|
@@ -36,13 +37,16 @@ def is_retryable_response(
|
|
|
36
37
|
) -> bool:
|
|
37
38
|
"""Whether a non-2xx answer is retried.
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
429 always: the run API limiter and dataset backpressure refuse it before
|
|
41
|
+
anything is written. Idempotent methods: also 500/502/503/504. Others
|
|
42
|
+
(dataset push, KV PUT) only on a 503 that certainly was not processed: the
|
|
43
|
+
rate limiter refused it ("rate-limit-unavailable", before the handler), or
|
|
44
|
+
HAProxy had no backend (its own page, nothing forwarded). Any other 503 may
|
|
45
|
+
mean "outcome unknown" (a direct dataset write whose commit was not
|
|
46
|
+
confirmed), and re-sending could store the items twice.
|
|
45
47
|
"""
|
|
48
|
+
if status == 429:
|
|
49
|
+
return True
|
|
46
50
|
if idempotent:
|
|
47
51
|
return status in (500, 502, 503, 504)
|
|
48
52
|
if status != 503:
|
|
@@ -70,22 +74,60 @@ def _error_type(response: httpx.Response) -> Optional[str]:
|
|
|
70
74
|
return None
|
|
71
75
|
|
|
72
76
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
# Default time budget for retrying one API call (429, 503, network errors).
|
|
78
|
+
DEFAULT_API_RETRY_WINDOW_SECS = 120.0
|
|
79
|
+
_MAX_BACKOFF_SECS = 30.0
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def api_retry_window_secs() -> float:
|
|
83
|
+
"""How long one API call keeps retrying before it gives up.
|
|
84
|
+
|
|
85
|
+
``SCRAPELY_API_RETRY_WINDOW_SECS`` when set, else ``ApiClient.retry_window_secs``
|
|
86
|
+
(2 minutes by default).
|
|
87
|
+
"""
|
|
88
|
+
raw = os.environ.get('SCRAPELY_API_RETRY_WINDOW_SECS')
|
|
89
|
+
if raw:
|
|
77
90
|
try:
|
|
78
|
-
|
|
91
|
+
value = float(raw)
|
|
92
|
+
if value >= 0:
|
|
93
|
+
return value
|
|
79
94
|
except ValueError:
|
|
80
|
-
|
|
81
|
-
|
|
95
|
+
pass
|
|
96
|
+
return ApiClient.retry_window_secs
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _parse_retry_after(retry_after: Optional[str], now: Optional[float]) -> Optional[float]:
|
|
100
|
+
if retry_after is None or not retry_after.strip():
|
|
101
|
+
return None
|
|
102
|
+
secs: Optional[float] = None
|
|
103
|
+
try:
|
|
104
|
+
secs = float(retry_after)
|
|
105
|
+
except ValueError:
|
|
106
|
+
try:
|
|
107
|
+
from email.utils import parsedate_to_datetime
|
|
82
108
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
109
|
+
secs = max(0.0, parsedate_to_datetime(retry_after).timestamp() - (time.time() if now is None else now))
|
|
110
|
+
except Exception:
|
|
111
|
+
secs = None
|
|
112
|
+
if secs is None or secs < 0 or secs != secs:
|
|
113
|
+
return None
|
|
114
|
+
# Not capped: a Retry-After past the retry window ends the retries instead.
|
|
115
|
+
return secs
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def server_retry_delay(attempt: int, retry_after: Optional[str], now: Optional[float] = None) -> float:
|
|
119
|
+
"""Delay in seconds before retry number ``attempt + 1``.
|
|
120
|
+
|
|
121
|
+
Exponential backoff (0.5 s doubling, capped at 30 s) with equal jitter, and
|
|
122
|
+
never earlier than the server's Retry-After (seconds or HTTP date) plus a little jitter so throttled clients don't all come back in
|
|
123
|
+
the same instant.
|
|
124
|
+
"""
|
|
125
|
+
exp: float = min(_MAX_BACKOFF_SECS, 0.5 * float(2 ** min(attempt, 16)))
|
|
126
|
+
backoff = exp / 2 + random.random() * (exp / 2)
|
|
127
|
+
ra = _parse_retry_after(retry_after, now)
|
|
128
|
+
if ra is None:
|
|
129
|
+
return backoff
|
|
130
|
+
return max(ra + random.random() * min(1.0, ra * 0.2 + 0.1), backoff)
|
|
89
131
|
|
|
90
132
|
|
|
91
133
|
class AdaptiveRateLimiter:
|
|
@@ -139,25 +181,8 @@ class AdaptiveRateLimiter:
|
|
|
139
181
|
|
|
140
182
|
self.last_request_time = time.time() * 1000
|
|
141
183
|
|
|
142
|
-
|
|
184
|
+
def record_rate_limited(self) -> None:
|
|
143
185
|
self._rate_limit_errors[429] = self._rate_limit_errors.get(429, 0) + 1
|
|
144
|
-
retry_after = response.headers.get('Retry-After')
|
|
145
|
-
wait_ms = 1000.0
|
|
146
|
-
if retry_after:
|
|
147
|
-
try:
|
|
148
|
-
wait_ms = float(retry_after) * 1000
|
|
149
|
-
except ValueError:
|
|
150
|
-
try:
|
|
151
|
-
from email.utils import parsedate_to_datetime
|
|
152
|
-
|
|
153
|
-
retry_date = parsedate_to_datetime(retry_after).timestamp() * 1000
|
|
154
|
-
wait_ms = max(0.0, retry_date - time.time() * 1000)
|
|
155
|
-
except Exception:
|
|
156
|
-
wait_ms = 1000.0
|
|
157
|
-
|
|
158
|
-
wait_ms = wait_ms + random.random() * 100
|
|
159
|
-
logger.warning('[RateLimiter] Rate limited, waiting %.0fms before retry', wait_ms)
|
|
160
|
-
await asyncio.sleep(wait_ms / 1000)
|
|
161
186
|
|
|
162
187
|
def get_rate_limit_errors(self) -> Dict[int, int]:
|
|
163
188
|
return dict(self._rate_limit_errors)
|
|
@@ -170,11 +195,10 @@ class ApiClient:
|
|
|
170
195
|
"""
|
|
171
196
|
|
|
172
197
|
_rate_limiter = AdaptiveRateLimiter()
|
|
173
|
-
|
|
174
|
-
#
|
|
175
|
-
#
|
|
176
|
-
|
|
177
|
-
_max_server_retries = 8
|
|
198
|
+
# How long one call keeps retrying 429/503/network errors before it gives
|
|
199
|
+
# up (SCRAPELY_API_RETRY_WINDOW_SECS overrides it). Long enough that a
|
|
200
|
+
# throttled run slows down instead of failing, and covers a main redeploy.
|
|
201
|
+
retry_window_secs = DEFAULT_API_RETRY_WINDOW_SECS
|
|
178
202
|
|
|
179
203
|
def __init__(
|
|
180
204
|
self,
|
|
@@ -260,12 +284,25 @@ class ApiClient:
|
|
|
260
284
|
idempotent: Optional[bool] = None,
|
|
261
285
|
retry_deadline: Optional[float] = None,
|
|
262
286
|
) -> httpx.Response:
|
|
263
|
-
"""Make an HTTP request with
|
|
287
|
+
"""Make an HTTP request with the API retry policy.
|
|
288
|
+
|
|
289
|
+
Retries, with exponential backoff and jitter that honours Retry-After,
|
|
290
|
+
until the retry window (2 minutes by default) runs out:
|
|
291
|
+
|
|
292
|
+
- 429 (run API rate limit, dataset backpressure): always; nothing was applied.
|
|
293
|
+
- 503 from the limiter or HAProxy: always; nothing was forwarded.
|
|
294
|
+
- 500/502/503/504, read/write timeouts and dropped connections:
|
|
295
|
+
idempotent calls only.
|
|
296
|
+
- Refused connections and connect/pool timeouts: always; the request
|
|
297
|
+
never left.
|
|
298
|
+
|
|
299
|
+
A write (dataset push, KV PUT) whose request may have reached the API is
|
|
300
|
+
never re-sent: there is no idempotency key for it, so a re-send could
|
|
301
|
+
store the same rows twice. Such failures surface to the caller instead.
|
|
264
302
|
|
|
265
|
-
``retry_deadline`` (a ``time.monotonic()`` value)
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
of being cancelled mid-retry.
|
|
303
|
+
``retry_deadline`` (a ``time.monotonic()`` value) ends the retries
|
|
304
|
+
earlier, so a caller with a time budget (e.g. the exit flush) gets an
|
|
305
|
+
answer instead of being cancelled mid-retry.
|
|
269
306
|
"""
|
|
270
307
|
client = await self._ensure_client()
|
|
271
308
|
url = self._url(path)
|
|
@@ -277,10 +314,30 @@ class ApiClient:
|
|
|
277
314
|
|
|
278
315
|
if idempotent is None:
|
|
279
316
|
idempotent = method.upper() in IDEMPOTENT_METHODS
|
|
280
|
-
|
|
281
|
-
|
|
317
|
+
deadline = time.monotonic() + max(0.0, api_retry_window_secs())
|
|
318
|
+
if retry_deadline is not None:
|
|
319
|
+
deadline = min(deadline, retry_deadline)
|
|
320
|
+
attempt = 0
|
|
321
|
+
|
|
322
|
+
async def backoff(why: str, retry_after: Optional[str]) -> bool:
|
|
323
|
+
"""Sleep before the next attempt; False once the retry window is used up."""
|
|
324
|
+
nonlocal attempt
|
|
325
|
+
# Retrying sooner than asked would only be refused again: give up
|
|
326
|
+
# when the next try would land past the window / deadline.
|
|
327
|
+
wait = server_retry_delay(attempt, retry_after)
|
|
328
|
+
if time.monotonic() + wait > deadline:
|
|
329
|
+
return False
|
|
330
|
+
attempt += 1
|
|
331
|
+
logger.warning('%s %s %s; retry %d in %.1fs', method, path, why, attempt, wait)
|
|
332
|
+
await asyncio.sleep(wait)
|
|
333
|
+
return True
|
|
334
|
+
|
|
282
335
|
while True:
|
|
283
336
|
await self._rate_limiter.wait_for_slot()
|
|
337
|
+
request_kwargs: Dict[str, Any] = {}
|
|
338
|
+
if retry_deadline is not None:
|
|
339
|
+
# A caller with a time budget: no attempt may outlive it.
|
|
340
|
+
request_kwargs['timeout'] = max(0.5, min(self.timeout, retry_deadline - time.monotonic()))
|
|
284
341
|
try:
|
|
285
342
|
response = await client.request(
|
|
286
343
|
method,
|
|
@@ -289,63 +346,38 @@ class ApiClient:
|
|
|
289
346
|
json=json_data,
|
|
290
347
|
content=content,
|
|
291
348
|
headers=request_headers or None,
|
|
349
|
+
**request_kwargs,
|
|
292
350
|
)
|
|
293
|
-
except httpx.
|
|
294
|
-
|
|
351
|
+
except (httpx.ConnectTimeout, httpx.PoolTimeout, httpx.ConnectError) as exc:
|
|
352
|
+
# Never reached the API: safe for every method.
|
|
353
|
+
if isinstance(exc, (httpx.ConnectTimeout, httpx.PoolTimeout)) and do_not_retry_timeouts:
|
|
295
354
|
raise
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
355
|
+
if await backoff(f'failed ({type(exc).__name__})', None):
|
|
356
|
+
continue
|
|
357
|
+
raise
|
|
358
|
+
except httpx.TimeoutException as exc:
|
|
359
|
+
# Read/write timeout: the request may have been applied.
|
|
360
|
+
if idempotent and not do_not_retry_timeouts and await backoff(f'timed out ({type(exc).__name__})', None):
|
|
361
|
+
continue
|
|
362
|
+
raise
|
|
299
363
|
except httpx.TransportError as exc:
|
|
300
|
-
#
|
|
301
|
-
|
|
302
|
-
# mid-request) only for idempotent ones.
|
|
303
|
-
wait = server_retry_delay(server_attempt, None)
|
|
304
|
-
if (
|
|
305
|
-
(idempotent or isinstance(exc, httpx.ConnectError))
|
|
306
|
-
and server_attempt < self._max_server_retries
|
|
307
|
-
and (retry_deadline is None or time.monotonic() + wait <= retry_deadline)
|
|
308
|
-
):
|
|
309
|
-
server_attempt += 1
|
|
310
|
-
logger.warning(
|
|
311
|
-
'%s %s failed (%s); retry %d/%d in %.1fs',
|
|
312
|
-
method, path, type(exc).__name__, server_attempt, self._max_server_retries, wait,
|
|
313
|
-
)
|
|
314
|
-
await asyncio.sleep(wait)
|
|
364
|
+
# Reset mid-request: only idempotent calls are re-sent.
|
|
365
|
+
if idempotent and await backoff(f'failed ({type(exc).__name__})', None):
|
|
315
366
|
continue
|
|
316
367
|
raise
|
|
317
368
|
|
|
318
369
|
self._rate_limiter.update_from_headers(response.headers)
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
if (
|
|
328
|
-
not response.is_success
|
|
329
|
-
and server_attempt < self._max_server_retries
|
|
330
|
-
and (
|
|
331
|
-
retry_deadline is None
|
|
332
|
-
or time.monotonic() + server_retry_delay(server_attempt, response.headers.get('Retry-After')) <= retry_deadline
|
|
333
|
-
)
|
|
334
|
-
and is_retryable_response(
|
|
335
|
-
response.status_code,
|
|
336
|
-
idempotent,
|
|
337
|
-
_error_type(response) if response.status_code == 503 else None,
|
|
338
|
-
response.text if response.status_code == 503 else None,
|
|
339
|
-
)
|
|
370
|
+
if response.status_code == 429:
|
|
371
|
+
self._rate_limiter.record_rate_limited()
|
|
372
|
+
|
|
373
|
+
if not response.is_success and is_retryable_response(
|
|
374
|
+
response.status_code,
|
|
375
|
+
idempotent,
|
|
376
|
+
_error_type(response) if response.status_code == 503 else None,
|
|
377
|
+
response.text if response.status_code == 503 else None,
|
|
340
378
|
):
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
logger.warning(
|
|
344
|
-
'%s %s answered %d; retry %d/%d in %.1fs',
|
|
345
|
-
method, path, response.status_code, server_attempt, self._max_server_retries, wait,
|
|
346
|
-
)
|
|
347
|
-
await asyncio.sleep(wait)
|
|
348
|
-
continue
|
|
379
|
+
if await backoff(f'answered {response.status_code}', response.headers.get('Retry-After')):
|
|
380
|
+
continue
|
|
349
381
|
|
|
350
382
|
if raise_for_status:
|
|
351
383
|
await self._raise_for_status(response)
|
|
@@ -402,6 +434,7 @@ class ApiClient:
|
|
|
402
434
|
params: Optional[Dict[str, Any]] = None,
|
|
403
435
|
*,
|
|
404
436
|
raise_for_status: bool = True,
|
|
437
|
+
idempotent: Optional[bool] = None,
|
|
405
438
|
) -> httpx.Response:
|
|
406
439
|
return await self.request(
|
|
407
440
|
'PUT',
|
|
@@ -411,6 +444,7 @@ class ApiClient:
|
|
|
411
444
|
content=content,
|
|
412
445
|
headers=headers,
|
|
413
446
|
raise_for_status=raise_for_status,
|
|
447
|
+
idempotent=idempotent,
|
|
414
448
|
)
|
|
415
449
|
|
|
416
450
|
async def delete(
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
scrapely/__init__.py,sha256=
|
|
1
|
+
scrapely/__init__.py,sha256=R7A3fQmfkS8LtiN04-32fWvjo6kjNrBSlaeFJCLNgjc,5746
|
|
2
2
|
scrapely/_browser_locations.py,sha256=8QVVnkms2bDn3_LDYgYDR59MvFx8757l_JYnZ7xb3oI,145269
|
|
3
|
-
scrapely/actor.py,sha256=
|
|
3
|
+
scrapely/actor.py,sha256=xo25KfueM_quEFrD9buWqIjja69LSt-koCVIdg3arqg,77912
|
|
4
4
|
scrapely/browser_configuration.py,sha256=jAZapNSHLFW05B96df4wNBtcyiiU9DA3ml0yTL1FwfA,5770
|
|
5
5
|
scrapely/cdp_retry.py,sha256=hdTJEAIKhApM7IOk1DrQEl8NoflSJSmqd4RbNJ2IsgI,8540
|
|
6
|
-
scrapely/charging.py,sha256=
|
|
6
|
+
scrapely/charging.py,sha256=_GHEKEgQ7lRYTVPacBiBuvlaqGTNcsF65K8xhiJ0dNM,68526
|
|
7
7
|
scrapely/charging_client.py,sha256=-4q-Ww7gu9EZXpM9QIn1Vd4vwAXmgL_WEKqaQIx3ViA,3961
|
|
8
8
|
scrapely/configuration.py,sha256=GjMr3YsxM16HYjAOTUzWZKG2E8FuMzAoFXmkn5dbDPM,16396
|
|
9
9
|
scrapely/errors.py,sha256=DCAlVvE3jToZuPEG2HVDL7i8D8hLboomQ5JpSOcsXoQ,4107
|
|
@@ -23,7 +23,7 @@ scrapely/request_loaders/__init__.py,sha256=aNgr4pRt3MziUbU6UQ_bjTT3moMNvvyUsPeC
|
|
|
23
23
|
scrapely/request_loaders/_scrapely_request_list.py,sha256=vguj-EvBjvrGY5_vf2fGXnJBfnhlPeRjdkjPFaxUA7g,7628
|
|
24
24
|
scrapely/storage/__init__.py,sha256=diAhxEvOXjc3YVhZvzkO3MttJUXQWJ5ogxstiApsh7U,1405
|
|
25
25
|
scrapely/storage/_metadata.py,sha256=WWawBnVN0b1iK2K-SlxZXUc6czb5wXWf8iC_0tzkgAQ,4764
|
|
26
|
-
scrapely/storage/api_client.py,sha256=
|
|
26
|
+
scrapely/storage/api_client.py,sha256=aWuMwMlHiOkKDCxcN3KDH8Fgf-W2SCSCtphlm80d8nw,17935
|
|
27
27
|
scrapely/storage/client.py,sha256=Ln_NA8nJz62nXldkcj54PFZopX5P3Lp8HGot_8friNM,22015
|
|
28
28
|
scrapely/storage/patch_storage.py,sha256=o3Fv0BByeZ1T9HhkzhY6bjolOFsIQcnqBoHq1EdG8Fw,8758
|
|
29
29
|
scrapely/storage/scrapely_dataset_client.py,sha256=u5dnXxe1BAsq7rh-haR5W1ChE7UZ_k7B9ShR8YtwFkg,10695
|
|
@@ -38,10 +38,10 @@ scrapely/storage/_file_system/_key_value_store_client.py,sha256=4Ipgbh4qEqAkwQJ9
|
|
|
38
38
|
scrapely/storage/_file_system/_storage_client.py,sha256=aaGJ4P0QAtIgEMl6yHpuobeWFD5M4rMC5HwhOOvuGuo,1794
|
|
39
39
|
scrapely/storage_clients/__init__.py,sha256=cs1YX7JV2_GKBXeiiJgkQFali08b5_y_jH6Yw7iROAA,656
|
|
40
40
|
scrapely/storages/__init__.py,sha256=4PAELhJNZ3exJ7Uouicn-bCQ58NafKjhLBixQ8ATQc4,173
|
|
41
|
-
scrapely_python-0.1.
|
|
42
|
-
scrapely_python-0.1.
|
|
43
|
-
scrapely_python-0.1.
|
|
44
|
-
scrapely_python-0.1.
|
|
45
|
-
scrapely_python-0.1.
|
|
46
|
-
scrapely_python-0.1.
|
|
47
|
-
scrapely_python-0.1.
|
|
41
|
+
scrapely_python-0.1.12.dist-info/licenses/LICENSE,sha256=pQGY0XwjIYh-a2HfawxHq1en53bXkFkDj2Q70jKWHr4,1088
|
|
42
|
+
scrapely_python-0.1.12.dist-info/licenses/LICENSE-APACHE,sha256=N8m0FzMq8i4UbTgXZvKEZJSD-27OF8pWZYS_atz4kTU,11355
|
|
43
|
+
scrapely_python-0.1.12.dist-info/licenses/NOTICE,sha256=DEsNrw6UdSIqdYYqMWVFgJnyt6jUivTD7iFU6YM_e1A,1122
|
|
44
|
+
scrapely_python-0.1.12.dist-info/METADATA,sha256=zVfjqlKUO2cXYOX8JTl6b8d8mjpS6qXocCrY8PDK9qI,3487
|
|
45
|
+
scrapely_python-0.1.12.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
46
|
+
scrapely_python-0.1.12.dist-info/top_level.txt,sha256=T4B9UMrs4q99QSAFyD1zdXhl40KnLhBKqrDkPhOTsmY,9
|
|
47
|
+
scrapely_python-0.1.12.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
{scrapely_python-0.1.11.dist-info → scrapely_python-0.1.12.dist-info}/licenses/LICENSE-APACHE
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|