Maint: rework openvpn restart on unhealthy

This commit is contained in:
Quentin McGaw (desktop)
2021-07-18 03:17:48 +00:00
parent 7e50c95823
commit c0d27b4bfc
5 changed files with 59 additions and 74 deletions

View File

@@ -9,24 +9,24 @@ import (
"time"
)
func (s *server) runHealthcheckLoop(ctx context.Context, healthy chan<- bool, done chan<- struct{}) {
func (s *server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) {
defer close(done)
s.openvpn.healthyTimer = time.NewTimer(defaultOpenvpnHealthyWaitTime)
for {
previousErr := s.handler.getErr()
err := healthCheck(ctx, s.resolver)
s.handler.setErr(err)
// Notify the healthy channel, or not if it's already full
select {
case healthy <- err == nil:
default:
}
if previousErr != nil && err == nil {
s.logger.Info("healthy!")
s.openvpn.healthyTimer.Stop()
s.openvpn.healthyWaitTime = defaultOpenvpnHealthyWaitTime
} else if previousErr == nil && err != nil {
s.logger.Info("unhealthy: " + err.Error())
s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWaitTime)
}
if err != nil { // try again after 1 second
@@ -38,9 +38,12 @@ func (s *server) runHealthcheckLoop(ctx context.Context, healthy chan<- bool, do
}
return
case <-timer.C:
case <-s.openvpn.healthyTimer.C:
s.onUnhealthyOpenvpn(ctx)
}
continue
}
// Success, check again in 5 seconds
const period = 5 * time.Second
timer := time.NewTimer(period)

View File

@@ -0,0 +1,17 @@
package healthcheck
import (
"context"
"time"
"github.com/qdm12/gluetun/internal/constants"
)
func (s *server) onUnhealthyOpenvpn(ctx context.Context) {
s.logger.Info("program has been unhealthy for " +
s.openvpn.healthyWaitTime.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)
}

View File

@@ -7,11 +7,12 @@ import (
"net/http"
"time"
"github.com/qdm12/gluetun/internal/openvpn"
"github.com/qdm12/golibs/logging"
)
type Server interface {
Run(ctx context.Context, healthy chan<- bool, done chan<- struct{})
Run(ctx context.Context, done chan<- struct{})
}
type server struct {
@@ -19,22 +20,40 @@ type server struct {
logger logging.Logger
handler *handler
resolver *net.Resolver
openvpn openvpnHealth
}
func NewServer(address string, logger logging.Logger) Server {
type openvpnHealth struct {
looper openvpn.Looper
healthyWaitTime 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 {
return &server{
address: address,
logger: logger,
handler: newHandler(logger),
resolver: net.DefaultResolver,
openvpn: openvpnHealth{
looper: openvpnLooper,
healthyWaitTime: defaultOpenvpnHealthyWaitTime,
},
}
}
func (s *server) Run(ctx context.Context, healthy chan<- bool, done chan<- struct{}) {
func (s *server) Run(ctx context.Context, done chan<- struct{}) {
defer close(done)
s.logger.Debug("here 0")
loopDone := make(chan struct{})
go s.runHealthcheckLoop(ctx, healthy, loopDone)
go s.runHealthcheckLoop(ctx, loopDone)
server := http.Server{
Addr: s.address,