Skip to content

Commit 64a2671

Browse files
lesnik512claude
andcommitted
feat(resilience): Retry — network/timeout exception retry
Retries NetworkError and TimeoutError on idempotent methods. Bare TransportError (e.g., InvalidURL) is NOT retried since it escaped the NetworkError refinement in errors.py. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 7fa7a5d commit 64a2671

2 files changed

Lines changed: 87 additions & 3 deletions

File tree

‎src/httpware/middleware/resilience/retry.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import httpx2
1515

16-
from httpware.errors import RetryBudgetExhaustedError, StatusError
16+
from httpware.errors import NetworkError, RetryBudgetExhaustedError, StatusError, TimeoutError # noqa: A004
1717
from httpware.middleware import Next
1818
from httpware.middleware.resilience._backoff import full_jitter_delay
1919
from httpware.middleware.resilience.budget import RetryBudget
@@ -86,6 +86,11 @@ async def __call__(self, request: httpx2.Request, next: Next) -> httpx2.Response
8686
raise
8787
last_exc = exc
8888
last_response = exc.response
89+
except (NetworkError, TimeoutError) as exc:
90+
if not method_eligible:
91+
raise
92+
last_exc = exc
93+
last_response = None
8994

9095
# ---- retryable failure path
9196
if is_last:

‎tests/test_retry.py‎

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import httpx2
1111
import pytest
1212

13-
from httpware import AsyncClient, NotFoundError, ServiceUnavailableError
14-
from httpware.errors import RetryBudgetExhaustedError
13+
from httpware import AsyncClient, NotFoundError, ServiceUnavailableError, TransportError
14+
from httpware.errors import NetworkError, RetryBudgetExhaustedError
1515
from httpware.middleware.resilience.budget import RetryBudget
1616
from httpware.middleware.resilience.retry import (
1717
DEFAULT_IDEMPOTENT_METHODS,
@@ -155,3 +155,82 @@ async def test_budget_exhausted_raises_retry_budget_exhausted_error() -> None:
155155
assert handler.calls == 1
156156
assert info.value.attempts == 1
157157
assert sleeper.calls == []
158+
159+
160+
async def test_retries_on_network_error() -> None:
161+
sleeper = _SleepRecorder()
162+
call_count = {"n": 0}
163+
164+
def handler(request: httpx2.Request) -> httpx2.Response:
165+
call_count["n"] += 1
166+
if call_count["n"] < 2: # noqa: PLR2004 — "2" is intentional literal in test assertion
167+
msg = "transient"
168+
raise httpx2.ConnectError(msg)
169+
return httpx2.Response(HTTPStatus.OK, request=request)
170+
171+
client = _client(handler, retry=Retry(_sleep=sleeper, base_delay=0.01, max_delay=0.02))
172+
response = await client.get("https://example.test/x")
173+
assert response.status_code == HTTPStatus.OK
174+
assert call_count["n"] == 2 # noqa: PLR2004 — "2" is intentional literal in test assertion
175+
assert len(sleeper.calls) == 1
176+
177+
178+
async def test_retries_on_httpware_timeout_error() -> None:
179+
sleeper = _SleepRecorder()
180+
call_count = {"n": 0}
181+
182+
def handler(request: httpx2.Request) -> httpx2.Response:
183+
call_count["n"] += 1
184+
if call_count["n"] < 2: # noqa: PLR2004 — "2" is intentional literal in test assertion
185+
msg = "read timeout"
186+
raise httpx2.ReadTimeout(msg)
187+
return httpx2.Response(HTTPStatus.OK, request=request)
188+
189+
client = _client(handler, retry=Retry(_sleep=sleeper, base_delay=0.01, max_delay=0.02))
190+
response = await client.get("https://example.test/x")
191+
assert response.status_code == HTTPStatus.OK
192+
assert call_count["n"] == 2 # noqa: PLR2004 — "2" is intentional literal in test assertion
193+
194+
195+
async def test_does_not_retry_on_bare_transport_error_like_invalid_url() -> None:
196+
sleeper = _SleepRecorder()
197+
198+
def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
199+
msg = "bad url"
200+
raise httpx2.InvalidURL(msg)
201+
202+
client = _client(handler, retry=Retry(_sleep=sleeper))
203+
with pytest.raises(TransportError) as info:
204+
await client.get("https://example.test/x")
205+
assert not isinstance(info.value, NetworkError)
206+
assert sleeper.calls == []
207+
208+
209+
async def test_network_error_exhaustion_reraises_with_note() -> None:
210+
sleeper = _SleepRecorder()
211+
212+
def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
213+
msg = "never works"
214+
raise httpx2.ConnectError(msg)
215+
216+
client = _client(handler, retry=Retry(_sleep=sleeper, max_attempts=2, base_delay=0.01, max_delay=0.02))
217+
with pytest.raises(NetworkError) as info:
218+
await client.get("https://example.test/x")
219+
notes = getattr(info.value, "__notes__", [])
220+
assert any("gave up after 2 attempts" in note for note in notes)
221+
222+
223+
async def test_does_not_retry_network_error_on_non_idempotent_method() -> None:
224+
sleeper = _SleepRecorder()
225+
call_count = {"n": 0}
226+
227+
def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
228+
call_count["n"] += 1
229+
msg = "transient"
230+
raise httpx2.ConnectError(msg)
231+
232+
client = _client(handler, retry=Retry(_sleep=sleeper))
233+
with pytest.raises(NetworkError):
234+
await client.post("https://example.test/x", json={"x": 1})
235+
assert call_count["n"] == 1
236+
assert sleeper.calls == []

0 commit comments

Comments
 (0)