Prechádzať zdrojové kódy

retry on ReadTimeout

raylu 1 týždeň pred
rodič
commit
8a7404c6c3
1 zmenil súbory, kde vykonal 13 pridanie a 2 odobranie
  1. 13 2
      cache.py

+ 13 - 2
cache.py

@@ -14,7 +14,7 @@ if typing.TYPE_CHECKING:
 ONE_DAY = datetime.timedelta(days=1)
 
 CACHE_DIR = pathlib.Path(__file__).parent / 'cache'
-client = httpx.Client(transport=httpx.HTTPTransport(http2=True, retries=2), timeout=10)
+client = httpx.Client(transport=httpx.HTTPTransport(http2=True, retries=3), timeout=5) # retry on ConnectError and ConnectTimeout
 
 @typing.overload
 def get(url: str, *, json: typing.Literal[True]=True, headers: httpx._types.HeaderTypes|None=None,
@@ -43,7 +43,7 @@ def get(url: str, *, json=True, headers=None, expiry=datetime.timedelta(minutes=
 	except FileNotFoundError:
 		pass # fall through
 
-	r = client.get(url, headers=headers).raise_for_status()
+	r = get_with_retries(url, headers)
 	cache_path.parent.mkdir(parents=True, exist_ok=True)
 	with lzma.open(cache_path, 'wb') as f:
 		if json:
@@ -53,3 +53,14 @@ def get(url: str, *, json=True, headers=None, expiry=datetime.timedelta(minutes=
 			data = r.text
 			f.write(data.encode('utf-8'))
 	return data
+
+def get_with_retries(url: str, headers: httpx._types.HeaderTypes|None=None) -> httpx.Response:
+	for attempt in range(5):
+		try:
+			return client.get(url, headers=headers).raise_for_status()
+		except httpx.ReadTimeout:
+			if attempt == 4:
+				raise
+			else:
+				print(url, 'attempt', attempt+1, 'timed out; retrying...')
+	raise AssertionError