mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-30 13:28:51 -06:00
fix: retry archives and the app like files
One decision says whether a failed request is asked again and when: 5xx and 429 wait what Retry-After asks, a clock in the past or no certificate list stops at once. A data archive and the handheld app waited 1 s on a 429 that asked for 30 and gave up where a BIOS file arrived, and the app took a body cut short of its Content-Length as the file.
This commit is contained in:
1 parent
c41fb9e609
commit
6acdf4b193
9 files changed
+144
-58
No files matched your search
+51
-22
@@ -7316,10 +7316,8 @@ def _offline_only() -> bool:
|
||||
|
||||
# --- installer/network.py ----------------------------------------------------
|
||||
|
||||
def _read_limited_json(
|
||||
response: http.client.HTTPResponse, limit: int, label: str, raw: bool = False
|
||||
) -> object:
|
||||
"""Read a bounded UTF-8 JSON response, its bytes as they came with raw.
|
||||
def _read_limited(response: http.client.HTTPResponse, limit: int, label: str) -> bytes:
|
||||
"""Read a response of limit bytes at most.
|
||||
|
||||
A body shorter than its Content-Length is a dropped connection, raised
|
||||
as one so the caller retries: parsed, the cut read as an invalid file
|
||||
@@ -7344,6 +7342,14 @@ def _read_limited_json(
|
||||
raise ValueError(f"{label} exceeds {limit} bytes")
|
||||
if expected is not None and len(payload) < expected:
|
||||
raise ConnectionError(f"{label} cut after {len(payload)} of {expected} bytes")
|
||||
return payload
|
||||
|
||||
|
||||
def _read_limited_json(
|
||||
response: http.client.HTTPResponse, limit: int, label: str, raw: bool = False
|
||||
) -> object:
|
||||
"""Read a bounded UTF-8 JSON response, its bytes as they came with raw."""
|
||||
payload = _read_limited(response, limit, label)
|
||||
if raw:
|
||||
return payload
|
||||
try:
|
||||
@@ -7583,21 +7589,21 @@ def _fetch_json(
|
||||
return _read_limited_json(resp, limit, label, raw=True)
|
||||
return _read_limited_json(resp, limit, label)
|
||||
except urllib.error.HTTPError as exc:
|
||||
if exc.code < 500 and exc.code != 429 or attempt == attempts:
|
||||
wait = _retry_after_failure(exc, attempt, attempts)
|
||||
if wait is None:
|
||||
raise
|
||||
delay = _retry_delay(attempt, exc.headers.get("Retry-After") if exc.headers else None)
|
||||
delay = wait
|
||||
problem = f"HTTP {exc.code}"
|
||||
except (urllib.error.URLError, OSError, http.client.HTTPException) as exc:
|
||||
code, problem = network_words(exc)
|
||||
# A wrong clock or no certificate list fails every attempt alike.
|
||||
if (attempt == attempts or code != "network"
|
||||
or (_stalled(exc) and attempt >= STALLED_ATTEMPTS)):
|
||||
problem = network_words(exc)[1]
|
||||
wait = _retry_after_failure(exc, attempt, attempts)
|
||||
if wait is None or (_stalled(exc) and attempt >= STALLED_ATTEMPTS):
|
||||
if isinstance(exc, http.client.HTTPException):
|
||||
raise ConnectionError(f"connection cut: {exc!r}") from exc
|
||||
# How many were made, for the line that says so.
|
||||
exc.__dict__["attempts"] = attempt
|
||||
raise
|
||||
delay = _retry_delay(attempt, None)
|
||||
delay = wait
|
||||
if _stalled(exc):
|
||||
tries = min(attempts, STALLED_ATTEMPTS)
|
||||
# Said, or three 30-second attempts read as a frozen program.
|
||||
@@ -7814,6 +7820,26 @@ def _retry_delay(attempt: int, retry_after: str | None) -> float:
|
||||
return float(min(2 ** (attempt - 1), 8))
|
||||
|
||||
|
||||
def _server_delay(exc: urllib.error.HTTPError, attempt: int) -> float:
|
||||
"""Seconds before asking a server that refused again: its Retry-After."""
|
||||
return _retry_delay(attempt, exc.headers.get("Retry-After") if exc.headers else None)
|
||||
|
||||
|
||||
def _retry_after_failure(exc: BaseException, attempt: int, attempts: int) -> float | None:
|
||||
"""Seconds before the attempt after exc, None when it is final: the last
|
||||
attempt, a refusal other than 429, a wrong clock or no certificate list,
|
||||
which fail every attempt alike."""
|
||||
if attempt >= attempts:
|
||||
return None
|
||||
if isinstance(exc, urllib.error.HTTPError):
|
||||
if exc.code < 500 and exc.code != 429:
|
||||
return None
|
||||
return _server_delay(exc, attempt)
|
||||
if network_words(exc)[0] != "network":
|
||||
return None
|
||||
return _retry_delay(attempt, None)
|
||||
|
||||
|
||||
def _read_capped(path: Path, limit: int) -> bytes | None:
|
||||
try:
|
||||
with open(path, "rb") as handle:
|
||||
@@ -16301,7 +16327,7 @@ def _fetch_file(job: _FileFetch) -> tuple[str, bool, str]:
|
||||
|
||||
except urllib.error.HTTPError as exc:
|
||||
reason = _http_attempt_failure(job, exc, attempt)
|
||||
delay = _retry_delay(attempt, exc.headers.get("Retry-After") if exc.headers else None)
|
||||
delay = _server_delay(exc, attempt)
|
||||
|
||||
# http.client raises IncompleteRead (an HTTPException, not an
|
||||
# OSError) when a chunked transfer is cut, as a proxy re-encoding
|
||||
@@ -16846,10 +16872,14 @@ def _fetch_archive(url: str, directory: Path, progress: Progress | None = None,
|
||||
if exc.code < 500 and exc.code not in (416, 429):
|
||||
raise
|
||||
reason = exc
|
||||
# A range past an archive that shrank is asked again whole.
|
||||
delay = (_retry_delay(attempt, None) if exc.code == 416 and attempt < MAX_RETRIES
|
||||
else _retry_after_failure(exc, attempt, MAX_RETRIES))
|
||||
except (urllib.error.URLError, OSError, http.client.HTTPException) as exc:
|
||||
if keep is None:
|
||||
Path(handle.name).unlink(missing_ok=True)
|
||||
reason = exc if isinstance(exc, OSError) else ConnectionError(repr(exc))
|
||||
delay = _retry_after_failure(exc, attempt, MAX_RETRIES)
|
||||
except ValueError:
|
||||
_drop_archive(Path(handle.name), note)
|
||||
raise
|
||||
@@ -16859,8 +16889,9 @@ def _fetch_archive(url: str, directory: Path, progress: Progress | None = None,
|
||||
Path(handle.name).unlink(missing_ok=True)
|
||||
raise
|
||||
count.take_back()
|
||||
if attempt < MAX_RETRIES:
|
||||
time.sleep(_retry_delay(attempt, None))
|
||||
if delay is None:
|
||||
break
|
||||
time.sleep(delay)
|
||||
raise reason if reason else OSError("download failed")
|
||||
|
||||
|
||||
@@ -17363,19 +17394,17 @@ def app_archive(members: list[tuple[dict, bytes]]) -> bytes:
|
||||
|
||||
|
||||
def _app_download(url: str, size: int) -> bytes:
|
||||
"""The bytes at url, tried MAX_RETRIES times, never more than size + 1."""
|
||||
"""The bytes at url, tried MAX_RETRIES times, refused past size."""
|
||||
_network_allowed("the RetroBIOS app")
|
||||
for attempt in range(1, MAX_RETRIES + 1):
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout=60) as resp:
|
||||
return resp.read(size + 1)
|
||||
except urllib.error.HTTPError as exc:
|
||||
if exc.code < 500 and exc.code != 429 or attempt == MAX_RETRIES:
|
||||
return _read_limited(resp, size, url)
|
||||
except (urllib.error.URLError, OSError, http.client.HTTPException) as exc:
|
||||
delay = _retry_after_failure(exc, attempt, MAX_RETRIES)
|
||||
if delay is None:
|
||||
raise
|
||||
except (urllib.error.URLError, OSError, http.client.HTTPException):
|
||||
if attempt == MAX_RETRIES:
|
||||
raise
|
||||
time.sleep(_retry_delay(attempt, None))
|
||||
time.sleep(delay)
|
||||
raise AssertionError("unreachable")
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user