From bfbe148b887913e60a10c3658439c0311b315d48 Mon Sep 17 00:00:00 2001 From: Austin de Coup-Crank Date: Tue, 4 Feb 2025 19:20:59 -0600 Subject: [PATCH 1/4] Add default fallback --- retryhttp/_wait.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/retryhttp/_wait.py b/retryhttp/_wait.py index 008524f..d6ddec7 100644 --- a/retryhttp/_wait.py +++ b/retryhttp/_wait.py @@ -32,7 +32,7 @@ class wait_from_header(wait_base): program will hang if the server responds with an excessive wait value. fallback (wait_base): Wait strategy to use if `header` is not present, or unable to parse to a `float` value, or if value parsed from header - exceeds `wait_max`. Defaults to `None`. + exceeds `wait_max`. Defaults to `tenacity.wait_exponential`. Raises: ValueError: If `fallback` is `None`, and any one of the following is true: @@ -47,7 +47,7 @@ def __init__( self, header: str, wait_max: Union[PositiveFloat, PositiveInt, None] = 120.0, - fallback: Optional[wait_base] = None, + fallback: Optional[wait_base] = wait_exponential(), ) -> None: self.header = header self.wait_max = float(wait_max) if wait_max else None @@ -69,7 +69,9 @@ def _get_wait_value(self, retry_state: RetryCallState) -> float: if retry_state.outcome: exc = retry_state.outcome.exception() if isinstance(exc, get_default_http_status_exceptions()): - value = exc.response.headers.get(self.header, "") + value = exc.response.headers.get(self.header) + if value is None: + raise ValueError(f"Header not present: {self.header}") if re.match(r"^\d+$", value): return float(value) else: @@ -115,7 +117,7 @@ class wait_retry_after(wait_from_header): program will hang if the server responds with an excessive wait value. fallback (wait_base): Wait strategy to use if `header` is not present, or unable to parse to a `float` value, or if value parsed from header - exceeds `wait_max`. Defaults to `None`. + exceeds `wait_max`. Defaults to `tenacity.wait_exponential()`. Raises: ValueError: If `fallback` is `None`, and any one of the following is true: @@ -129,7 +131,7 @@ class wait_retry_after(wait_from_header): def __init__( self, wait_max: Union[PositiveFloat, PositiveInt, None] = 120.0, - fallback: Optional[wait_base] = None, + fallback: Optional[wait_base] = wait_exponential(), ) -> None: super().__init__(header="Retry-After", wait_max=wait_max, fallback=fallback) From 91fe75215a30cbb802b1db94ee503e6cbe9b8795 Mon Sep 17 00:00:00 2001 From: Austin de Coup-Crank Date: Tue, 4 Feb 2025 19:25:12 -0600 Subject: [PATCH 2/4] Update changelog --- docs/changelog.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/changelog.md b/docs/changelog.md index 4b5e878..54d5fb5 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,10 @@ # Changelog +## v1.2.1 + +* Fix typing error ([#15](https://github.com/austind/retryhttp/pull/15)) +* Add default fallback wait strategy [`tenacity.wait_random_exponential`][] to [`retryhttp.wait_from_header`][] and [`retryhttp.wait_retry_after`][] ([#17](https://github.com/austind/retryhttp/pull/)) + ## v1.2.0 * Added `wait_max` argument to [`retryhttp.wait_from_header`][] and [`retryhttp.wait_retry_after`][], which defaults to 120.0 seconds. From 9fb6f91fa66eab13103a8cf30acaf9caab852f1a Mon Sep 17 00:00:00 2001 From: Austin de Coup-Crank Date: Tue, 4 Feb 2025 19:25:49 -0600 Subject: [PATCH 3/4] Version fix --- docs/changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changelog.md b/docs/changelog.md index 54d5fb5..c1aefe1 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,6 +1,6 @@ # Changelog -## v1.2.1 +## v1.3.0 * Fix typing error ([#15](https://github.com/austind/retryhttp/pull/15)) * Add default fallback wait strategy [`tenacity.wait_random_exponential`][] to [`retryhttp.wait_from_header`][] and [`retryhttp.wait_retry_after`][] ([#17](https://github.com/austind/retryhttp/pull/)) From 77b431eae962043c4bb0feeb8cbf3735da6b9712 Mon Sep 17 00:00:00 2001 From: Austin de Coup-Crank Date: Tue, 4 Feb 2025 19:29:53 -0600 Subject: [PATCH 4/4] Fix test --- tests/test_wait.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_wait.py b/tests/test_wait.py index aac4052..7df38a4 100644 --- a/tests/test_wait.py +++ b/tests/test_wait.py @@ -11,7 +11,7 @@ @retry( retry=retry_if_server_error(), - wait=wait_from_header(header="Retry-After", wait_max=5), + wait=wait_from_header(header="Retry-After", wait_max=5, fallback=None), stop=stop_after_attempt(3), ) def planned_downtime_impatient_no_fallback():