Skip to content

Commit

Permalink
Do not retry low timeout response (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pliner authored Oct 22, 2024
1 parent 2676bf0 commit 4404b5d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
11 changes: 9 additions & 2 deletions aio_request/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@ async def execute(


class LowTimeoutModule(RequestModule):
__slots__ = ("__low_timeout_threshold",)
__slots__ = ("__low_timeout_threshold", "__timeout_response")

def __init__(self, low_timeout_threshold: float):
self.__low_timeout_threshold = low_timeout_threshold

headers = multidict.CIMultiDict[str]()
headers[Header.X_DO_NOT_RETRY] = "1"
self.__timeout_response = EmptyResponse(
status=408,
headers=multidict.CIMultiDictProxy[str](headers),
)

async def execute(
self,
next: NextModuleFunc,
Expand All @@ -62,7 +69,7 @@ async def execute(
priority: Priority,
) -> ClosableResponse:
if deadline.expired or deadline.timeout < self.__low_timeout_threshold:
return EmptyResponse(status=408)
return self.__timeout_response

return await next(endpoint, request, deadline, priority)

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@dataclasses.dataclass(frozen=True)
class FakeResponseConfiguration:
status: int
delay_seconds: float
delay_seconds: float = 0


class FakeTransport(aio_request.Transport):
Expand Down
19 changes: 18 additions & 1 deletion tests/test_parallel_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,24 @@
from .conftest import FakeResponseConfiguration, FakeTransport


async def test_timeout_because_of_expiration():
async def test_timeout_due_to_low_timeout():
client = aio_request.setup(
transport=FakeTransport([FakeResponseConfiguration(status=200)]),
endpoint="http://service.com",
)
deadline = aio_request.Deadline.from_timeout(0.004)
response_ctx = client.request(
aio_request.get("hello"),
deadline=deadline,
strategy=aio_request.parallel_strategy(),
)
async with response_ctx as response:
assert response.status == 408
assert aio_request.Header.X_DO_NOT_RETRY in response.headers
assert not deadline.expired


async def test_timeout_due_to_expiration():
client = aio_request.setup(
transport=FakeTransport(
[
Expand Down
19 changes: 18 additions & 1 deletion tests/test_sequential_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,24 @@
from .conftest import FakeResponseConfiguration, FakeTransport


async def test_timeout_because_of_expiration():
async def test_timeout_due_to_low_timeout():
client = aio_request.setup(
transport=FakeTransport([FakeResponseConfiguration(status=200)]),
endpoint="http://service.com",
)
deadline = aio_request.Deadline.from_timeout(0.004)
response_ctx = client.request(
aio_request.get("hello"),
deadline=deadline,
strategy=aio_request.sequential_strategy(attempts_count=3, delays_provider=aio_request.linear_backoff_delays()),
)
async with response_ctx as response:
assert response.status == 408
assert aio_request.Header.X_DO_NOT_RETRY in response.headers
assert not deadline.expired


async def test_timeout_due_to_expiration():
client = aio_request.setup(
transport=FakeTransport([FakeResponseConfiguration(status=200, delay_seconds=5)]),
endpoint="http://service.com",
Expand Down

0 comments on commit 4404b5d

Please sign in to comment.