Skip to content

Commit 66008ba

Browse files
committed
implement flush-responses approach
1 parent 78f9f2f commit 66008ba

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

redis/connection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ def __init__(
559559
self.set_parser(parser_class)
560560
self._connect_callbacks = []
561561
self._buffer_cutoff = 6000
562+
self._expected_responses = 0
562563

563564
def __repr__(self):
564565
repr_args = ",".join([f"{k}={v}" for k, v in self.repr_pieces()])
@@ -739,6 +740,7 @@ def disconnect(self, *args):
739740
except OSError:
740741
pass
741742
self._sock = None
743+
self._expected_responses = 0
742744

743745
def _send_ping(self):
744746
"""Send PING, expect PONG in return"""
@@ -762,11 +764,17 @@ def send_packed_command(self, command, check_health=True):
762764
# guard against health check recursion
763765
if check_health:
764766
self.check_health()
767+
768+
# flush un-retrieved responses to any previous commands
769+
while self._expected_responses:
770+
_ = self.read_response(disable_decoding=True)
771+
765772
try:
766773
if isinstance(command, str):
767774
command = [command]
768775
for item in command:
769776
self._sock.sendall(item)
777+
self._expected_responses += 1
770778
except socket.timeout:
771779
self.disconnect()
772780
raise TimeoutError("Timeout writing to socket")
@@ -820,6 +828,8 @@ def read_response(self, disable_decoding=False):
820828
self.disconnect()
821829
raise
822830

831+
self._expected_responses -= 1
832+
823833
if self.health_check_interval:
824834
self.next_health_check = time() + self.health_check_interval
825835

0 commit comments

Comments
 (0)