package pool import ( "context" "time" ) type healthCheck struct { cancel context.CancelFunc closedCh chan struct{} clientRebalanceInterval time.Duration } func newHealthCheck(clientRebalanceInterval time.Duration) *healthCheck { var h healthCheck h.clientRebalanceInterval = clientRebalanceInterval h.closedCh = make(chan struct{}) return &h } // startRebalance runs loop to monitor connection healthy status. func (h *healthCheck) startRebalance(ctx context.Context, callback func(ctx context.Context)) { ctx, cancel := context.WithCancel(ctx) h.cancel = cancel go func() { ticker := time.NewTicker(h.clientRebalanceInterval) defer ticker.Stop() for { select { case <-ctx.Done(): close(h.closedCh) return case <-ticker.C: callback(ctx) ticker.Reset(h.clientRebalanceInterval) } } }() } func (h *healthCheck) stopRebalance() { h.cancel() <-h.closedCh }