Skip to content

Commit

Permalink
Retry connecting to GitLab API on error
Browse files Browse the repository at this point in the history
On connection error, retry to connect to the GitLab API.
Found this usefull with https://gitlab.freedesktop.org/
which showed several connection resets for my case.

Signed-off-by: Sandro Bonazzola <[email protected]>
  • Loading branch information
sandrobonazzola committed Dec 14, 2023
1 parent ffa7820 commit 78657bd
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion did/plugins/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"""

import distutils.util
from time import sleep

import dateutil
import requests
Expand All @@ -36,6 +37,8 @@

GITLAB_SSL_VERIFY = True
GITLAB_API = 4
GITLAB_MAX_RETRIES = 5
GITLAB_RETRIES_INTERVAL = 5

# Identifier padding
PADDING = 3
Expand All @@ -61,7 +64,26 @@ def __init__(self, url, token, ssl_verify=GITLAB_SSL_VERIFY):
self.project_issues = {}

def _get_gitlab_api_raw(self, url):
return requests.get(url, headers=self.headers, verify=self.ssl_verify)
log.debug("Connecting to GitLab API at %s", url)
retries = 0
while True:
try:
api_raw = requests.get(
url, headers=self.headers, verify=self.ssl_verify)
return api_raw
except requests.exceptions.ConnectionError as connection_error:
retries += 1
if retries > GITLAB_MAX_RETRIES:
raise ReportError(
f"Unable to connect to {url}. Error: {connection_error}"
) from connection_error
log.debug(
"Retrying connection to %s in %s seconds due to %s",
url,
GITLAB_RETRIES_INTERVAL,
connection_error
)
sleep(GITLAB_RETRIES_INTERVAL)

def _get_gitlab_api(self, endpoint):
url = '{0}/api/v{1}/{2}'.format(self.url, GITLAB_API, endpoint)
Expand Down

0 comments on commit 78657bd

Please sign in to comment.