Skip to content

Commit d5fc899

Browse files
committed
Avoid an extra "can_read" call and use timeout directly.
1 parent cdbc662 commit d5fc899

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

redis/asyncio/client.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -759,18 +759,8 @@ async def parse_response(self, block: bool = True, timeout: float = 0):
759759
if not conn.is_connected:
760760
await conn.connect()
761761

762-
if not block:
763-
764-
async def read_with_timeout():
765-
try:
766-
async with async_timeout.timeout(timeout):
767-
return await conn.read_response()
768-
except asyncio.TimeoutError:
769-
return None
770-
771-
response = await self._execute(conn, read_with_timeout)
772-
else:
773-
response = await self._execute(conn, conn.read_response)
762+
read_timeout = None if block else timeout
763+
response = await self._execute(conn, conn.read_response, timeout=read_timeout)
774764

775765
if conn.health_check_interval and response == self.health_check_response:
776766
# ignore the health check message as user might not expect it

redis/asyncio/connection.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import contextlib
23
import copy
34
import enum
45
import errno
@@ -55,6 +56,19 @@
5556
if HIREDIS_AVAILABLE:
5657
import hiredis
5758

59+
if sys.version_info[:2] >= (3, 10):
60+
nullcontext = contextlib.nullcontext()
61+
else:
62+
63+
class NullContext:
64+
async def __aenter__(self):
65+
pass
66+
67+
async def __aexit__(self, *args):
68+
pass
69+
70+
nullcontext = NullContext()
71+
5872
NONBLOCKING_EXCEPTION_ERROR_NUMBERS = {
5973
BlockingIOError: errno.EWOULDBLOCK,
6074
ssl.SSLWantReadError: 2,
@@ -922,19 +936,23 @@ async def can_read_destructive(self):
922936
f"Error while reading from {self.host}:{self.port}: {e.args}"
923937
)
924938

925-
async def read_response(self, disable_decoding: bool = False):
939+
async def read_response(
940+
self,
941+
disable_decoding: bool = False,
942+
timeout: Optional[float] = None,
943+
):
926944
"""Read the response from a previously sent command"""
945+
read_timeout = timeout if timeout is not None else self.socket_timeout
927946
try:
928-
if self.socket_timeout:
929-
async with async_timeout.timeout(self.socket_timeout):
930-
response = await self._parser.read_response(
931-
disable_decoding=disable_decoding
932-
)
933-
else:
947+
async with async_timeout.timeout(read_timeout):
934948
response = await self._parser.read_response(
935949
disable_decoding=disable_decoding
936950
)
937951
except asyncio.TimeoutError:
952+
if timeout is not None:
953+
# user requested timeout, return None
954+
return None
955+
# it was a self.socket_timeout error.
938956
await self.disconnect(nowait=True)
939957
raise TimeoutError(f"Timeout reading from {self.host}:{self.port}")
940958
except OSError as e:

0 commit comments

Comments
 (0)