summaryrefslogtreecommitdiff
path: root/gitlab
diff options
context:
space:
mode:
authorSrikanth Chelluri <srikanth.chelluri@appian.com>2019-01-08 20:58:26 -0500
committerSrikanth Chelluri <srikanth.chelluri@appian.com>2019-01-08 21:54:09 -0500
commit7a3724f3fca93b4f55aed5132cf46d3718c4f594 (patch)
tree640200cc827b3c6f6fcf3b638bcc4e9ffcc696dc /gitlab
parentce2c8356cdd0e086ec67a1bf73adc2d0ea251971 (diff)
downloadgitlab-7a3724f3fca93b4f55aed5132cf46d3718c4f594.tar.gz
fix: handle empty 'Retry-After' header from GitLab
When requests are throttled (HTTP response code 429), python-gitlab assumed that 'Retry-After' existed in the response headers. This is not always the case and so the request fails due to a KeyError. The change in this commit adds a rudimentary exponential backoff to the 'http_request' method, which defaults to 10 retries but can be set to -1 to retry without bound.
Diffstat (limited to 'gitlab')
-rw-r--r--gitlab/__init__.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py
index 01f9426..c280974 100644
--- a/gitlab/__init__.py
+++ b/gitlab/__init__.py
@@ -477,6 +477,10 @@ class Gitlab(object):
# obey the rate limit by default
obey_rate_limit = kwargs.get("obey_rate_limit", True)
+ # set max_retries to 10 by default, disable by setting it to -1
+ max_retries = kwargs.get("max_retries", 10)
+ cur_retries = 0
+
while True:
result = self.session.send(prepped, timeout=timeout, **settings)
@@ -486,9 +490,13 @@ class Gitlab(object):
return result
if 429 == result.status_code and obey_rate_limit:
- wait_time = int(result.headers["Retry-After"])
- time.sleep(wait_time)
- continue
+ if max_retries == -1 or cur_retries < max_retries:
+ wait_time = 2 ** cur_retries * 0.1
+ if "Retry-After" in result.headers:
+ wait_time = int(result.headers["Retry-After"])
+ cur_retries += 1
+ time.sleep(wait_time)
+ continue
error_message = result.content
try: