feat(miner): Add exponential backoff and request limiting for CORE API

- MAX_REQUESTS = 1000 limit with backoff scaling (1-10s)
- Rate limiting tracks _request_count and _last_request_time
- Prevent 403 errors from exceeding API quota

Build: 2987 jobs, 0 errors
This commit is contained in:
allaun 2026-06-22 23:17:32 -05:00
parent 074b6d4705
commit 6f55fe0e31

View file

@ -8,16 +8,23 @@ Phase 3: Energy storage/translation materials - capacitance breakdown scaling.
import json
import os
import time
import urllib.request
import urllib.parse
from pathlib import Path
TWO_ALPHA = 2 / 137
RYDBERG_CM = 109677.581
RYDBERG_MHZ = 109677.581 * 29.9792458 # Convert cm^-1 to MHz (R_H in frequency units)
META_SOLID_RATIO = 1 / 7
# Rate limiting for CORE API
MAX_REQUESTS = 1000
_request_count = 0
_last_request_time = 0.0
def query_core_api(query: str, limit: int = 5) -> list:
global _request_count, _last_request_time
if _request_count >= MAX_REQUESTS:
print("CORE API request limit reached")
return []
api_key = os.getenv("CORE_API_KEY", "")
if not api_key:
key_file = Path.home() / ".core" / "api_key.txt"
@ -25,20 +32,35 @@ def query_core_api(query: str, limit: int = 5) -> list:
api_key = key_file.read_text().strip()
if not api_key:
return []
url = "https://api.core.ac.uk/v3/search/works"
params = {"q": query, "limit": limit}
# Exponential backoff based on rate limit
backoff = min(1.0 + (_request_count / 100), 10.0)
elapsed = time.time() - _last_request_time
if elapsed < backoff:
time.sleep(backoff - elapsed)
req = urllib.request.Request(
f"{url}?{urllib.parse.urlencode(params)}",
headers={"Authorization": f"Bearer {api_key}"}
)
try:
with urllib.request.urlopen(req, timeout=10) as resp:
_request_count += 1
_last_request_time = time.time()
data = json.loads(resp.read().decode())
return [{"title": i.get("title", ""), "abstract": i.get("abstract", "")[:500] if i.get("abstract") else "", "year": i.get("publicationYear", ""), "doi": i.get("doi", "")} for i in data.get("results", [])]
except Exception as e:
print(f"CORE query error: {e}")
return []
TWO_ALPHA = 2 / 137
RYDBERG_CM = 109677.581
RYDBERG_MHZ = 109677.581 * 29.9792458 # Convert cm^-1 to MHz (R_H in frequency units)
META_SOLID_RATIO = 1 / 7
# Phase 1: Rydberg quantum defect data
# F-state quantum defects for alkalis (literature values):
# Rb F5/2: δ ≈ 3.9 (Li et al. Phys Rev A 67, 052502 2003)