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

[PR #8662/be23d16f backport][3.10] Improve performance of keepalive rescheduling #8670

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES/8662.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improved performance of HTTP keep-alive checks -- by :user:`bdraco`.

Previously, when processing a request for a keep-alive connection, the keep-alive check would happen every second; the check is now rescheduled if it fires too early instead.
33 changes: 15 additions & 18 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,14 @@ class RequestHandler(BaseProtocol):

"""

KEEPALIVE_RESCHEDULE_DELAY = 1

__slots__ = (
"_request_count",
"_keepalive",
"_manager",
"_request_handler",
"_request_factory",
"_tcp_keepalive",
"_keepalive_time",
"_next_keepalive_close_time",
"_keepalive_handle",
"_keepalive_timeout",
"_lingering_time",
Expand Down Expand Up @@ -197,7 +195,7 @@ def __init__(

self._tcp_keepalive = tcp_keepalive
# placeholder to be replaced on keepalive timeout setup
self._keepalive_time = 0.0
self._next_keepalive_close_time = 0.0
self._keepalive_handle: Optional[asyncio.Handle] = None
self._keepalive_timeout = keepalive_timeout
self._lingering_time = float(lingering_time)
Expand Down Expand Up @@ -429,23 +427,21 @@ def log_exception(self, *args: Any, **kw: Any) -> None:
self.logger.exception(*args, **kw)

def _process_keepalive(self) -> None:
self._keepalive_handle = None
if self._force_close or not self._keepalive:
return

next = self._keepalive_time + self._keepalive_timeout
loop = self._loop
now = loop.time()
close_time = self._next_keepalive_close_time
if now <= close_time:
# Keep alive close check fired too early, reschedule
self._keepalive_handle = loop.call_at(close_time, self._process_keepalive)
return

# handler in idle state
if self._waiter:
if self._loop.time() > next:
self.force_close()
return

# not all request handlers are done,
# reschedule itself to next second
self._keepalive_handle = self._loop.call_later(
self.KEEPALIVE_RESCHEDULE_DELAY,
self._process_keepalive,
)
self.force_close()

async def _handle_request(
self,
Expand Down Expand Up @@ -596,11 +592,12 @@ async def start(self) -> None:
if self._keepalive and not self._close:
# start keep-alive timer
if keepalive_timeout is not None:
now = self._loop.time()
self._keepalive_time = now
now = loop.time()
close_time = now + keepalive_timeout
self._next_keepalive_close_time = close_time
if self._keepalive_handle is None:
self._keepalive_handle = loop.call_at(
now + keepalive_timeout, self._process_keepalive
close_time, self._process_keepalive
)
else:
break
Expand Down
Loading