@@ -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 ()
@@ -924,12 +911,10 @@ async def send_command(self, *args: Any, **kwargs: Any) -> None:
924
911
self .pack_command (* args ), check_health = kwargs .get ("check_health" , True )
925
912
)
926
913
927
- async def can_read (self , timeout : float = 0 ):
914
+ async def can_read_destructive (self ):
928
915
"""Poll the socket to see if there's data that can be read."""
929
- if not self .is_connected :
930
- await self .connect ()
931
916
try :
932
- return await self ._parser .can_read ( timeout )
917
+ return await self ._parser .can_read_destructive ( )
933
918
except OSError as e :
934
919
await self .disconnect ()
935
920
raise ConnectionError (
@@ -1497,12 +1482,12 @@ async def get_connection(self, command_name, *keys, **options):
1497
1482
# pool before all data has been read or the socket has been
1498
1483
# closed. either way, reconnect and verify everything is good.
1499
1484
try :
1500
- if await connection .can_read ():
1485
+ if await connection .can_read_destructive ():
1501
1486
raise ConnectionError ("Connection has data" ) from None
1502
1487
except ConnectionError :
1503
1488
await connection .disconnect ()
1504
1489
await connection .connect ()
1505
- if await connection .can_read ():
1490
+ if await connection .can_read_destructive ():
1506
1491
raise ConnectionError ("Connection not ready" ) from None
1507
1492
except BaseException :
1508
1493
# release the connection back to the pool so that we don't
@@ -1698,12 +1683,12 @@ async def get_connection(self, command_name, *keys, **options):
1698
1683
# pool before all data has been read or the socket has been
1699
1684
# closed. either way, reconnect and verify everything is good.
1700
1685
try :
1701
- if await connection .can_read ():
1686
+ if await connection .can_read_destructive ():
1702
1687
raise ConnectionError ("Connection has data" ) from None
1703
1688
except ConnectionError :
1704
1689
await connection .disconnect ()
1705
1690
await connection .connect ()
1706
- if await connection .can_read ():
1691
+ if await connection .can_read_destructive ():
1707
1692
raise ConnectionError ("Connection not ready" ) from None
1708
1693
except BaseException :
1709
1694
# release the connection back to the pool so that we don't leak it
0 commit comments