From f96182adab30d8609342c6d273c5aad1cd92b29f Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Fri, 9 Aug 2024 17:17:22 +0000 Subject: [PATCH] [PR #8662/be23d16f backport][3.10] Improve performance of keepalive rescheduling (#8670) Co-authored-by: J. Nick Koston --- CHANGES/8662.misc.rst | 3 +++ aiohttp/web_protocol.py | 33 +++++++++++++++------------------ 2 files changed, 18 insertions(+), 18 deletions(-) create mode 100644 CHANGES/8662.misc.rst diff --git a/CHANGES/8662.misc.rst b/CHANGES/8662.misc.rst new file mode 100644 index 00000000000..efe30a60cb2 --- /dev/null +++ b/CHANGES/8662.misc.rst @@ -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. diff --git a/aiohttp/web_protocol.py b/aiohttp/web_protocol.py index f60759d927b..635b668ceb0 100644 --- a/aiohttp/web_protocol.py +++ b/aiohttp/web_protocol.py @@ -134,8 +134,6 @@ class RequestHandler(BaseProtocol): """ - KEEPALIVE_RESCHEDULE_DELAY = 1 - __slots__ = ( "_request_count", "_keepalive", @@ -143,7 +141,7 @@ class RequestHandler(BaseProtocol): "_request_handler", "_request_factory", "_tcp_keepalive", - "_keepalive_time", + "_next_keepalive_close_time", "_keepalive_handle", "_keepalive_timeout", "_lingering_time", @@ -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) @@ -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, @@ -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