refactor: route network access through upstream

Branch heads (scrapers, FBNeo import, data directories) resolve through
core.upstream.resolve_head, with cache_dir=None to keep nothing under
.cache/upstream. Streaming downloads (large files, external pack files,
data directory archives, scraper downloads, standalone probe) go through
one core.upstream.download: scratch then rename, size cap, retries of a
cut stream, a 5xx or a rate limit. The shared rate limit wait reads
GoatCounter's X-Rate-Limit-Reset, so site insights, download.py,
indexnow and the contributor list use the same retrying client.

list_gitlinks raises TruncatedListingError on a partial tree, read
from one tree walk shared with list_tree (Forgejo paging included).
The Recalbox configs/ listing is paged with the shared page cap.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 10:30:13 +01:00
1 parent 526d7f09c7
commit 8a8fa71fbc
25 files changed
+708 -266

No files matched your search

+32 -3
View File
@@ -37,6 +37,9 @@ PAYLOAD_B = b"B" * (256 * 1024)
# the CI test job sets RETROBIOS_OFFLINE to keep every other test off the
# network, so it is cleared here.
_ONLINE = mock.patch.dict(os.environ, {"RETROBIOS_OFFLINE": ""})
# core.upstream retries a failed transfer after a pause: the pauses are
# skipped, the retries are not.
_NO_PAUSE = mock.patch.object(largefiles.upstream, "_sleep", lambda _s: None)
# The names these tests fetch, registered as .gitignore registers an asset:
# a bare name no registered path carries is never asked of the release.
@@ -58,15 +61,19 @@ def _test_registry(case: unittest.TestCase) -> None:
def setUpModule():
_ONLINE.start()
_NO_PAUSE.start()
def tearDownModule():
_NO_PAUSE.stop()
_ONLINE.stop()
class _SlowResponse(io.BytesIO):
"""Serve a payload in chunks, yielding between them."""
headers: dict[str, str] = {}
def __init__(self, data: bytes, barrier: threading.Barrier | None = None):
super().__init__(data)
self._barrier = barrier
@@ -629,16 +636,18 @@ class ReleaseAssetNames(unittest.TestCase):
largefiles.fetch_large_file(
self.RTCW, dest_dir=cache, registered=self.REGISTERED
)
# A refused connection is retried: each asset is asked
# more than once, and only the assets asked matter here.
self.assertEqual(
requested,
[largefiles.urllib.parse.quote(expected[self.RTCW])],
set(requested),
{largefiles.urllib.parse.quote(expected[self.RTCW])},
)
requested.clear()
largefiles.fetch_large_file(
"pak0.pk3", dest_dir=cache, registered=self.REGISTERED
)
self.assertEqual(
sorted(requested),
sorted(set(requested)),
sorted(
largefiles.urllib.parse.quote(expected[p])
for p in (self.ET, self.RTCW)
@@ -667,6 +676,8 @@ class ReleaseAssetNames(unittest.TestCase):
class _StalledResponse(io.BytesIO):
"""Serves one chunk, then the stream stalls."""
headers: dict[str, str] = {}
def __init__(self):
super().__init__(PAYLOAD_A)
self._served = False
@@ -702,6 +713,24 @@ class DownloadFailuresAndRevisions(unittest.TestCase):
self.assertIsNone(result)
self.assertEqual(os.listdir(self.dir), [])
def test_a_transient_server_error_is_retried(self):
"""One 503 from the release CDN dropped the asset from the build."""
answers: list = [urllib.error.HTTPError("u", 503, "busy", {}, None), _SlowResponse(PAYLOAD_A)]
def urlopen(*_args, **_kwargs):
answer = answers.pop(0)
if isinstance(answer, Exception):
raise answer
return answer
largefiles.urllib.request.urlopen = urlopen
result = largefiles.fetch_large_file(
"big.bin", self.dir, expected_md5=hashlib.md5(PAYLOAD_A).hexdigest(),
registered=TEST_REGISTERED,
)
self.assertIsNotNone(result)
self.assertEqual(answers, [])
def test_another_revision_is_kept_not_evicted(self):
cached = Path(self.dir, "PS3UPDAT.PUP")
cached.write_bytes(PAYLOAD_A)