Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry connection #7363

Merged
merged 13 commits into from
Jan 20, 2024
1 change: 1 addition & 0 deletions CHANGES/7297.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a feature to retry closed connections automatically for idempotent methods. -- by :user:`Dreamsorcerer`
10 changes: 10 additions & 0 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ class ClientTimeout:
# 5 Minute default read timeout
DEFAULT_TIMEOUT: Final[ClientTimeout] = ClientTimeout(total=5 * 60)

# https://www.rfc-editor.org/rfc/rfc9110#section-9.2.2
IDEMPOTENT_METHODS = frozenset({"GET", "HEAD", "OPTIONS", "TRACE", "PUT", "DELETE"})

_RetType = TypeVar("_RetType")


Expand Down Expand Up @@ -435,6 +438,8 @@ async def _request(
timer = tm.timer()
try:
with timer:
# https://www.rfc-editor.org/rfc/rfc9112.html#name-retrying-requests
retry_persistent_connection = method in IDEMPOTENT_METHODS
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
while True:
url, auth_from_url = strip_auth_from_url(url)
if auth and auth_from_url:
Expand Down Expand Up @@ -541,6 +546,11 @@ async def _request(
except BaseException:
conn.close()
raise
except (ClientOSError, ServerDisconnectedError):
if retry_persistent_connection:
retry_persistent_connection = False
continue
raise
except ClientError:
raise
except OSError as exc:
Expand Down
86 changes: 74 additions & 12 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import pathlib
import socket
import ssl
import sys
import time
from typing import Any, AsyncIterator
from unittest import mock

Expand Down Expand Up @@ -126,6 +128,67 @@ async def handler(request):
assert 0 == len(client._session.connector._conns)


async def test_keepalive_timeout_async_sleep() -> None:
async def handler(request):
body = await request.read()
assert b"" == body
return web.Response(body=b"OK")

app = web.Application()
app.router.add_route("GET", "/", handler)

runner = web.AppRunner(app, tcp_keepalive=True, keepalive_timeout=0.001)
await runner.setup()

port = unused_port()
site = web.TCPSite(runner, host="localhost", port=port)
await site.start()

try:
async with aiohttp.client.ClientSession() as sess:
resp1 = await sess.get(f"http://localhost:{port}/")
await resp1.read()
# wait for server keepalive_timeout
await asyncio.sleep(0.01)
resp2 = await sess.get(f"http://localhost:{port}/")
await resp2.read()
finally:
await asyncio.gather(runner.shutdown(), site.stop())


@pytest.mark.skipif(
sys.version_info[:2] == (3, 11),
reason="https://github.com/pytest-dev/pytest/issues/10763",
)
async def test_keepalive_timeout_sync_sleep() -> None:
async def handler(request):
body = await request.read()
assert b"" == body
return web.Response(body=b"OK")

app = web.Application()
app.router.add_route("GET", "/", handler)

runner = web.AppRunner(app, tcp_keepalive=True, keepalive_timeout=0.001)
await runner.setup()

port = unused_port()
site = web.TCPSite(runner, host="localhost", port=port)
await site.start()

try:
async with aiohttp.client.ClientSession() as sess:
resp1 = await sess.get(f"http://localhost:{port}/")
await resp1.read()
# wait for server keepalive_timeout
# time.sleep is a more challenging scenario than asyncio.sleep
time.sleep(0.01)
resp2 = await sess.get(f"http://localhost:{port}/")
await resp2.read()
finally:
await asyncio.gather(runner.shutdown(), site.stop())


async def test_release_early(aiohttp_client: Any) -> None:
async def handler(request):
await request.read()
Expand Down Expand Up @@ -2838,21 +2901,20 @@ def connection_lost(self, exc):

addr = server.sockets[0].getsockname()

connector = aiohttp.TCPConnector(limit=1)
session = aiohttp.ClientSession(connector=connector)
async with aiohttp.TCPConnector(limit=1) as connector:
async with aiohttp.ClientSession(connector=connector) as session:
url = "http://{}:{}/".format(*addr)

url = "http://{}:{}/".format(*addr)
r = await session.request("GET", url)
await r.read()
assert 1 == len(connector._conns)
closed_conn = next(iter(connector._conns.values()))

r = await session.request("GET", url)
await r.read()
assert 1 == len(connector._conns)
await session.request("GET", url)
assert 1 == len(connector._conns)
new_conn = next(iter(connector._conns.values()))
assert closed_conn is not new_conn

with pytest.raises(aiohttp.ClientConnectionError):
await session.request("GET", url)
assert 0 == len(connector._conns)

await session.close()
await connector.close()
server.close()
await server.wait_closed()

Expand Down
Loading