@@ -208,7 +208,7 @@ def on_disconnect(self):
208208 def on_connect (self , connection : "Connection" ):
209209 raise NotImplementedError ()
210210
211- async def can_read (self , timeout : float ) -> bool :
211+ async def can_read_destructive (self ) -> bool :
212212 raise NotImplementedError ()
213213
214214 async def read_response (
@@ -286,9 +286,9 @@ async def _read_from_socket(
286286 return False
287287 raise ConnectionError (f"Error while reading from socket: { ex .args } " )
288288
289- async def can_read (self , timeout : float ) -> bool :
289+ async def can_read_destructive (self ) -> bool :
290290 return bool (self .length ) or await self ._read_from_socket (
291- timeout = timeout , raise_on_timeout = False
291+ timeout = 0 , raise_on_timeout = False
292292 )
293293
294294 async def read (self , length : int ) -> bytes :
@@ -386,8 +386,8 @@ def on_disconnect(self):
386386 self ._buffer = None
387387 self .encoder = None
388388
389- async def can_read (self , timeout : float ):
390- return self ._buffer and bool (await self ._buffer .can_read ( timeout ))
389+ async def can_read_destructive (self ):
390+ return self ._buffer and bool (await self ._buffer .can_read_destructive ( ))
391391
392392 async def read_response (
393393 self , disable_decoding : bool = False
@@ -444,9 +444,7 @@ async def read_response(
444444class HiredisParser (BaseParser ):
445445 """Parser class for connections using Hiredis"""
446446
447- __slots__ = BaseParser .__slots__ + ("_next_response" , "_reader" , "_socket_timeout" )
448-
449- _next_response : bool
447+ __slots__ = BaseParser .__slots__ + ("_reader" , "_socket_timeout" )
450448
451449 def __init__ (self , socket_read_size : int ):
452450 if not HIREDIS_AVAILABLE :
@@ -466,23 +464,18 @@ def on_connect(self, connection: "Connection"):
466464 kwargs ["errors" ] = connection .encoder .encoding_errors
467465
468466 self ._reader = hiredis .Reader (** kwargs )
469- self ._next_response = False
470467 self ._socket_timeout = connection .socket_timeout
471468
472469 def on_disconnect (self ):
473470 self ._stream = None
474471 self ._reader = None
475- self ._next_response = False
476472
477- async def can_read (self , timeout : float ):
473+ async def can_read_destructive (self ):
478474 if not self ._stream or not self ._reader :
479475 raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR )
480-
481- if self ._next_response is False :
482- self ._next_response = self ._reader .gets ()
483- if self ._next_response is False :
484- return await self .read_from_socket (timeout = timeout , raise_on_timeout = False )
485- return True
476+ if self ._reader .gets ():
477+ return True
478+ return await self .read_from_socket (timeout = 0 , raise_on_timeout = False )
486479
487480 async def read_from_socket (
488481 self ,
@@ -523,12 +516,6 @@ async def read_response(
523516 self .on_disconnect ()
524517 raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR ) from None
525518
526- # _next_response might be cached from a can_read() call
527- if self ._next_response is not False :
528- response = self ._next_response
529- self ._next_response = False
530- return response
531-
532519 response = self ._reader .gets ()
533520 while response is False :
534521 await self .read_from_socket ()
@@ -925,12 +912,10 @@ async def send_command(self, *args: Any, **kwargs: Any) -> None:
925912 self .pack_command (* args ), check_health = kwargs .get ("check_health" , True )
926913 )
927914
928- async def can_read (self , timeout : float = 0 ):
915+ async def can_read_destructive (self ):
929916 """Poll the socket to see if there's data that can be read."""
930- if not self .is_connected :
931- await self .connect ()
932917 try :
933- return await self ._parser .can_read ( timeout )
918+ return await self ._parser .can_read_destructive ( )
934919 except OSError as e :
935920 await self .disconnect (nowait = True )
936921 raise ConnectionError (
@@ -957,6 +942,10 @@ async def read_response(self, disable_decoding: bool = False):
957942 raise ConnectionError (
958943 f"Error while reading from { self .host } :{ self .port } : { e .args } "
959944 )
945+ except asyncio .CancelledError :
946+ # need this check for 3.7, where CancelledError
947+ # is subclass of Exception, not BaseException
948+ raise
960949 except Exception :
961950 await self .disconnect (nowait = True )
962951 raise
@@ -1498,12 +1487,12 @@ async def get_connection(self, command_name, *keys, **options):
14981487 # pool before all data has been read or the socket has been
14991488 # closed. either way, reconnect and verify everything is good.
15001489 try :
1501- if await connection .can_read ():
1490+ if await connection .can_read_destructive ():
15021491 raise ConnectionError ("Connection has data" ) from None
15031492 except ConnectionError :
15041493 await connection .disconnect ()
15051494 await connection .connect ()
1506- if await connection .can_read ():
1495+ if await connection .can_read_destructive ():
15071496 raise ConnectionError ("Connection not ready" ) from None
15081497 except BaseException :
15091498 # release the connection back to the pool so that we don't
@@ -1699,12 +1688,12 @@ async def get_connection(self, command_name, *keys, **options):
16991688 # pool before all data has been read or the socket has been
17001689 # closed. either way, reconnect and verify everything is good.
17011690 try :
1702- if await connection .can_read ():
1691+ if await connection .can_read_destructive ():
17031692 raise ConnectionError ("Connection has data" ) from None
17041693 except ConnectionError :
17051694 await connection .disconnect ()
17061695 await connection .connect ()
1707- if await connection .can_read ():
1696+ if await connection .can_read_destructive ():
17081697 raise ConnectionError ("Connection not ready" ) from None
17091698 except BaseException :
17101699 # release the connection back to the pool so that we don't leak it
0 commit comments