From 6aca611e328224af5f8a595c96c75f94d6d60181 Mon Sep 17 00:00:00 2001 From: Tony Breeds Date: Tue, 10 Oct 2017 11:11:55 +1100 Subject: [PATCH] Add a retry when getting data from gerrit Borrowed from openstack-infra/system-config:tools/owners.py Change-Id: I7526d03838567ed5d916ed8165e52b1d62f5ccc6 --- openstack_election/utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/openstack_election/utils.py b/openstack_election/utils.py index 21322f42..9eaa20eb 100644 --- a/openstack_election/utils.py +++ b/openstack_election/utils.py @@ -45,6 +45,15 @@ exceptions = None # Generic functions +def requester(url, params={}, headers={}): + """A requests wrapper to consistently retry HTTPS queries""" + + # Try up to 3 times + retry = requests.Session() + retry.mount("https://", requests.adapters.HTTPAdapter(max_retries=3)) + return retry.get(url=url, params=params, headers=headers) + + def load_exceptions(): global exceptions exceptions = {} @@ -67,7 +76,7 @@ def gerrit_datetime(dt): def gerrit_query(url, params=None): - r = requests.get(url, params=params) + r = requester(url, params=params) if r.status_code == 200: data = json.loads(r.text[4:]) else: