From 0064936c5b77c874f8e1f089c955ec8bef3818c4 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sun, 11 Nov 2018 12:13:00 +0200 Subject: [PATCH] Cleanup --- sentinel.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/sentinel.go b/sentinel.go index f265e9d..0caccd6 100644 --- a/sentinel.go +++ b/sentinel.go @@ -305,16 +305,27 @@ func (c *sentinelFailover) switchMaster(addr string) { func (c *sentinelFailover) setSentinel(sentinel *SentinelClient) { c.discoverSentinels(sentinel) c.sentinel = sentinel - go c.listen(sentinel) + + c.pubsub = sentinel.Subscribe("+switch-master") + go c.listen(c.pubsub) } func (c *sentinelFailover) closeSentinel() error { - if err := c.pubsub.Close(); err != nil { - return err + var firstErr error + + err := c.pubsub.Close() + if err != nil && firstErr == err { + firstErr = err + } + c.pubsub = nil + + err = c.sentinel.Close() + if err != nil && firstErr == err { + firstErr = err } - err := c.sentinel.Close() c.sentinel = nil - return err + + return firstErr } func (c *sentinelFailover) discoverSentinels(sentinel *SentinelClient) { @@ -339,9 +350,8 @@ func (c *sentinelFailover) discoverSentinels(sentinel *SentinelClient) { } } -func (c *sentinelFailover) listen(sentinel *SentinelClient) { - c.pubsub = sentinel.Subscribe("+switch-master") - ch := c.pubsub.Channel() +func (c *sentinelFailover) listen(pubsub *PubSub) { + ch := pubsub.Channel() for { msg, ok := <-ch if !ok {