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 <sbonazzo@redhat.com>
  • Loading branch information
sandrobonazzola authored and psss committed Jan 2, 2024
1 parent f48f9f6 commit b871424
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 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 @@ -37,6 +38,10 @@
GITLAB_SSL_VERIFY = True
GITLAB_API = 4

# Retry fetching
GITLAB_ATTEMPTS = 5
GITLAB_INTERVAL = 5

# Identifier padding
PADDING = 3

Expand All @@ -61,7 +66,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_ATTEMPTS:
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_INTERVAL,
connection_error
)
sleep(GITLAB_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 b871424

Please sign in to comment.