Skip to content

Commit 14d5295

Browse files
authored
Improve performance of WebSockets when there is no timeout (#8660)
1 parent 6c6ecfa commit 14d5295

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

CHANGES/8660.misc.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improved performance of :py:meth:`~aiohttp.ClientWebSocketResponse.receive` and :py:meth:`~aiohttp.web.WebSocketResponse.receive` when there is no timeout. -- by :user:`bdraco`.
2+
3+
The timeout context manager is now avoided when there is no timeout as it accounted for up to 50% of the time spent in the :py:meth:`~aiohttp.ClientWebSocketResponse.receive` and :py:meth:`~aiohttp.web.WebSocketResponse.receive` methods.

aiohttp/client_ws.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ async def close(self, *, code: int = WSCloseCode.OK, message: bytes = b"") -> bo
291291
return False
292292

293293
async def receive(self, timeout: Optional[float] = None) -> WSMessage:
294+
receive_timeout = timeout or self._timeout.ws_receive
295+
294296
while True:
295297
if self._waiting:
296298
raise RuntimeError("Concurrent call to receive() is not allowed")
@@ -304,9 +306,14 @@ async def receive(self, timeout: Optional[float] = None) -> WSMessage:
304306
try:
305307
self._waiting = True
306308
try:
307-
async with async_timeout.timeout(
308-
timeout or self._timeout.ws_receive
309-
):
309+
if receive_timeout:
310+
# Entering the context manager and creating
311+
# Timeout() object can take almost 50% of the
312+
# run time in this loop so we avoid it if
313+
# there is no read timeout.
314+
async with async_timeout.timeout(receive_timeout):
315+
msg = await self._reader.read()
316+
else:
310317
msg = await self._reader.read()
311318
self._reset_heartbeat()
312319
finally:

aiohttp/web_ws.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ async def receive(self, timeout: Optional[float] = None) -> WSMessage:
509509

510510
loop = self._loop
511511
assert loop is not None
512+
receive_timeout = timeout or self._receive_timeout
512513
while True:
513514
if self._waiting:
514515
raise RuntimeError("Concurrent call to receive() is not allowed")
@@ -524,7 +525,14 @@ async def receive(self, timeout: Optional[float] = None) -> WSMessage:
524525
try:
525526
self._waiting = True
526527
try:
527-
async with async_timeout.timeout(timeout or self._receive_timeout):
528+
if receive_timeout:
529+
# Entering the context manager and creating
530+
# Timeout() object can take almost 50% of the
531+
# run time in this loop so we avoid it if
532+
# there is no read timeout.
533+
async with async_timeout.timeout(receive_timeout):
534+
msg = await self._reader.read()
535+
else:
528536
msg = await self._reader.read()
529537
self._reset_heartbeat()
530538
finally:

0 commit comments

Comments
 (0)