From 43d9b98d4801f2ab73d2586ebf8c8bd947fac997 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco <vladimir.webdev@gmail.com> Date: Sun, 6 Dec 2020 10:05:26 +0200 Subject: [PATCH] Fix race --- sentinel.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sentinel.go b/sentinel.go index 0d61d29..0e32986 100644 --- a/sentinel.go +++ b/sentinel.go @@ -177,6 +177,7 @@ func NewFailoverClient(failoverOpt *FailoverOptions) *Client { opt.init() connPool := newConnPool(opt) + failover.mu.Lock() failover.onFailover = func(ctx context.Context, addr string) { _ = connPool.Filter(func(cn *pool.Conn) bool { @@ -648,6 +649,7 @@ func (c *sentinelFailover) discoverSentinels(ctx context.Context) { func (c *sentinelFailover) listen(pubsub *PubSub) { ctx := context.TODO() + if c.onUpdate != nil { c.onUpdate(ctx) } @@ -725,9 +727,12 @@ func NewFailoverClusterClient(failoverOpt *FailoverOptions) *ClusterClient { } c := NewClusterClient(opt) + + failover.mu.Lock() failover.onUpdate = func(ctx context.Context) { c.ReloadState(ctx) } + failover.mu.Unlock() return c }