diff --git a/osscluster.go b/osscluster.go index 17f98d9d..3d8875a1 100644 --- a/osscluster.go +++ b/osscluster.go @@ -62,6 +62,8 @@ type ClusterOptions struct { OnConnect func(ctx context.Context, cn *Conn) error + OnClose func() error + Protocol int Username string Password string @@ -868,7 +870,6 @@ func NewClusterClient(opt *ClusterOptions) *ClusterClient { c.state = newClusterStateHolder(c.loadState) c.cmdsInfoCache = newCmdsInfoCache(c.cmdsInfo) c.cmdable = c.Process - c.initHooks(hooks{ dial: nil, process: c.process, @@ -895,7 +896,11 @@ func (c *ClusterClient) ReloadState(ctx context.Context) { // It is rare to Close a ClusterClient, as the ClusterClient is meant // to be long-lived and shared between many goroutines. func (c *ClusterClient) Close() error { - return c.nodes.Close() + err := c.nodes.Close() + if c.opt.OnClose != nil && err == nil { + err = c.opt.OnClose() + } + return err } // Do create a Cmd from the args and processes the cmd. diff --git a/sentinel.go b/sentinel.go index 188f8849..f242161d 100644 --- a/sentinel.go +++ b/sentinel.go @@ -825,7 +825,7 @@ func NewFailoverClusterClient(failoverOpt *FailoverOptions) *ClusterClient { } return slots, nil } - + opt.OnClose = failover.Close c := NewClusterClient(opt) failover.mu.Lock()