Feed reads: one more try when the first times out or the connection fails (the first wait is 12 s, the second the full 30 s); about one github.com request in 25 hung for 30 s and the first-run card said offline

This commit is contained in:
Niko1221 committed 2026-10-24 14:44:12 +02:00
1 parent 9cf91f3555
commit abab458d74
2 files changed
+73 -4

No files matched your search

+22 -4
View File
@@ -156,17 +156,35 @@ def _remember_date(response) -> None:
pass
FIRST_TRY_SECONDS = 12.0
def _open_small(request, timeout: float, limit: int) -> bytes:
with _opener.open(request, timeout=timeout) as response:
_remember_date(response)
return response.read(limit + 1)
def read_small(location: str, *, limit: int = SMALL_LIMIT, timeout: float = 30) -> bytes:
"""A small document (the feed, its signature). Larger than `limit` is refused, not truncated."""
"""A small document (the feed, its signature). Larger than `limit` is refused, not truncated.
A web address gets one more try when the first one times out or the connection fails: about one request in 25 to github.com hung for the whole
timeout on a PC whose next 24 answered in half a second (measured on a 3060 PC: the first-run card said "offline" a moment before the install's
own read worked). The first try therefore waits at most 12 s; the second the full `timeout`."""
kind = kind_of(location)
_SERVER_DATE.clear() # only the answer of this read counts
try:
if kind in ("http", "https"):
check_url_policy(location)
request = urllib.request.Request(location, headers={"User-Agent": _USER_AGENT, "Cache-Control": "no-cache"})
with _opener.open(request, timeout=timeout) as response:
_remember_date(response)
data = response.read(limit + 1)
try:
data = _open_small(request, min(timeout, FIRST_TRY_SECONDS), limit)
except urllib.error.HTTPError:
raise
except (urllib.error.URLError, OSError, TimeoutError) as first:
if tls.is_certificate_error(first):
raise
_SERVER_DATE.clear()
data = _open_small(request, timeout, limit)
else:
path = _local_path(location)
if path.stat().st_size > limit:
+51
View File
@@ -0,0 +1,51 @@
"""A feed read that hangs once is tried once more: about one request in 25 to github.com hung for the whole 30 s while the next answered in 0.4 s,
and the app's first-run card said "offline" a moment before the install's own read worked."""
import io
import unittest
import urllib.error
from unittest import mock
from strata_control import transfer
class Answer(io.BytesIO):
headers = {}
def __enter__(self):
return self
def __exit__(self, *a):
return False
class ReadSmallRetry(unittest.TestCase):
def read(self, side_effect):
with mock.patch.object(transfer, "check_url_policy", lambda url: None), mock.patch.object(transfer._opener, "open", side_effect=side_effect) as op:
try:
return transfer.read_small("https://updates.example.org/feed.json", timeout=30), op
except transfer.TransferError as exc:
return exc, op
def test_one_timeout_is_forgiven_and_the_first_wait_is_short(self):
got, op = self.read([urllib.error.URLError(TimeoutError("timed out")), Answer(b"{}")])
self.assertEqual(got, b"{}")
self.assertEqual([c.kwargs["timeout"] for c in op.call_args_list], [transfer.FIRST_TRY_SECONDS, 30])
def test_two_timeouts_still_say_so(self):
got, op = self.read([urllib.error.URLError(TimeoutError("timed out"))] * 2)
self.assertIsInstance(got, transfer.TransferError)
self.assertEqual(op.call_count, 2)
def test_an_answer_of_404_is_not_tried_again(self):
err = urllib.error.HTTPError("https://x", 404, "no", {}, None)
got, op = self.read([err])
self.assertIsInstance(got, transfer.TransferError)
self.assertEqual(op.call_count, 1)
def test_a_good_first_answer_is_one_request(self):
got, op = self.read([Answer(b"{}")])
self.assertEqual((got, op.call_count), (b"{}", 1))
if __name__ == "__main__":
unittest.main()