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 Nov 14, 2023
1 parent 71e2580 commit 9bbe3c2
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion did/plugins/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
import dateutil
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from time import sleep

from did.base import Config, ReportError, get_token
from did.stats import Stats, StatsGroup
from did.utils import listed, log, pretty

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

# Identifier padding
PADDING = 3
Expand All @@ -61,7 +64,25 @@ 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 9bbe3c2

Please sign in to comment.