From 19c65d0aee0aec6617277077506df069c908508a Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Thu, 17 Apr 2025 14:18:41 +0300 Subject: [PATCH 1/4] Better error handling when fetching the master node from the sentinels --- sentinel.go | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/sentinel.go b/sentinel.go index 0638663536..9498b51319 100644 --- a/sentinel.go +++ b/sentinel.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "errors" + "fmt" "net" "strings" "sync" @@ -583,17 +584,12 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) { sentinelCli := NewSentinelClient(c.opt.sentinelOptions(addr)) addrVal, err := sentinelCli.GetMasterAddrByName(ctx, c.opt.MasterName).Result() if err != nil { - if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { - // Report immediately and return - errCh <- err - return - } internal.Logger.Printf(ctx, "sentinel: GetMasterAddrByName addr=%s, master=%q failed: %s", addr, c.opt.MasterName, err) _ = sentinelCli.Close() + errCh <- err return } - once.Do(func() { masterAddr = net.JoinHostPort(addrVal[0], addrVal[1]) // Push working sentinel to the top @@ -605,21 +601,15 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) { }(i, sentinelAddr) } - done := make(chan struct{}) - go func() { - wg.Wait() - close(done) - }() - - select { - case <-done: - if masterAddr != "" { - return masterAddr, nil - } - return "", errors.New("redis: all sentinels specified in configuration are unreachable") - case err := <-errCh: - return "", err + wg.Wait() + if masterAddr != "" { + return masterAddr, nil + } + errs := make([]error, len(c.sentinelAddrs)) + for err := range errCh { + errs = append(errs, err) } + return "", fmt.Errorf("redis: all sentinels specified in configuration are unreachable: %w", errs) } func (c *sentinelFailover) replicaAddrs(ctx context.Context, useDisconnected bool) ([]string, error) { From 260a56cc457993acb1336bcad10af745b1149bcf Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Thu, 17 Apr 2025 14:22:34 +0300 Subject: [PATCH 2/4] fix error message generation --- sentinel.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sentinel.go b/sentinel.go index 9498b51319..8435586820 100644 --- a/sentinel.go +++ b/sentinel.go @@ -605,11 +605,11 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) { if masterAddr != "" { return masterAddr, nil } - errs := make([]error, len(c.sentinelAddrs)) + errs := make([]error, 0, len(c.sentinelAddrs)) for err := range errCh { errs = append(errs, err) } - return "", fmt.Errorf("redis: all sentinels specified in configuration are unreachable: %w", errs) + return "", fmt.Errorf("redis: all sentinels specified in configuration are unreachable: %w", errors.Join(errs...)) } func (c *sentinelFailover) replicaAddrs(ctx context.Context, useDisconnected bool) ([]string, error) { From 9402e9028cc422069d8a1bba066377e38b80a1b2 Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Thu, 17 Apr 2025 14:25:11 +0300 Subject: [PATCH 3/4] close the errCh to not block --- sentinel.go | 1 + 1 file changed, 1 insertion(+) diff --git a/sentinel.go b/sentinel.go index 8435586820..d5a26ac693 100644 --- a/sentinel.go +++ b/sentinel.go @@ -602,6 +602,7 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) { } wg.Wait() + close(errCh) if masterAddr != "" { return masterAddr, nil } From c9f301e45202d8092b081638b8a287cd82b587ad Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Thu, 17 Apr 2025 15:58:58 +0300 Subject: [PATCH 4/4] use len over errCh --- sentinel.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentinel.go b/sentinel.go index d5a26ac693..f5b9a52d10 100644 --- a/sentinel.go +++ b/sentinel.go @@ -606,7 +606,7 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) { if masterAddr != "" { return masterAddr, nil } - errs := make([]error, 0, len(c.sentinelAddrs)) + errs := make([]error, 0, len(errCh)) for err := range errCh { errs = append(errs, err) }