Spaces:
Running
Running
siddhm11 Claude Opus 5.5 commited on
Commit ·
25ea863
1
Parent(s): e1e1d27
fix(ingest): back off from arXiv rate limits for minutes, honouring Retry-After
Browse filesAfter a heavy night of requests arXiv started answering this host with
HTTP 429 and stalled connections; retries 5, 10 and 15 s apart failed every
time (cs.CL, cs.CV, cs.LG on 2026-10-01). A 429/503 now waits arXiv's
Retry-After when given, else 30 s doubling to a 5 min cap, over 6 attempts;
other errors still retry within seconds.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- scripts/ingest_arxiv.py +23 -3
- tests/test_ingest_safety.py +30 -0
scripts/ingest_arxiv.py
CHANGED
|
@@ -124,6 +124,26 @@ def parse_entry(entry) -> dict | None:
|
|
| 124 |
}
|
| 125 |
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
def fetch_page(category: str, since: str, until: str, start: int) -> tuple[list[dict], int | None]:
|
| 128 |
"""One page of results. Returns (papers, total_available); total is None
|
| 129 |
when the request failed, so a failure is never mistaken for an empty
|
|
@@ -137,17 +157,17 @@ def fetch_page(category: str, since: str, until: str, start: int) -> tuple[list[
|
|
| 137 |
"sortBy": "submittedDate",
|
| 138 |
"sortOrder": "ascending",
|
| 139 |
})
|
| 140 |
-
for attempt in range(
|
| 141 |
try:
|
| 142 |
with deadline(150, f"arXiv {category} @{start}"), \
|
| 143 |
urllib.request.urlopen(url, timeout=120) as r:
|
| 144 |
xml = r.read()
|
| 145 |
break
|
| 146 |
except Exception as e:
|
| 147 |
-
if attempt ==
|
| 148 |
print(f" [{category}] fetch failed: {str(e)[:90]}")
|
| 149 |
return [], None
|
| 150 |
-
time.sleep(
|
| 151 |
root = ET.fromstring(xml)
|
| 152 |
total_el = root.find("opensearch:totalResults", NS)
|
| 153 |
total = int(total_el.text) if total_el is not None and total_el.text else 0
|
|
|
|
| 124 |
}
|
| 125 |
|
| 126 |
|
| 127 |
+
FETCH_ATTEMPTS = 6
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def retry_wait(error: Exception, attempt: int) -> float:
|
| 131 |
+
"""Seconds to wait before retrying a failed arXiv request.
|
| 132 |
+
|
| 133 |
+
A rate limit (429/503) gets arXiv's Retry-After when it sends one, else an
|
| 134 |
+
exponential back-off from 30 s, capped at 5 min: on 2026-10-01 retries 5,
|
| 135 |
+
10 and 15 s apart failed every time once arXiv had started throttling.
|
| 136 |
+
Other errors (timeouts, resets) retry sooner.
|
| 137 |
+
"""
|
| 138 |
+
code = getattr(error, "code", None)
|
| 139 |
+
if code in (429, 503):
|
| 140 |
+
after = (getattr(error, "headers", None) or {}).get("Retry-After", "")
|
| 141 |
+
if str(after).strip().isdigit():
|
| 142 |
+
return min(300.0, float(after))
|
| 143 |
+
return min(300.0, 30.0 * 2 ** attempt)
|
| 144 |
+
return 5.0 * (attempt + 1)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
def fetch_page(category: str, since: str, until: str, start: int) -> tuple[list[dict], int | None]:
|
| 148 |
"""One page of results. Returns (papers, total_available); total is None
|
| 149 |
when the request failed, so a failure is never mistaken for an empty
|
|
|
|
| 157 |
"sortBy": "submittedDate",
|
| 158 |
"sortOrder": "ascending",
|
| 159 |
})
|
| 160 |
+
for attempt in range(FETCH_ATTEMPTS):
|
| 161 |
try:
|
| 162 |
with deadline(150, f"arXiv {category} @{start}"), \
|
| 163 |
urllib.request.urlopen(url, timeout=120) as r:
|
| 164 |
xml = r.read()
|
| 165 |
break
|
| 166 |
except Exception as e:
|
| 167 |
+
if attempt == FETCH_ATTEMPTS - 1:
|
| 168 |
print(f" [{category}] fetch failed: {str(e)[:90]}")
|
| 169 |
return [], None
|
| 170 |
+
time.sleep(retry_wait(e, attempt))
|
| 171 |
root = ET.fromstring(xml)
|
| 172 |
total_el = root.find("opensearch:totalResults", NS)
|
| 173 |
total = int(total_el.text) if total_el is not None and total_el.text else 0
|
tests/test_ingest_safety.py
CHANGED
|
@@ -153,3 +153,33 @@ def test_point_ids_are_stable_disjoint_and_fit_uint64():
|
|
| 153 |
assert len(set(ids)) == len(ids) # 300k ids, no collision
|
| 154 |
assert min(ids) >= 1 << 62 > 300_000 # never meets sequential ids
|
| 155 |
assert max(ids) < 1 << 63 # Qdrant ids are unsigned 64-bit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
assert len(set(ids)) == len(ids) # 300k ids, no collision
|
| 154 |
assert min(ids) >= 1 << 62 > 300_000 # never meets sequential ids
|
| 155 |
assert max(ids) < 1 << 63 # Qdrant ids are unsigned 64-bit
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def test_rate_limits_back_off_for_minutes_and_honour_retry_after():
|
| 159 |
+
import urllib.error
|
| 160 |
+
def http(code, retry_after=None):
|
| 161 |
+
headers = {"Retry-After": retry_after} if retry_after else {}
|
| 162 |
+
return urllib.error.HTTPError("u", code, "x", headers, None)
|
| 163 |
+
assert ingest_arxiv.retry_wait(http(429), 0) == 30
|
| 164 |
+
assert ingest_arxiv.retry_wait(http(429), 2) == 120
|
| 165 |
+
assert ingest_arxiv.retry_wait(http(429), 5) == 300 # capped
|
| 166 |
+
assert ingest_arxiv.retry_wait(http(503, "45"), 0) == 45 # server's own advice
|
| 167 |
+
assert ingest_arxiv.retry_wait(TimeoutError("x"), 0) == 5 # transient: retry soon
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def test_fetch_page_retries_a_rate_limit_then_succeeds(monkeypatch):
|
| 171 |
+
import io, urllib.error
|
| 172 |
+
calls, waits = [], []
|
| 173 |
+
feed = (b'<feed xmlns="http://www.w3.org/2005/Atom" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">'
|
| 174 |
+
b'<opensearch:totalResults>0</opensearch:totalResults></feed>')
|
| 175 |
+
|
| 176 |
+
def urlopen(url, timeout):
|
| 177 |
+
calls.append(url)
|
| 178 |
+
if len(calls) < 3:
|
| 179 |
+
raise urllib.error.HTTPError(url, 429, "Too Many Requests", {}, None)
|
| 180 |
+
return io.BytesIO(feed)
|
| 181 |
+
|
| 182 |
+
monkeypatch.setattr(ingest_arxiv.urllib.request, "urlopen", urlopen)
|
| 183 |
+
monkeypatch.setattr(ingest_arxiv.time, "sleep", waits.append)
|
| 184 |
+
assert ingest_arxiv.fetch_page("cs.RO", "2026-09-28", "2026-10-02", 0) == ([], 0)
|
| 185 |
+
assert waits == [30, 60] and len(calls) == 3
|