mirror of https://github.com/go-redis/redis.git
Compare commits
2 Commits
af51a55c3f
...
9e095f2bf3
Author | SHA1 | Date |
---|---|---|
kwen | 9e095f2bf3 | |
kwen | 7e63d6519a |
52
sentinel.go
52
sentinel.go
|
@ -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 {
|
for i, sentinelAddr := range c.sentinelAddrs {
|
||||||
sentinel := NewSentinelClient(c.opt.sentinelOptions(sentinelAddr))
|
wg.Add(1)
|
||||||
|
go func(i int, addr string) {
|
||||||
masterAddr, err := sentinel.GetMasterAddrByName(ctx, c.opt.MasterName).Result()
|
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 {
|
if err != nil {
|
||||||
_ = sentinel.Close()
|
internal.Logger.Printf(ctx, "sentinel: GetMasterAddrByName addr=%s, master=%q failed: %s",
|
||||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
addr, c.opt.MasterName, err)
|
||||||
return "", err
|
_ = sentinelCli.Close()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
internal.Logger.Printf(ctx, "sentinel: GetMasterAddrByName master=%q failed: %s",
|
internal.Logger.Printf(ctx, "get addr %s master res %v", addr, masterAddr)
|
||||||
c.opt.MasterName, err)
|
once.Do(func() {
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Push working sentinel to the top.
|
// 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.sentinelAddrs[0], c.sentinelAddrs[i] = c.sentinelAddrs[i], c.sentinelAddrs[0]
|
||||||
c.setSentinel(ctx, sentinel)
|
c.setSentinel(ctx, sentinelCli)
|
||||||
|
close(done)
|
||||||
|
|
||||||
addr := net.JoinHostPort(masterAddr[0], masterAddr[1])
|
})
|
||||||
return addr, nil
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}(i, sentinelAddr)
|
||||||
|
|
||||||
|
}
|
||||||
|
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")
|
return "", errors.New("redis: all sentinels specified in configuration are unreachable")
|
||||||
|
|
Loading…
Reference in New Issue