Skip to content

Commit

Permalink
Add more robust retry to the pubmed client's send_request
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaris committed Nov 20, 2023
1 parent 68f7d15 commit 469bfbc
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions indra/literature/pubmed_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Search and get metadata for articles in Pubmed.
"""
import logging
import random

import requests
from time import sleep
from typing import List
Expand All @@ -19,7 +21,7 @@

# Send request can't be cached by lru_cache because it takes a dict
# (a mutable/unhashable type) as an argument. We cache the callers instead.
def send_request(url, data):
def send_request(url, data, retry_pause=0.5, max_tries=3):
try:
res = requests.get(url, params=data)
except requests.exceptions.Timeout as e:
Expand All @@ -32,9 +34,12 @@ def send_request(url, data):
logger.error('url: %s, data: %s' % (url, data))
logger.error(e)
return None
if res.status_code == 429:
sleep(0.5)
res = requests.get(url, params=data)
if res.status_code in {429, 502, 503} and max_tries > 0:
sleep(retry_pause)
# Increase the sleep time at random to avoid multiple clients
# retrying at the same time for e.g. tests
retry_pause += 1 + random.random()
return send_request(url, data, retry_pause, max_tries - 1)
if not res.status_code == 200:
logger.error('Got return code %d from pubmed client.'
% res.status_code)
Expand Down

0 comments on commit 469bfbc

Please sign in to comment.