Skip to content

Commit 7eab477

Browse files
committed
feat: allow global retry_transient_errors
Value set on the `Gitlab` object will be used as a default in case every subsequent API call does not provide a `retry_transient_errors` parameter. This allows code to be succinct and the retry policy to be set for all calls to the `Gitlab` instance.
1 parent 4782678 commit 7eab477

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

docs/api-usage.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,19 @@ default an exception is raised for these errors.
391391
gl = gitlab.gitlab(url, token, api_version=4)
392392
gl.projects.list(all=True, retry_transient_errors=True)
393393
394+
The default ``retry_transient_errors`` can also be set on the ``Gitlab`` object
395+
and overridden by individual API calls.
396+
397+
.. code-block:: python
398+
399+
import gitlab
400+
import requests
401+
402+
gl = gitlab.gitlab(url, token, api_version=4, retry_transient_errors=True)
403+
gl.projects.list(all=True) # uses default retry_transient_errors=True
404+
gl.projects.list(all=True, retry_transient_errors=False) # uses overridden value retry_transient_errors=False
405+
406+
394407
Timeout
395408
-------
396409

gitlab/client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Gitlab(object):
5252
pagination (str): Can be set to 'keyset' to use keyset pagination
5353
order_by (str): Set order_by globally
5454
user_agent (str): A custom user agent to use for making HTTP requests.
55+
retry_transient_errors (bool): Whether to retry after 500, 502, 503, or
56+
504 responses. Defaults to False.
5557
"""
5658

5759
def __init__(
@@ -70,6 +72,7 @@ def __init__(
7072
pagination: Optional[str] = None,
7173
order_by: Optional[str] = None,
7274
user_agent: str = gitlab.const.USER_AGENT,
75+
retry_transient_errors: bool = False,
7376
) -> None:
7477

7578
self._api_version = str(api_version)
@@ -79,6 +82,7 @@ def __init__(
7982
self._url = "%s/api/v%s" % (self._base_url, api_version)
8083
#: Timeout to use for requests to gitlab server
8184
self.timeout = timeout
85+
self.retry_transient_errors = retry_transient_errors
8286
#: Headers that will be used in request to GitLab
8387
self.headers = {"User-Agent": user_agent}
8488

@@ -511,7 +515,6 @@ def http_request(
511515
files: Optional[Dict[str, Any]] = None,
512516
timeout: Optional[float] = None,
513517
obey_rate_limit: bool = True,
514-
retry_transient_errors: bool = False,
515518
max_retries: int = 10,
516519
**kwargs: Any,
517520
) -> requests.Response:
@@ -531,9 +534,6 @@ def http_request(
531534
timeout (float): The timeout, in seconds, for the request
532535
obey_rate_limit (bool): Whether to obey 429 Too Many Request
533536
responses. Defaults to True.
534-
retry_transient_errors (bool): Whether to retry after 500, 502,
535-
503, or 504 responses. Defaults
536-
to False.
537537
max_retries (int): Max retries after 429 or transient errors,
538538
set to -1 to retry forever. Defaults to 10.
539539
**kwargs: Extra options to send to the server (e.g. sudo)
@@ -598,6 +598,9 @@ def http_request(
598598
if 200 <= result.status_code < 300:
599599
return result
600600

601+
retry_transient_errors = kwargs.get(
602+
"retry_transient_errors", self.retry_transient_errors
603+
)
601604
if (429 == result.status_code and obey_rate_limit) or (
602605
result.status_code in [500, 502, 503, 504] and retry_transient_errors
603606
):

0 commit comments

Comments
 (0)