From 0f40ae3ff2ef4df6d1fbac433e63362ee45683c8 Mon Sep 17 00:00:00 2001 From: fukua95 Date: Mon, 9 Jun 2025 16:06:21 +0800 Subject: [PATCH] fix: check if the shard exists to avoid returning nil (#3396) Signed-off-by: fukua95 --- ring.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ring.go b/ring.go index 8a004b8c..4da0b21a 100644 --- a/ring.go +++ b/ring.go @@ -405,7 +405,12 @@ func (c *ringSharding) GetByName(shardName string) (*ringShard, error) { c.mu.RLock() defer c.mu.RUnlock() - return c.shards.m[shardName], nil + shard, ok := c.shards.m[shardName] + if !ok { + return nil, errors.New("redis: the shard is not in the ring") + } + + return shard, nil } func (c *ringSharding) Random() (*ringShard, error) {