mirror of
https://github.com/hanzokv/go.git
synced 2026-09-24 00:31:51 +00:00
fix(sentinel): close non-winning sentinel clients in MasterAddr concurrent probe (#3827)
In MasterAddr, multiple goroutines probe sentinels concurrently. The first to succeed enters sync.Once and saves its SentinelClient via setSentinel. Other goroutines that also succeeded but lost the once.Do race doesn't closed its SentinelClient — a resource leak. After once.Do returns, close the sentinelCli if it's not the winner. sync.Once guarantees all goroutines see c.sentinel written by the winner.
This commit is contained in:
+7
-2
@@ -954,6 +954,7 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
|
||||
errCh <- err
|
||||
return
|
||||
}
|
||||
|
||||
once.Do(func() {
|
||||
masterAddr = net.JoinHostPort(addrVal[0], addrVal[1])
|
||||
// Push working sentinel to the top
|
||||
@@ -962,6 +963,10 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
|
||||
internal.Logger.Printf(ctx, "sentinel: selected addr=%s masterAddr=%s", addr, masterAddr)
|
||||
cancel()
|
||||
})
|
||||
|
||||
if sentinelCli != c.sentinel {
|
||||
_ = sentinelCli.Close()
|
||||
}
|
||||
}(i, sentinelAddr)
|
||||
}
|
||||
|
||||
@@ -1045,9 +1050,9 @@ func (c *sentinelFailover) replicaAddrs(ctx context.Context, useDisconnected boo
|
||||
}
|
||||
|
||||
if sentinelReachable {
|
||||
return []string{}, nil
|
||||
return nil, nil
|
||||
}
|
||||
return []string{}, errors.New("redis: all sentinels specified in configuration are unreachable")
|
||||
return nil, errors.New("redis: all sentinels specified in configuration are unreachable")
|
||||
}
|
||||
|
||||
func (c *sentinelFailover) getMasterAddr(ctx context.Context, sentinel *SentinelClient) (string, error) {
|
||||
|
||||
Reference in New Issue
Block a user