Retry on server errors too

This commit is contained in:
Šarūnas Nejus 2025-12-24 22:19:13 +00:00
parent 1447f49b72
commit 55b9c1c145
No known key found for this signature in database
2 changed files with 24 additions and 4 deletions

View file

@ -67,7 +67,7 @@ class TimeoutAndRetrySession(requests.Session, metaclass=SingletonMeta):
* default beets User-Agent header
* default request timeout
* automatic retries on transient connection errors
* automatic retries on transient connection or server errors
* raises exceptions for HTTP error status codes
"""
@ -75,7 +75,18 @@ class TimeoutAndRetrySession(requests.Session, metaclass=SingletonMeta):
super().__init__(*args, **kwargs)
self.headers["User-Agent"] = f"beets/{__version__} https://beets.io/"
retry = Retry(connect=2, total=2, backoff_factor=1)
retry = Retry(
connect=2,
total=2,
backoff_factor=1,
# Retry on server errors
status_forcelist=[
HTTPStatus.INTERNAL_SERVER_ERROR,
HTTPStatus.BAD_GATEWAY,
HTTPStatus.SERVICE_UNAVAILABLE,
HTTPStatus.GATEWAY_TIMEOUT,
],
)
adapter = HTTPAdapter(max_retries=retry)
self.mount("https://", adapter)
self.mount("http://", adapter)

View file

@ -48,11 +48,20 @@ class TestRequestHandlerRetry:
assert response.status_code == HTTPStatus.OK
@pytest.mark.parametrize(
"last_response", [ConnectionResetError], ids=["conn_error"]
"last_response",
[
ConnectionResetError,
HTTPResponse(
body=io.BytesIO(b"Server Error"),
status=HTTPStatus.INTERNAL_SERVER_ERROR,
preload_content=False,
),
],
ids=["conn_error", "server_error"],
)
def test_retry_exhaustion(self, request_handler):
"""Verify that the handler raises an error after exhausting retries."""
with pytest.raises(
requests.exceptions.ConnectionError, match="Max retries exceeded"
requests.exceptions.RequestException, match="Max retries exceeded"
):
request_handler.get("http://example.com/api")