From 59fe2714741133989a7beed613f1eeb67c18c54e Mon Sep 17 00:00:00 2001 From: Mitar Date: Thu, 12 Dec 2019 20:02:24 -0800 Subject: [PATCH] feat: retry transient HTTP errors Fixes #970 --- docs/api-usage.rst | 18 ++++++++++++++++++ gitlab/__init__.py | 6 +++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/docs/api-usage.rst b/docs/api-usage.rst index 19b959317..fc2314ee8 100644 --- a/docs/api-usage.rst +++ b/docs/api-usage.rst @@ -348,3 +348,21 @@ throttled, you can set this parameter to -1. This parameter is ignored if .. warning:: You will get an Exception, if you then go over the rate limit of your GitLab instance. + +Transient errors +---------------- + +GitLab server can sometimes return a transient HTTP error. +python-gitlab can automatically retry in such case, when +``retry_transient_errors`` argument is set to ``True``. When enabled, +HTTP error codes 500 (Internal Server Error), 502 (502 Bad Gateway), +503 (Service Unavailable), and 504 (Gateway Timeout) are retried. By +default an exception is raised for these errors. + +.. code-block:: python + + import gitlab + import requests + + gl = gitlab.gitlab(url, token, api_version=4) + gl.projects.list(all=True, retry_transient_errors=True) diff --git a/gitlab/__init__.py b/gitlab/__init__.py index 3605b80b3..09b7b81bb 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -518,6 +518,8 @@ def http_request( # obey the rate limit by default obey_rate_limit = kwargs.get("obey_rate_limit", True) + # do not retry transient errors by default + retry_transient_errors = kwargs.get("retry_transient_errors", False) # set max_retries to 10 by default, disable by setting it to -1 max_retries = kwargs.get("max_retries", 10) @@ -531,7 +533,9 @@ def http_request( if 200 <= result.status_code < 300: return result - if 429 == result.status_code and obey_rate_limit: + if (429 == result.status_code and obey_rate_limit) or ( + result.status_code in [500, 502, 503, 504] and retry_transient_errors + ): if max_retries == -1 or cur_retries < max_retries: wait_time = 2 ** cur_retries * 0.1 if "Retry-After" in result.headers: