mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-29 12:35:13 +00:00
fix(session): log "Retrying" only when a retry follows
A per-call max_retries=0 logged "Retrying... (1/0)" before raising, and the last of the default retries logged "(6/5)".
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
"""RnetSession.request logs "Retrying" only when a retry follows."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from unshackle.core import session as session_module
|
||||||
|
from unshackle.core.session import MaxRetriesError, RnetSession
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def rate_limited(monkeypatch):
|
||||||
|
s = RnetSession()
|
||||||
|
monkeypatch.setattr(s, "ensure_client", lambda: SimpleNamespace(request=lambda *a, **k: None))
|
||||||
|
monkeypatch.setattr(s, "get_sleep_time", lambda *a: 0)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
session_module, "RnetResponse", lambda raw: SimpleNamespace(status_code=429, reason="Too Many Requests")
|
||||||
|
)
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("max_retries", [0, 2])
|
||||||
|
def test_retry_log_counts_only_real_retries(rate_limited, caplog, max_retries):
|
||||||
|
with caplog.at_level(logging.WARNING), pytest.raises(MaxRetriesError):
|
||||||
|
rate_limited.get("https://example.invalid/x", max_retries=max_retries)
|
||||||
|
|
||||||
|
retries = [r.getMessage() for r in caplog.records if "Retrying" in r.getMessage()]
|
||||||
|
assert retries == [f"429 Too Many Requests(/x). Retrying... ({n}/{max_retries})" for n in range(1, max_retries + 1)]
|
||||||
@@ -835,19 +835,15 @@ class RnetSession:
|
|||||||
if response.status_code not in self.status_forcelist:
|
if response.status_code not in self.status_forcelist:
|
||||||
return response
|
return response
|
||||||
last_exception = HTTPError(f"Received status code: {response.status_code}", response=response)
|
last_exception = HTTPError(f"Received status code: {response.status_code}", response=response)
|
||||||
self.log.warning(
|
failure = f"{response.status_code} {response.reason}"
|
||||||
f"{response.status_code} {response.reason}({urlparse(url).path}). Retrying... "
|
|
||||||
f"({attempt + 1}/{max_retries})"
|
|
||||||
)
|
|
||||||
|
|
||||||
except self.catch_exceptions as e:
|
except self.catch_exceptions as e:
|
||||||
last_exception = e
|
last_exception = e
|
||||||
response = None
|
response = None
|
||||||
self.log.warning(
|
failure = e.__class__.__name__
|
||||||
f"{e.__class__.__name__}({urlparse(url).path}). Retrying... ({attempt + 1}/{max_retries})"
|
|
||||||
)
|
|
||||||
|
|
||||||
if attempt < max_retries:
|
if attempt < max_retries:
|
||||||
|
self.log.warning(f"{failure}({urlparse(url).path}). Retrying... ({attempt + 1}/{max_retries})")
|
||||||
if sleep_duration := self.get_sleep_time(response, attempt + 1):
|
if sleep_duration := self.get_sleep_time(response, attempt + 1):
|
||||||
if sleep_duration > 0:
|
if sleep_duration > 0:
|
||||||
time.sleep(sleep_duration)
|
time.sleep(sleep_duration)
|
||||||
|
|||||||
Reference in New Issue
Block a user