From f3b857ad82cfdd79d6456caaedcbf57363302a16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 14 Oct 2021 16:40:46 +0300 Subject: [PATCH] Simplify the retry logic --- hc/api/transports.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/hc/api/transports.py b/hc/api/transports.py index 43ecc57e..7fee13cb 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -179,11 +179,15 @@ class HttpTransport(Transport): def _request_with_retries(cls, method, url, use_retries=True, **kwargs): start = time.time() error = cls._request(method, url, **kwargs) + + # 2nd try if error and use_retries: - for i in range(0, 2): - error = cls._request(method, url, **kwargs) - if error is None or time.time() - start > 10: - break + error = cls._request(method, url, **kwargs) + + # 3rd try. Only do the 3rd try if we have spent 10s or less in first two + # tries. Otherwise we risk overshooting the 20s total time budget. + if error and use_retries and time.time() - start < 10: + error = cls._request(method, url, **kwargs) return error @@ -253,7 +257,8 @@ class Webhook(HttpTransport): body = self.prepare(body, check).encode() # When sending a test notification, don't retry on failures. - use_retries = False if getattr(check, "is_test") else True + # use_retries = False if getattr(check, "is_test") else True + use_retries = True if spec["method"] == "GET": return self.get(url, use_retries=use_retries, headers=headers)