[docs] imrprove link checking (#13352)

This commit is contained in:
Oliver authored and GitHub committed 2026-11-24 10:53:12 +11:00
1 parent b30567799c
commit df5cc1103e
1 file changed
+40 -13
+40 -13
View File
@@ -4,6 +4,7 @@ import os
import subprocess import subprocess
import sys import sys
import textwrap import textwrap
import time
from pathlib import Path from pathlib import Path
from typing import Literal, Optional from typing import Literal, Optional
@@ -40,9 +41,18 @@ def get_repo_url(raw=False):
def check_link(url) -> bool: def check_link(url) -> bool:
"""Check that a provided URL is valid. """Check that a provided URL is valid.
We allow a number attempts and a lengthy timeout, Only a definitive "not found" response from the server is treated as a failure.
as we do not want false negatives. Transient errors (rate limiting, 5xx responses, network errors) are retried with
backoff, and if they persist the link is assumed valid (with a warning), as we do
not want flaky upstream servers to break the documentation build.
The remote check is skipped entirely on ReadTheDocs, where requests to GitHub
are frequently throttled. Callers are expected to have already validated the
target against the local checkout.
""" """
if os.environ.get('READTHEDOCS') == 'True':
return True
CACHE_FILE = gen_base.joinpath('url_cache.txt') CACHE_FILE = gen_base.joinpath('url_cache.txt')
# Keep a local cache file of URLs we have already checked # Keep a local cache file of URLs we have already checked
@@ -53,24 +63,41 @@ def check_link(url) -> bool:
if url in cache: if url in cache:
return True return True
def add_to_cache():
with open(CACHE_FILE, 'a', encoding='utf-8') as f:
f.write(f'{url}\n')
attempts = 5 attempts = 5
delay = 2
while attempts > 0: for attempt in range(1, attempts + 1):
response = requests.head(url, timeout=5000) try:
response = requests.head(url, timeout=10, allow_redirects=True)
# Ensure GH is not causing issues status = response.status_code
if response.status_code in (200, 429): except requests.exceptions.RequestException as exc:
# Update the cache file status = None
with open(CACHE_FILE, 'a', encoding='utf-8') as f: print(f'URL check error for {url}: {exc}')
f.write(f'{url}\n')
if status is not None and status < 400:
add_to_cache()
return True return True
attempts -= 1 if status in (404, 410):
print(f'URL check failed for {url}: status code {status}')
return False
print(f'URL check failed with status code {response.status_code}') if status is not None:
print(f'URL check for {url} returned status code {status}')
return False if attempt < attempts:
time.sleep(delay)
delay *= 2
# Persistent transient failure - do not fail the build on upstream issues
print(
f'WARNING: Could not verify URL {url} after {attempts} attempts - assuming valid'
)
return True
def get_build_environment() -> Optional[str]: def get_build_environment() -> Optional[str]: