From 6f79953aa52c08581a5ce5a9a0dafa89d79ba390 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Thu, 6 Nov 2014 12:55:09 +0200 Subject: [PATCH] sentinel: don't leak goroutines on sentinel fail. Fixes #48. --- sentinel.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/sentinel.go b/sentinel.go index 262ac44..d3ffeca 100644 --- a/sentinel.go +++ b/sentinel.go @@ -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{ - Addr: addr, + Addr: sentinelAddr, DB: d.opt.DB, Password: d.opt.Password, @@ -171,18 +171,20 @@ func (d *sentinelFailover) MasterAddr() (string, error) { PoolSize: d.opt.PoolSize, IdleTimeout: d.opt.IdleTimeout, }) - addr, err := sentinel.GetMasterAddrByName(d.masterName).Result() + masterAddr, err := sentinel.GetMasterAddrByName(d.masterName).Result() if err != nil { log.Printf("redis-sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err) - } else { - // Push working sentinel to the top. - 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 + sentinel.Close() + continue } + + // 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") @@ -235,7 +237,7 @@ func (d *sentinelFailover) listen() { msgIface, err := pubsub.Receive() if err != nil { log.Printf("redis-sentinel: Receive failed: %s", err) - pubsub = nil + pubsub.Close() return }