Skip to content

choose a node randomly when subscribing #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 4.4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import threading
import time
import warnings
from collections import defaultdict
from itertools import chain
from typing import Optional

Expand All @@ -14,6 +15,7 @@
list_or_args,
)
from redis.connection import ConnectionPool, SSLConnection, UnixDomainSocketConnection
from redis.crc import key_slot
from redis.credentials import CredentialProvider
from redis.exceptions import (
ConnectionError,
Expand Down Expand Up @@ -1447,11 +1449,14 @@ def on_connect(self, connection):
}
self.psubscribe(**patterns)
if self.shard_channels:
shard_channels = {
self.encoder.decode(k, force=True): v
for k, v in self.shard_channels.items()
}
self.ssubscribe(**shard_channels)
channels_by_slot = defaultdict(dict)
for k, v in self.shard_channels.items():
key = self.encoder.decode(k, force=True)
slot = key_slot(self.encoder.encode(key))
channels_by_slot[slot][key] = v

for slot, channels in channels_by_slot.items():
self.ssubscribe(**channels)

@property
def subscribed(self):
Expand Down Expand Up @@ -1672,8 +1677,8 @@ def ssubscribe(self, *args, target_node=None, **kwargs):
args = list_or_args(args[0], args[1:])
new_s_channels = dict.fromkeys(args)
new_s_channels.update(kwargs)
for channel in new_s_channels: # We should send ssubscribe one by one on redis cluster to prevent CROSSSLOT error
self.execute_command("SSUBSCRIBE", channel)
ret_val = self.execute_command("SSUBSCRIBE", *new_s_channels.keys())

# update the s_channels dict AFTER we send the command. we don't want to
# subscribe twice to these channels, once for the command and again
# for the reconnection.
Expand All @@ -1685,6 +1690,7 @@ def ssubscribe(self, *args, target_node=None, **kwargs):
# Clear the health check counter
self.health_check_response_counter = 0
self.pending_unsubscribe_shard_channels.difference_update(new_s_channels)
return ret_val

def sunsubscribe(self, *args, target_node=None):
"""
Expand Down