mirror of
https://github.com/redis/go-redis.git
synced 2025-04-19 07:22:17 +03:00
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
This commit is contained in:
parent
a4aea258fc
commit
e191cf9dbe
31
sentinel.go
31
sentinel.go
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -583,17 +584,12 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
|
|||||||
sentinelCli := NewSentinelClient(c.opt.sentinelOptions(addr))
|
sentinelCli := NewSentinelClient(c.opt.sentinelOptions(addr))
|
||||||
addrVal, err := sentinelCli.GetMasterAddrByName(ctx, c.opt.MasterName).Result()
|
addrVal, err := sentinelCli.GetMasterAddrByName(ctx, c.opt.MasterName).Result()
|
||||||
if err != nil {
|
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",
|
internal.Logger.Printf(ctx, "sentinel: GetMasterAddrByName addr=%s, master=%q failed: %s",
|
||||||
addr, c.opt.MasterName, err)
|
addr, c.opt.MasterName, err)
|
||||||
_ = sentinelCli.Close()
|
_ = sentinelCli.Close()
|
||||||
|
errCh <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
masterAddr = net.JoinHostPort(addrVal[0], addrVal[1])
|
masterAddr = net.JoinHostPort(addrVal[0], addrVal[1])
|
||||||
// Push working sentinel to the top
|
// Push working sentinel to the top
|
||||||
@ -605,21 +601,16 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
|
|||||||
}(i, sentinelAddr)
|
}(i, sentinelAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
done := make(chan struct{})
|
wg.Wait()
|
||||||
go func() {
|
close(errCh)
|
||||||
wg.Wait()
|
if masterAddr != "" {
|
||||||
close(done)
|
return masterAddr, nil
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
if masterAddr != "" {
|
|
||||||
return masterAddr, nil
|
|
||||||
}
|
|
||||||
return "", errors.New("redis: all sentinels specified in configuration are unreachable")
|
|
||||||
case err := <-errCh:
|
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
|
errs := make([]error, 0, len(errCh))
|
||||||
|
for err := range errCh {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
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) {
|
func (c *sentinelFailover) replicaAddrs(ctx context.Context, useDisconnected bool) ([]string, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user