Skip to content

Commit

Permalink
fix callback; add callback to list retry
Browse files Browse the repository at this point in the history
  • Loading branch information
inyutin committed Aug 11, 2022
1 parent 3f8353f commit c5e6bb7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Python 3.7 or higher.


### Breaking API changes
- Everything between [2.7.0 - 2.8.3) is yanked.
There is a bug with evaluate_response_callback, it led to infinite retries

- 2.8.0 is incorrect and yanked.
https://github.com/inyutin/aiohttp_retry/issues/79

Expand Down
2 changes: 1 addition & 1 deletion aiohttp_retry/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async def _do_request(self) -> ClientResponse:
else:
is_response_correct = True

if is_response_correct:
if is_response_correct or current_attempt == self._retry_options.attempts:
self._response = response
return response
else:
Expand Down
9 changes: 8 additions & 1 deletion aiohttp_retry/retry_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,15 @@ def __init__(
statuses: Optional[Iterable[int]] = None, # On which statuses we should retry
exceptions: Optional[Iterable[Type[Exception]]] = None, # On which exceptions we should retry
retry_all_server_errors: bool = True,
evaluate_response_callback: Optional[EvaluateResponseCallbackType] = None,
):
super().__init__(len(timeouts), statuses, exceptions, retry_all_server_errors)
super().__init__(
attempts=len(timeouts),
statuses=statuses,
exceptions=exceptions,
retry_all_server_errors=retry_all_server_errors,
evaluate_response_callback=evaluate_response_callback,
)
self.timeouts = timeouts

def get_timeout(self, attempt: int, response: Optional[ClientResponse] = None) -> float:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='aiohttp_retry',
version='2.8.2',
version='2.8.3',
description='Simple retry client for aiohttp',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
2 changes: 2 additions & 0 deletions tests/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ async def check_headers(self, request: web.Request) -> web.Response:
return web.Response(text='Ok!', status=200)

async def with_auth(self, request: web.Request) -> web.Response:
self.counter += 1

# BasicAuth("username", "password")
if request.headers.get('Authorization') != 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=':
return web.Response(text='incorrect auth', status=403)
Expand Down
17 changes: 16 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)
from yarl import URL

from aiohttp_retry import ExponentialRetry, ListRetry, RetryClient, retry_options
from aiohttp_retry import ExponentialRetry, ListRetry, RetryClient
from aiohttp_retry.client import RequestParams
from aiohttp_retry.retry_options import RetryOptionsBase
from tests.app import App
Expand Down Expand Up @@ -460,3 +460,18 @@ async def test_request_headers(aiohttp_client):
assert test_app.counter == 1

await retry_client.close()


async def test_list_retry_all_failed(aiohttp_client):
# there was a specific bug
async def evaluate_response(response: ClientResponse) -> bool:
return False

retry_options = ListRetry(timeouts=[1]*3, statuses={403}, evaluate_response_callback=evaluate_response)
retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)

async with retry_client.get('/with_auth', retry_options=retry_options) as response:
assert response.status == 403
assert test_app.counter == 3

await retry_client.close()

0 comments on commit c5e6bb7

Please sign in to comment.