Merge pull request #49 from go-redis/fix/dont-leak-goroutines-when-sentinel-fails

sentinel: don't leak goroutines on sentinel fail. Fixes #48.
This commit is contained in:
Vladimir Mihailenco 2014-11-06 13:30:59 +02:00
commit 9805fc35f8
1 changed files with 14 additions and 12 deletions

View File

@ -157,9 +157,9 @@ func (d *sentinelFailover) MasterAddr() (string, error) {
} }
} }
for i, addr := range d.sentinelAddrs { for i, sentinelAddr := range d.sentinelAddrs {
sentinel := newSentinel(&Options{ sentinel := newSentinel(&Options{
Addr: addr, Addr: sentinelAddr,
DB: d.opt.DB, DB: d.opt.DB,
Password: d.opt.Password, Password: d.opt.Password,
@ -171,18 +171,20 @@ func (d *sentinelFailover) MasterAddr() (string, error) {
PoolSize: d.opt.PoolSize, PoolSize: d.opt.PoolSize,
IdleTimeout: d.opt.IdleTimeout, IdleTimeout: d.opt.IdleTimeout,
}) })
addr, err := sentinel.GetMasterAddrByName(d.masterName).Result() masterAddr, err := sentinel.GetMasterAddrByName(d.masterName).Result()
if err != nil { if err != nil {
log.Printf("redis-sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err) log.Printf("redis-sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err)
} else { sentinel.Close()
// Push working sentinel to the top. continue
d.sentinelAddrs[0], d.sentinelAddrs[i] = d.sentinelAddrs[i], d.sentinelAddrs[0]
d.setSentinel(sentinel)
addr := net.JoinHostPort(addr[0], addr[1])
log.Printf("redis-sentinel: %q addr is %s", d.masterName, addr)
return addr, nil
} }
// Push working sentinel to the top.
d.sentinelAddrs[0], d.sentinelAddrs[i] = d.sentinelAddrs[i], d.sentinelAddrs[0]
d.setSentinel(sentinel)
addr := net.JoinHostPort(masterAddr[0], masterAddr[1])
log.Printf("redis-sentinel: %q addr is %s", d.masterName, addr)
return addr, nil
} }
return "", errors.New("redis: all sentinels are unreachable") return "", errors.New("redis: all sentinels are unreachable")
@ -235,7 +237,7 @@ func (d *sentinelFailover) listen() {
msgIface, err := pubsub.Receive() msgIface, err := pubsub.Receive()
if err != nil { if err != nil {
log.Printf("redis-sentinel: Receive failed: %s", err) log.Printf("redis-sentinel: Receive failed: %s", err)
pubsub = nil pubsub.Close()
return return
} }