frostfs-sdk-go/pool/healthcheck.go
Alexander Chuprov d77a8742bc [#300] pool: Move 'healthCheck' to separate file
Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
2025-03-07 11:45:31 +00:00

47 lines
915 B
Go

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
}