8000 feat: retry transient HTTP errors · python-gitlab/python-gitlab@59fe271 · GitHub
[go: up one dir, main page]

Skip to content

Commit 59fe271

Browse files
committed
feat: retry transient HTTP errors
Fixes #970
1 parent 3e2d694 commit 59fe271

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

docs/api-usage.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,21 @@ throttled, you can set this parameter to -1. This parameter is ignored if
348348
.. warning::
349349

350350
You will get an Exception, if you then go over the rate limit of your GitLab instance.
351+
352+
Transient errors
353+
----------------
354+
355+
GitLab server can sometimes return a transient HTTP error.
356+
python-gitlab can automatically retry in such case, when
357+
``retry_transient_errors`` argument is set to ``True``. When enabled,
358+
HTTP error codes 500 (Internal Server Error), 502 (502 Bad Gateway),
359+
503 (Service Unavailable), and 504 (Gateway Timeout) are retried. By
360+
default an exception is raised for these errors.
361+
362+
.. code-block:: python
363+
364+
import gitlab
365+
import requests
366+
367+
gl = gitlab.gitlab(url, token, api_version=4)
368+
gl.projects.list(all=True, retry_transient_errors=True)

gitlab/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,8 @@ def http_request(
518518

519519
# obey the rate limit by default
520520
obey_rate_limit = kwargs.get("obey_rate_limit", True)
521+
# do not retry transient errors by default
522+
retry_transient_errors = kwargs.get("retry_transient_errors", False)
521523

522524
# set max_retries to 10 by default, disable by setting it to -1
523525
max_retries = kwargs.get("max_retries", 10)
@@ -531,7 +533,9 @@ def http_request(
531533
if 200 <= result.status_code < 300:
532534
return result
533535

534-
if 429 == result.status_code and obey_rate_limit:
536+
if (429 == result.status_code and obey_rate_limit) or (
537+
result.status_code in [500, 502, 503, 504] and retry_transient_errors
538+
):
535539
if max_retries == -1 or cur_retries < max_retries:
536540
wait_time = 2 ** cur_retries * 0.1
537541
if "Retry-After" in result.headers:

0 commit comments

Comments
 (0)
0