diff --git a/rediscluster/client.py b/rediscluster/client.py index cb8f59de..5571c49d 100644 --- a/rediscluster/client.py +++ b/rediscluster/client.py @@ -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) @@ -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) @@ -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) diff --git a/rediscluster/connection.py b/rediscluster/connection.py index 561e7657..1b998598 100644 --- a/rediscluster/connection.py +++ b/rediscluster/connection.py @@ -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)