Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
only send readonly to replicas once on new connections
Browse files Browse the repository at this point in the history
  • Loading branch information
kefu-reddit committed Oct 5, 2020
1 parent 73f27ed commit 6f1fb57
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
8 changes: 0 additions & 8 deletions rediscluster/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,6 @@ def _execute_command(self, *args, **kwargs):

redirect_addr = None
asking = False
is_read_replica = False

try_random_node = False
slot = self._determine_slot(*args)
Expand All @@ -605,7 +604,6 @@ def _execute_command(self, *args, **kwargs):
slot,
self.read_from_replicas and (command in self.READ_COMMANDS)
)
is_read_replica = node['server_type'] == 'slave'

connection = self.connection_pool.get_connection_by_node(node)

Expand All @@ -615,12 +613,6 @@ def _execute_command(self, *args, **kwargs):
connection.send_command('ASKING')
self.parse_response(connection, "ASKING", **kwargs)
asking = False
if is_read_replica:
# Ask read replica to accept reads (see https://redis.io/commands/readonly)
# TODO: do we need to handle errors from this response?
connection.send_command('READONLY')
self.parse_response(connection, 'READONLY', **kwargs)
is_read_replica = False

connection.send_command(*args)
return self.parse_response(connection, command, **kwargs)
Expand Down
4 changes: 4 additions & 0 deletions rediscluster/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ def get_connection_by_node(self, node):
connection = self._available_connections.get(node["name"], []).pop()
except IndexError:
connection = self.make_connection(node)
if node["server_type"] == "slave":
connection.send_command('READONLY')
if nativestr(connection.read_response()) != 'OK':
raise ConnectionError('READONLY command failed')

self._in_use_connections.setdefault(node["name"], set()).add(connection)

Expand Down

0 comments on commit 6f1fb57

Please sign in to comment.