Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more robust retry to the pubmed client's send_request #1426

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading