Skip to content

Commit

Permalink
Use a backoff factor for sleeps and timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
nsoranzo committed Oct 25, 2024
1 parent 2d7b04b commit 72eba54
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions rfparser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

REQUEST_TIMEOUT = 5.0
REQUEST_RETRIES = 3
REQUEST_RETRIES_BACKOFF_FACTOR = 1.0
BASE_CR_URL = "https://api.crossref.org"
BASE_DOI_URL = "https://doi.org"
BASE_RF_URL = "https://api.researchfish.com/restapi"
Expand Down Expand Up @@ -138,14 +139,15 @@ def get_url(
s: Optional[Session] = None,
) -> Response:
for i in range(retries):
backoff_time = 0 if i == 0 else REQUEST_RETRIES_BACKOFF_FACTOR * (2**i)
try:
if s:
r = s.get(url, params=params, headers=headers, timeout=timeout)
r = s.get(url, params=params, headers=headers, timeout=timeout + backoff_time)
else:
r = requests.get(url, params=params, headers=headers, timeout=timeout)
r = requests.get(url, params=params, headers=headers, timeout=timeout + backoff_time)
except Exception:
log.exception("Failed %d times to get URL %s", i + 1, url)
sleep(1)
sleep(backoff_time)
continue
try:
r.raise_for_status()
Expand All @@ -154,8 +156,8 @@ def get_url(
if 400 <= r.status_code < 500:
# Client error
raise
log.exception("Failed %d times to get URL %s", i + 1, url)
sleep(1)
log.exception("Failed %d times to get URL %s , status code %d", i + 1, url, r.status_code)
sleep(backoff_time)
else:
raise Exception(f"Failed too many times to get URL {url}")
return r
Expand Down

0 comments on commit 72eba54

Please sign in to comment.