pomerium/internal/zero/healthcheck/scheduler.go
Denis Mishin 0503b41108
zero/connect: add re-run health checks command (#5219)
* zero/connect: add run health checks and shutdown commands

* fix proto

* trigger re-run on command

* add handler

* rename runPeriodicHealthChecksLeased
2024-08-22 16:17:53 -04:00

54 lines
849 B
Go

package healthcheck
import (
"context"
"time"
"github.com/cenkalti/backoff/v4"
)
const (
runHealthChecksMaxInterval = time.Minute * 30
runHealthCheckMinInterval = time.Minute
)
func (c *Checker) Scheduler(ctx context.Context) {
bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = 0
bo.MaxInterval = runHealthChecksMaxInterval
bo.InitialInterval = runHealthCheckMinInterval
bo.Reset()
tm := time.NewTimer(runHealthCheckMinInterval)
defer tm.Stop()
select {
case <-ctx.Done():
return
case <-tm.C:
}
for {
select {
case <-ctx.Done():
return
case <-c.forceCheck:
case <-tm.C:
}
next := runHealthChecksMaxInterval
err := c.CheckRoutes(ctx)
if err != nil {
next = bo.NextBackOff()
} else {
bo.Reset()
}
if !tm.Stop() {
select {
case <-tm.C:
default:
}
}
tm.Reset(next)
}
}