@@ -208,7 +208,7 @@ def on_disconnect(self):
208
208
def on_connect (self , connection : "Connection" ):
209
209
raise NotImplementedError ()
210
210
211
- async def can_read (self , timeout : float ) -> bool :
211
+ async def can_read_destructive (self ) -> bool :
212
212
raise NotImplementedError ()
213
213
214
214
async def read_response (
@@ -286,9 +286,9 @@ async def _read_from_socket(
286
286
return False
287
287
raise ConnectionError (f"Error while reading from socket: { ex .args } " )
288
288
289
- async def can_read (self , timeout : float ) -> bool :
289
+ async def can_read_destructive (self ) -> bool :
290
290
return bool (self .length ) or await self ._read_from_socket (
291
- timeout = timeout , raise_on_timeout = False
291
+ timeout = 0 , raise_on_timeout = False
292
292
)
293
293
294
294
async def read (self , length : int ) -> bytes :
@@ -386,8 +386,8 @@ def on_disconnect(self):
386
386
self ._buffer = None
387
387
self .encoder = None
388
388
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 ( ))
391
391
392
392
async def read_response (
393
393
self , disable_decoding : bool = False
@@ -444,9 +444,7 @@ async def read_response(
444
444
class HiredisParser (BaseParser ):
445
445
"""Parser class for connections using Hiredis"""
446
446
447
- __slots__ = BaseParser .__slots__ + ("_next_response" , "_reader" , "_socket_timeout" )
448
-
449
- _next_response : bool
447
+ __slots__ = BaseParser .__slots__ + ("_reader" , "_socket_timeout" )
450
448
451
449
def __init__ (self , socket_read_size : int ):
452
450
if not HIREDIS_AVAILABLE :
@@ -466,23 +464,18 @@ def on_connect(self, connection: "Connection"):
466
464
kwargs ["errors" ] = connection .encoder .encoding_errors
467
465
468
466
self ._reader = hiredis .Reader (** kwargs )
469
- self ._next_response = False
470
467
self ._socket_timeout = connection .socket_timeout
471
468
472
469
def on_disconnect (self ):
473
470
self ._stream = None
474
471
self ._reader = None
475
- self ._next_response = False
476
472
477
- async def can_read (self , timeout : float ):
473
+ async def can_read_destructive (self ):
478
474
if not self ._stream or not self ._reader :
479
475
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 )
486
479
487
480
async def read_from_socket (
488
481
self ,
@@ -523,12 +516,6 @@ async def read_response(
523
516
self .on_disconnect ()
524
517
raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR ) from None
525
518
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
-
532
519
response = self ._reader .gets ()
533
520
while response is False :
534
521
await self .read_from_socket ()
@@ -925,12 +912,10 @@ async def send_command(self, *args: Any, **kwargs: Any) -> None:
925
912
self .pack_command (* args ), check_health = kwargs .get ("check_health" , True )
926
913
)
927
914
928
- async def can_read (self , timeout : float = 0 ):
915
+ async def can_read_destructive (self ):
929
916
"""Poll the socket to see if there's data that can be read."""
930
- if not self .is_connected :
931
- await self .connect ()
932
917
try :
933
- return await self ._parser .can_read ( timeout )
918
+ return await self ._parser .can_read_destructive ( )
934
919
except OSError as e :
935
920
await self .disconnect (nowait = True )
936
921
raise ConnectionError (
@@ -957,6 +942,10 @@ async def read_response(self, disable_decoding: bool = False):
957
942
raise ConnectionError (
958
943
f"Error while reading from { self .host } :{ self .port } : { e .args } "
959
944
)
945
+ except asyncio .CancelledError :
946
+ # need this check for 3.7, where CancelledError
947
+ # is subclass of Exception, not BaseException
948
+ raise
960
949
except Exception :
961
950
await self .disconnect (nowait = True )
962
951
raise
@@ -1498,12 +1487,12 @@ async def get_connection(self, command_name, *keys, **options):
1498
1487
# pool before all data has been read or the socket has been
1499
1488
# closed. either way, reconnect and verify everything is good.
1500
1489
try :
1501
- if await connection .can_read ():
1490
+ if await connection .can_read_destructive ():
1502
1491
raise ConnectionError ("Connection has data" ) from None
1503
1492
except ConnectionError :
1504
1493
await connection .disconnect ()
1505
1494
await connection .connect ()
1506
- if await connection .can_read ():
1495
+ if await connection .can_read_destructive ():
1507
1496
raise ConnectionError ("Connection not ready" ) from None
1508
1497
except BaseException :
1509
1498
# 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):
1699
1688
# pool before all data has been read or the socket has been
1700
1689
# closed. either way, reconnect and verify everything is good.
1701
1690
try :
1702
- if await connection .can_read ():
1691
+ if await connection .can_read_destructive ():
1703
1692
raise ConnectionError ("Connection has data" ) from None
1704
1693
except ConnectionError :
1705
1694
await connection .disconnect ()
1706
1695
await connection .connect ()
1707
- if await connection .can_read ():
1696
+ if await connection .can_read_destructive ():
1708
1697
raise ConnectionError ("Connection not ready" ) from None
1709
1698
except BaseException :
1710
1699
# release the connection back to the pool so that we don't leak it
0 commit comments