From 01958102114453c0d9dbb3cd8888ceaff3c58636 Mon Sep 17 00:00:00 2001 From: fukua95 Date: Sat, 7 Jun 2025 18:33:26 +0800 Subject: [PATCH] fix: check if the shard exists to avoid returning nil Signed-off-by: fukua95 --- ring.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ring.go b/ring.go index 8a004b8c0..4da0b21a6 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) {