Skip to content

Commit 19c65d0

Browse files
committed
Better error handling when fetching the master node from the sentinels
1 parent a4aea25 commit 19c65d0

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

sentinel.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/tls"
66
"errors"
7+
"fmt"
78
"net"
89
"strings"
910
"sync"
@@ -583,17 +584,12 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
583584
sentinelCli := NewSentinelClient(c.opt.sentinelOptions(addr))
584585
addrVal, err := sentinelCli.GetMasterAddrByName(ctx, c.opt.MasterName).Result()
585586
if err != nil {
586-
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
587-
// Report immediately and return
588-
errCh <- err
589-
return
590-
}
591587
internal.Logger.Printf(ctx, "sentinel: GetMasterAddrByName addr=%s, master=%q failed: %s",
592588
addr, c.opt.MasterName, err)
593589
_ = sentinelCli.Close()
590+
errCh <- err
594591
return
595592
}
596-
597593
once.Do(func() {
598594
masterAddr = net.JoinHostPort(addrVal[0], addrVal[1])
599595
// Push working sentinel to the top
@@ -605,21 +601,15 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
605601
}(i, sentinelAddr)
606602
}
607603

608-
done := make(chan struct{})
609-
go func() {
610-
wg.Wait()
611-
close(done)
612-
}()
613-
614-
select {
615-
case <-done:
616-
if masterAddr != "" {
617-
return masterAddr, nil
618-
}
619-
return "", errors.New("redis: all sentinels specified in configuration are unreachable")
620-
case err := <-errCh:
621-
return "", err
604+
wg.Wait()
605+
if masterAddr != "" {
606+
return masterAddr, nil
607+
}
608+
errs := make([]error, len(c.sentinelAddrs))
609+
for err := range errCh {
610+
errs = append(errs, err)
622611
}
612+
return "", fmt.Errorf("redis: all sentinels specified in configuration are unreachable: %w", errs)
623613
}
624614

625615
func (c *sentinelFailover) replicaAddrs(ctx context.Context, useDisconnected bool) ([]string, error) {

0 commit comments

Comments
 (0)