Feat: Env variables to set health timeouts

- HEALTH_OPENVPN_DURATION_INITIAL
- HEALTH_OPENVPN_DURATION_ADDITION
This commit is contained in:
Quentin McGaw (desktop)
2021-07-22 20:13:20 +00:00
parent 8beff34cca
commit 6acb7caf5b
11 changed files with 307 additions and 20 deletions

View File

@@ -12,7 +12,7 @@ import (
func (s *server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) {
defer close(done)
s.openvpn.healthyTimer = time.NewTimer(defaultOpenvpnHealthyWaitTime)
s.openvpn.healthyTimer = time.NewTimer(s.openvpn.currentHealthyWait)
for {
previousErr := s.handler.getErr()
@@ -23,10 +23,10 @@ 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.healthyWaitTime = defaultOpenvpnHealthyWaitTime
s.openvpn.currentHealthyWait = s.openvpn.healthyWaitConfig.Initial
} else if previousErr == nil && err != nil {
s.logger.Info("unhealthy: " + err.Error())
s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWaitTime)
s.openvpn.healthyTimer = time.NewTimer(s.openvpn.currentHealthyWait)
}
if err != nil { // try again after 1 second

View File

@@ -9,9 +9,9 @@ import (
func (s *server) onUnhealthyOpenvpn(ctx context.Context) {
s.logger.Info("program has been unhealthy for " +
s.openvpn.healthyWaitTime.String() + ": restarting OpenVPN")
s.openvpn.currentHealthyWait.String() + ": restarting OpenVPN")
_, _ = s.openvpn.looper.ApplyStatus(ctx, constants.Stopped)
_, _ = s.openvpn.looper.ApplyStatus(ctx, constants.Running)
s.openvpn.healthyWaitTime += openvpnHealthyWaitTimeAdd
s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWaitTime)
s.openvpn.currentHealthyWait += s.openvpn.healthyWaitConfig.Addition
s.openvpn.healthyTimer = time.NewTimer(s.openvpn.currentHealthyWait)
}

View File

@@ -7,6 +7,7 @@ import (
"net/http"
"time"
"github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/openvpn"
"github.com/qdm12/golibs/logging"
)
@@ -24,26 +25,23 @@ type server struct {
}
type openvpnHealth struct {
looper openvpn.Looper
healthyWaitTime time.Duration
healthyTimer *time.Timer
looper openvpn.Looper
healthyWaitConfig configuration.HealthyWait
currentHealthyWait time.Duration
healthyTimer *time.Timer
}
const (
defaultOpenvpnHealthyWaitTime = 6 * time.Second
openvpnHealthyWaitTimeAdd = 5 * time.Second
)
func NewServer(address string, logger logging.Logger,
openvpnLooper openvpn.Looper) Server {
func NewServer(address string, settings configuration.Health,
logger logging.Logger, openvpnLooper openvpn.Looper) Server {
return &server{
address: address,
logger: logger,
handler: newHandler(logger),
resolver: net.DefaultResolver,
openvpn: openvpnHealth{
looper: openvpnLooper,
healthyWaitTime: defaultOpenvpnHealthyWaitTime,
looper: openvpnLooper,
currentHealthyWait: settings.OpenVPN.Initial,
healthyWaitConfig: settings.OpenVPN,
},
}
}