Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lghorizon/lghorizon_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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", {}):
Expand Down
51 changes: 51 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down