Rate limiting and retry logic for PubChemAPI #15
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
PubChem allows up to 5 requests per second and 400 per minute. The current
make_requestmethod fires with no throttling and raisesRuntimeErroron any non-200 status, including the 503 PubChem returns when you've been rate-limited. Dissociation constants alone have 40+ pages. Hit that endpoint without a delay, and you will get throttled.Scope
Add a configurable delay between requests, default 200ms (5 req/s). A class-level
_last_request_timewith a sleep inmake_requestis enough.Add retry logic for HTTP 503 (rate limited) and 504 (gateway timeout): retry up to 3 times with exponential backoff (1s, 2s, 4s), then raise the existing
RuntimeError. Log retries at the WARNING level.Test with a mock session that returns 503 twice, then 200.
Definition of done
Rate-limited requests retry automatically. A minimum delay is enforced between consecutive requests. Tested with mock sessions.