@@ -1646,7 +1646,7 @@ class ClusterPubSub(PubSub):
1646
1646
https://redis-py-cluster.readthedocs.io/en/stable/pubsub.html
1647
1647
"""
1648
1648
1649
- def __init__ (self , redis_cluster , node = None , host = None , port = None , ** kwargs ):
1649
+ def __init__ (self , redis_cluster , node = None , host = None , port = None , replica = False , ** kwargs ):
1650
1650
"""
1651
1651
When a pubsub instance is created without specifying a node, a single
1652
1652
node will be transparently chosen for the pubsub connection on the
@@ -1661,6 +1661,7 @@ def __init__(self, redis_cluster, node=None, host=None, port=None, **kwargs):
1661
1661
:type port: int
1662
1662
"""
1663
1663
self .node = None
1664
+ self .replica = replica
1664
1665
self .set_pubsub_node (redis_cluster , node , host , port )
1665
1666
connection_pool = (
1666
1667
None
@@ -1794,7 +1795,7 @@ def get_sharded_message(
1794
1795
if message ["channel" ] in self .pending_unsubscribe_shard_channels :
1795
1796
self .pending_unsubscribe_shard_channels .remove (message ["channel" ])
1796
1797
self .shard_channels .pop (message ["channel" ], None )
1797
- node = self .cluster .get_node_from_key (message ["channel" ])
1798
+ node = self .cluster .get_node_from_key (message ["channel" ], self . replica )
1798
1799
if self .node_pubsub_mapping [node .name ].subscribed is False :
1799
1800
self .node_pubsub_mapping .pop (node .name )
1800
1801
if not self .channels and not self .patterns and not self .shard_channels :
@@ -1811,7 +1812,7 @@ def ssubscribe(self, *args, **kwargs):
1811
1812
s_channels = dict .fromkeys (args )
1812
1813
s_channels .update (kwargs )
1813
1814
for s_channel , handler in s_channels .items ():
1814
- node = self .cluster .get_node_from_key (s_channel )
1815
+ node = self .cluster .get_node_from_key (s_channel , self . replica )
1815
1816
pubsub = self ._get_node_pubsub (node )
1816
1817
if handler :
1817
1818
pubsub .ssubscribe (** {s_channel : handler })
@@ -1832,7 +1833,7 @@ def sunsubscribe(self, *args):
1832
1833
args = self .shard_channels
1833
1834
1834
1835
for s_channel in args :
1835
- node = self .cluster .get_node_from_key (s_channel )
1836
+ node = self .cluster .get_node_from_key (s_channel , self . replica )
1836
1837
p = self ._get_node_pubsub (node )
1837
1838
p .sunsubscribe (s_channel )
1838
1839
self .pending_unsubscribe_shard_channels .update (
0 commit comments