Skip to content

Commit e191cf9

Browse files
authored
fix: better error handling when fetching the master node from the sentinels (#3349)
* Better error handling when fetching the master node from the sentinels * fix error message generation * close the errCh to not block * use len over errCh
1 parent a4aea25 commit e191cf9

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

sentinel.go

+11-20
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,16 @@ 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+
close(errCh)
606+
if masterAddr != "" {
607+
return masterAddr, nil
608+
}
609+
errs := make([]error, 0, len(errCh))
610+
for err := range errCh {
611+
errs = append(errs, err)
622612
}
613+
return "", fmt.Errorf("redis: all sentinels specified in configuration are unreachable: %w", errors.Join(errs...))
623614
}
624615

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

0 commit comments

Comments
 (0)