Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: fix Query sentinel changed to concurrent query to avoid context … #3174

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,26 +549,50 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
}
}

var masterAddr string
var wg sync.WaitGroup
var once sync.Once
done := make(chan struct{})

for i, sentinelAddr := range c.sentinelAddrs {
sentinel := NewSentinelClient(c.opt.sentinelOptions(sentinelAddr))
wg.Add(1)
go func(i int, addr string) {
defer wg.Done()
select {
case <-done:
default:
sentinelCli := NewSentinelClient(c.opt.sentinelOptions(addr))
addrVal, err := sentinelCli.GetMasterAddrByName(ctx, c.opt.MasterName).Result()
if err != nil {
internal.Logger.Printf(ctx, "sentinel: GetMasterAddrByName addr=%s, master=%q failed: %s",
addr, c.opt.MasterName, err)
_ = sentinelCli.Close()
return
}
internal.Logger.Printf(ctx, "get addr %s master res %v", addr, masterAddr)
once.Do(func() {
// Push working sentinel to the top.
masterAddr = net.JoinHostPort(addrVal[0], addrVal[1])
c.sentinelAddrs[0], c.sentinelAddrs[i] = c.sentinelAddrs[i], c.sentinelAddrs[0]
c.setSentinel(ctx, sentinelCli)
close(done)

})

masterAddr, err := sentinel.GetMasterAddrByName(ctx, c.opt.MasterName).Result()
if err != nil {
_ = sentinel.Close()
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return "", err
}
internal.Logger.Printf(ctx, "sentinel: GetMasterAddrByName master=%q failed: %s",
c.opt.MasterName, err)
continue
}

// Push working sentinel to the top.
c.sentinelAddrs[0], c.sentinelAddrs[i] = c.sentinelAddrs[i], c.sentinelAddrs[0]
c.setSentinel(ctx, sentinel)
}(i, sentinelAddr)

addr := net.JoinHostPort(masterAddr[0], masterAddr[1])
return addr, nil
}
go func() {
wg.Wait()
once.Do(func() {
close(done)
})
}()
<-done
if masterAddr != "" {
return masterAddr, nil
}

return "", errors.New("redis: all sentinels specified in configuration are unreachable")
Expand Down
Loading