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 retry for annotation endpoints #158

Merged
merged 1 commit into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 27 additions & 2 deletions AnnotatorCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import logging
import re
import matplotlib
from requests.adapters import HTTPAdapter
from urllib3 import Retry

matplotlib.use('Agg')
import matplotlib.pyplot as plt
from datetime import date
Expand All @@ -23,6 +26,8 @@
# API timeout is set to two minutes
REQUEST_TIMEOUT = 240

API_REQUEST_RETRY_STATUS_FORCELIST = [429, 500, 502, 503, 504]

csv.field_size_limit(int(ct.c_ulong(-1).value // 2)) # Deal with overflow problem on Windows, https://stackoverflow.co/120m/questions/15063936/csv-error-field-larger-than-field-limit-131072
sizeLimit = csv.field_size_limit()
csv.field_size_limit(sizeLimit) # for reading large files
Expand Down Expand Up @@ -198,13 +203,33 @@ def gethotspots(url, type):
"reason: %s" % response.reason)
return hotspots

def requests_retry_session(
retries=3,
backoff_factor=0.3,
status_forcelist=API_REQUEST_RETRY_STATUS_FORCELIST,
method_whitelist=('GET', 'HEAD'),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
method_whitelist=method_whitelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session

def makeoncokbpostrequest(url, body):
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer %s' % oncokbapibearertoken
}
return requests.post(url, headers=headers, data=json.dumps(body, default=lambda o: o.__dict__),
return requests_retry_session(method_whitelist=["POST"]).post(url, headers=headers, data=json.dumps(body, default=lambda o: o.__dict__),
timeout=REQUEST_TIMEOUT)


Expand All @@ -213,7 +238,7 @@ def makeoncokbgetrequest(url):
'Content-Type': 'application/json',
'Authorization': 'Bearer %s' % oncokbapibearertoken
}
return requests.get(url, headers=headers, timeout=REQUEST_TIMEOUT)
return requests_retry_session(method_whitelist=["HEAD", "GET"]).get(url, headers=headers, timeout=REQUEST_TIMEOUT)


_3dhotspots = None
Expand Down
3 changes: 2 additions & 1 deletion requirements/common.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests==2.20.0
requests==2.27.1
urllib3==1.26.8