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

Commit 2a4c77d

Browse files
Fran GarciaGrokzen
authored andcommitted
Support reading from replicas on pipeline executions
1 parent 3b68c18 commit 2a4c77d

File tree

2 files changed

+47
-47
lines changed

2 files changed

+47
-47
lines changed

rediscluster/client.py

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,50 @@
5353
TimeoutError,
5454
)
5555

56+
# Not complete, but covers the major ones
57+
# https://redis.io/commands
58+
READ_COMMANDS = frozenset([
59+
"BITCOUNT",
60+
"BITPOS",
61+
"EXISTS",
62+
"GEODIST",
63+
"GEOHASH",
64+
"GEOPOS",
65+
"GEORADIUS",
66+
"GEORADIUSBYMEMBER",
67+
"GET",
68+
"GETBIT",
69+
"GETRANGE",
70+
"HEXISTS",
71+
"HGET",
72+
"HGETALL",
73+
"HKEYS",
74+
"HLEN",
75+
"HMGET",
76+
"HSTRLEN",
77+
"HVALS",
78+
"KEYS",
79+
"LINDEX",
80+
"LLEN",
81+
"LRANGE",
82+
"MGET",
83+
"PTTL",
84+
"RANDOMKEY",
85+
"SCARD",
86+
"SDIFF",
87+
"SINTER",
88+
"SISMEMBER",
89+
"SMEMBERS",
90+
"SRANDMEMBER",
91+
"STRLEN",
92+
"SUNION",
93+
"TTL",
94+
"ZCARD",
95+
"ZCOUNT",
96+
"ZRANGE",
97+
"ZSCORE",
98+
])
99+
56100

57101
log = logging.getLogger(__name__)
58102

@@ -168,50 +212,6 @@ class RedisCluster(Redis):
168212
], 'slot-id'),
169213
)
170214

171-
# Not complete, but covers the major ones
172-
# https://redis.io/commands
173-
READ_COMMANDS = [
174-
"BITCOUNT",
175-
"BITPOS",
176-
"EXISTS",
177-
"GEODIST",
178-
"GEOHASH",
179-
"GEOPOS",
180-
"GEORADIUS",
181-
"GEORADIUSBYMEMBER",
182-
"GET",
183-
"GETBIT",
184-
"GETRANGE",
185-
"HEXISTS",
186-
"HGET",
187-
"HGETALL",
188-
"HKEYS",
189-
"HLEN",
190-
"HMGET",
191-
"HSTRLEN",
192-
"HVALS",
193-
"KEYS",
194-
"LINDEX",
195-
"LLEN",
196-
"LRANGE",
197-
"MGET",
198-
"PTTL",
199-
"RANDOMKEY",
200-
"SCARD",
201-
"SDIFF",
202-
"SINTER",
203-
"SISMEMBER",
204-
"SMEMBERS",
205-
"SRANDMEMBER",
206-
"STRLEN",
207-
"SUNION",
208-
"TTL",
209-
"ZCARD",
210-
"ZCOUNT",
211-
"ZRANGE",
212-
"ZSCORE",
213-
]
214-
215215
RESULT_CALLBACKS = dict_merge(
216216
string_keys_to_dict([
217217
"BGREWRITEAOF",
@@ -608,7 +608,7 @@ def _execute_command(self, *args, **kwargs):
608608
else:
609609
node = self.connection_pool.get_node_by_slot(
610610
slot,
611-
self.read_from_replicas and (command in self.READ_COMMANDS)
611+
self.read_from_replicas and (command in READ_COMMANDS)
612612
)
613613
is_read_replica = node['server_type'] == 'slave'
614614

rediscluster/pipeline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55

66
# rediscluster imports
7-
from .client import RedisCluster
7+
from .client import RedisCluster, READ_COMMANDS
88
from .exceptions import (
99
RedisClusterException, AskError, MovedError, TryAgainError, ClusterDownError,
1010
)
@@ -194,7 +194,7 @@ def _send_cluster_commands(self, stack, raise_on_error=True, allow_redirections=
194194
# refer to our internal node -> slot table that tells us where a given
195195
# command should route to.
196196
slot = self._determine_slot(*c.args)
197-
node = self.connection_pool.get_node_by_slot(slot)
197+
node = self.connection_pool.get_node_by_slot(slot, self.read_from_replicas and c.args[0] in READ_COMMANDS)
198198

199199
# little hack to make sure the node name is populated. probably could clean this up.
200200
self.connection_pool.nodes.set_node_name(node)

0 commit comments

Comments
 (0)