diff --git a/internal/healthcheck/health.go b/internal/healthcheck/health.go index b0fb9295..179929f3 100644 --- a/internal/healthcheck/health.go +++ b/internal/healthcheck/health.go @@ -12,7 +12,7 @@ import ( func (s *server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) { defer close(done) - s.openvpn.healthyTimer = time.NewTimer(s.openvpn.currentHealthyWait) + s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWait) for { previousErr := s.handler.getErr() @@ -23,10 +23,11 @@ func (s *server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) { if previousErr != nil && err == nil { s.logger.Info("healthy!") s.openvpn.healthyTimer.Stop() - s.openvpn.currentHealthyWait = s.openvpn.healthyWaitConfig.Initial + s.openvpn.healthyWait = s.config.OpenVPN.Initial } else if previousErr == nil && err != nil { s.logger.Info("unhealthy: " + err.Error()) - s.openvpn.healthyTimer = time.NewTimer(s.openvpn.currentHealthyWait) + s.openvpn.healthyTimer.Stop() + s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWait) } if err != nil { // try again after 1 second diff --git a/internal/healthcheck/openvpn.go b/internal/healthcheck/openvpn.go index dcf6a818..b74c3918 100644 --- a/internal/healthcheck/openvpn.go +++ b/internal/healthcheck/openvpn.go @@ -9,9 +9,9 @@ import ( func (s *server) onUnhealthyOpenvpn(ctx context.Context) { s.logger.Info("program has been unhealthy for " + - s.openvpn.currentHealthyWait.String() + ": restarting OpenVPN") + s.openvpn.healthyWait.String() + ": restarting OpenVPN") _, _ = s.openvpn.looper.ApplyStatus(ctx, constants.Stopped) _, _ = s.openvpn.looper.ApplyStatus(ctx, constants.Running) - s.openvpn.currentHealthyWait += s.openvpn.healthyWaitConfig.Addition - s.openvpn.healthyTimer = time.NewTimer(s.openvpn.currentHealthyWait) + s.openvpn.healthyWait += s.config.OpenVPN.Addition + s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWait) } diff --git a/internal/healthcheck/server.go b/internal/healthcheck/server.go index 5a3c7600..bcce922e 100644 --- a/internal/healthcheck/server.go +++ b/internal/healthcheck/server.go @@ -21,27 +21,27 @@ type server struct { logger logging.Logger handler *handler resolver *net.Resolver + config configuration.Health openvpn openvpnHealth } type openvpnHealth struct { - looper openvpn.Looper - healthyWaitConfig configuration.HealthyWait - currentHealthyWait time.Duration - healthyTimer *time.Timer + looper openvpn.Looper + healthyWait time.Duration + healthyTimer *time.Timer } -func NewServer(address string, settings configuration.Health, +func NewServer(address string, config configuration.Health, logger logging.Logger, openvpnLooper openvpn.Looper) Server { return &server{ address: address, logger: logger, handler: newHandler(logger), resolver: net.DefaultResolver, + config: config, openvpn: openvpnHealth{ - looper: openvpnLooper, - currentHealthyWait: settings.OpenVPN.Initial, - healthyWaitConfig: settings.OpenVPN, + looper: openvpnLooper, + healthyWait: config.OpenVPN.Initial, }, } }