diff --git a/lghorizon/lghorizon_models.py b/lghorizon/lghorizon_models.py index 824a84c..71376dd 100644 --- a/lghorizon/lghorizon_models.py +++ b/lghorizon/lghorizon_models.py @@ -35,6 +35,16 @@ } +def _is_permanent_client_error(error: LGHorizonApiConnectionError) -> bool: + """Return whether the error is an HTTP 4xx that a retry cannot fix.""" + cause = error.__cause__ + return ( + isinstance(cause, ClientResponseError) + and 400 <= cause.status < 500 + and cause.status not in (408, 429) + ) + + def _retry_after_seconds(error: ClientResponseError) -> float: """Return the Retry-After delay of a 429 response, capped.""" try: @@ -814,7 +824,12 @@ async def _get_json( ) return json_response - @backoff.on_exception(backoff.expo, LGHorizonApiConnectionError, max_tries=3) + @backoff.on_exception( + backoff.expo, + LGHorizonApiConnectionError, + max_tries=3, + giveup=_is_permanent_client_error, + ) async def request(self, host: str, path: str, params=None, **kwargs) -> Any: """Make a request.""" if headers := kwargs.pop("headers", {}): diff --git a/tests/test_models.py b/tests/test_models.py index 7e928e1..9fdbdfe 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -2489,6 +2489,57 @@ async def test_429_waits_retry_after(self, header, expected): assert sleep.await_args_list[0].args == (expected,) + @pytest.mark.parametrize( + ("status", "expected_attempts"), + [ + pytest.param(400, 1, id="400"), + pytest.param(403, 1, id="403"), + pytest.param(404, 1, id="404"), + pytest.param(408, 3, id="408"), + pytest.param(429, 3, id="429"), + pytest.param(500, 3, id="500"), + pytest.param(502, 3, id="502"), + ], + ) + async def test_only_retryable_errors_are_retried( + self, status: int, expected_attempts: int + ): + auth = make_auth() + auth.websession.request = AsyncMock(return_value=make_error_response(status)) + + with ( + patch("lghorizon.lghorizon_models.asyncio.sleep", new=AsyncMock()), + pytest.raises(LGHorizonApiConnectionError), + ): + await auth.request("http://host", "/path") + + assert auth.websession.request.call_count == expected_attempts + + async def test_401_after_token_refresh_is_not_retried_again(self): + auth = make_auth() + auth.websession.request = AsyncMock(return_value=make_error_response(401)) + + with ( + patch("lghorizon.lghorizon_models.asyncio.sleep", new=AsyncMock()), + pytest.raises(LGHorizonApiConnectionError), + ): + await auth.request("http://host", "/path") + + auth.fetch_access_token.assert_called_once() + assert auth.websession.request.call_count == 2 + + async def test_network_errors_are_retried(self): + auth = make_auth() + auth.websession.request = AsyncMock(side_effect=OSError("down")) + + with ( + patch("lghorizon.lghorizon_models.asyncio.sleep", new=AsyncMock()), + pytest.raises(LGHorizonApiConnectionError), + ): + await auth.request("http://host", "/path") + + assert auth.websession.request.call_count == 3 + async def test_non_429_error_does_not_sleep(self): auth = make_auth() auth.websession.request = AsyncMock(return_value=make_error_response(500))