mirror of
https://github.com/redis/go-redis.git
synced 2025-07-28 06:42:00 +03:00
feat(ring): add GetShardClients and GetShardClientForKey methods to Ring for shard access (#3388)
* feat: expose shard information in redis.Ring - Add GetShards() method to retrieve a list of active shard clients. - Add GetShardByKey(key string) method to get the shard client for a specific key. - These methods enable users to manage Pub/Sub operations more effectively by accessing shard-specific clients. * rename GetShardClients and GetShardClientForKey --------- Co-authored-by: DengY11 <212294929@qq.com> Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com>
This commit is contained in:
23
ring.go
23
ring.go
@ -847,3 +847,26 @@ func (c *Ring) Close() error {
|
||||
|
||||
return c.sharding.Close()
|
||||
}
|
||||
|
||||
// GetShardClients returns a list of all shard clients in the ring.
|
||||
// This can be used to create dedicated connections (e.g., PubSub) for each shard.
|
||||
func (c *Ring) GetShardClients() []*Client {
|
||||
shards := c.sharding.List()
|
||||
clients := make([]*Client, 0, len(shards))
|
||||
for _, shard := range shards {
|
||||
if shard.IsUp() {
|
||||
clients = append(clients, shard.Client)
|
||||
}
|
||||
}
|
||||
return clients
|
||||
}
|
||||
|
||||
// GetShardClientForKey returns the shard client that would handle the given key.
|
||||
// This can be used to determine which shard a particular key/channel would be routed to.
|
||||
func (c *Ring) GetShardClientForKey(key string) (*Client, error) {
|
||||
shard, err := c.sharding.GetByKey(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return shard.Client, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user