From 5dc9655b8238b57d15958664cb0612e8ee4481c7 Mon Sep 17 00:00:00 2001
From: Emanuele Aina <emanuele.aina@collabora.com>
Date: Thu, 8 Jul 2021 23:11:44 +0200
Subject: [PATCH] test_urls: Use a session to retry requests

We often get DNS resolution or connection failures, try a few time
before giving up.

Signed-off-by: Emanuele Aina <emanuele.aina@collabora.com>
---
 scripts/test_urls.py | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/scripts/test_urls.py b/scripts/test_urls.py
index 483a98a58..d7ce8fa07 100755
--- a/scripts/test_urls.py
+++ b/scripts/test_urls.py
@@ -82,6 +82,11 @@ headers={
     "User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
     }
 
+session = requests.Session()
+adapter = requests.adapters.HTTPAdapter(max_retries=3)
+session.mount('http://', adapter)
+session.mount('https://', adapter)
+
 broken = []
 for url in urls:
     print("%s : " %(url), end='')
@@ -90,7 +95,7 @@ for url in urls:
     status = None
     resp = None
     try:
-        resp = requests.head(url, headers=headers, allow_redirects=True, timeout=60, verify=False)
+        resp = session.head(url, headers=headers, allow_redirects=True, timeout=60, verify=False)
         status = resp.ok
         resp.close()
     except Exception as e:
@@ -102,7 +107,7 @@ for url in urls:
         # that's not got a 200 status code with GET as well.
         if not status:
             resp = None
-            resp = requests.get(url, headers=headers, allow_redirects=True, timeout=60, verify=False)
+            resp = session.get(url, headers=headers, allow_redirects=True, timeout=60, verify=False)
             status = resp.ok
             resp.close()
     except Exception as e:
-- 
GitLab