Maint: internal/vpn package for vpn loop

This commit is contained in:
Quentin McGaw (desktop)
2021-08-18 22:01:04 +00:00
parent 05018ec971
commit d4ca5cf257
20 changed files with 60 additions and 60 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(s.openvpn.healthyWait)
s.vpn.healthyTimer = time.NewTimer(s.vpn.healthyWait)
for {
previousErr := s.handler.getErr()
@@ -22,12 +22,12 @@ 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.healthyWait = s.config.OpenVPN.Initial
s.vpn.healthyTimer.Stop()
s.vpn.healthyWait = s.config.OpenVPN.Initial
} else if previousErr == nil && err != nil {
s.logger.Info("unhealthy: " + err.Error())
s.openvpn.healthyTimer.Stop()
s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWait)
s.vpn.healthyTimer.Stop()
s.vpn.healthyTimer = time.NewTimer(s.vpn.healthyWait)
}
if err != nil { // try again after 1 second
@@ -39,7 +39,7 @@ func (s *Server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) {
}
return
case <-timer.C:
case <-s.openvpn.healthyTimer.C:
case <-s.vpn.healthyTimer.C:
s.onUnhealthyOpenvpn(ctx)
}
continue

View File

@@ -5,20 +5,20 @@ import (
"time"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/openvpn"
"github.com/qdm12/gluetun/internal/vpn"
)
type openvpnHealth struct {
looper openvpn.Looper
type vpnHealth struct {
looper vpn.Looper
healthyWait time.Duration
healthyTimer *time.Timer
}
func (s *Server) onUnhealthyOpenvpn(ctx context.Context) {
s.logger.Info("program has been unhealthy for " +
s.openvpn.healthyWait.String() + ": restarting OpenVPN")
_, _ = s.openvpn.looper.ApplyStatus(ctx, constants.Stopped)
_, _ = s.openvpn.looper.ApplyStatus(ctx, constants.Running)
s.openvpn.healthyWait += s.config.OpenVPN.Addition
s.openvpn.healthyTimer = time.NewTimer(s.openvpn.healthyWait)
s.vpn.healthyWait.String() + ": restarting OpenVPN")
_, _ = s.vpn.looper.ApplyStatus(ctx, constants.Stopped)
_, _ = s.vpn.looper.ApplyStatus(ctx, constants.Running)
s.vpn.healthyWait += s.config.OpenVPN.Addition
s.vpn.healthyTimer = time.NewTimer(s.vpn.healthyWait)
}

View File

@@ -5,7 +5,7 @@ import (
"net"
"github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/openvpn"
"github.com/qdm12/gluetun/internal/vpn"
"github.com/qdm12/golibs/logging"
)
@@ -20,18 +20,18 @@ type Server struct {
handler *handler
resolver *net.Resolver
config configuration.Health
openvpn openvpnHealth
vpn vpnHealth
}
func NewServer(config configuration.Health,
logger logging.Logger, openvpnLooper openvpn.Looper) *Server {
logger logging.Logger, vpnLooper vpn.Looper) *Server {
return &Server{
logger: logger,
handler: newHandler(logger),
resolver: net.DefaultResolver,
config: config,
openvpn: openvpnHealth{
looper: openvpnLooper,
vpn: vpnHealth{
looper: vpnLooper,
healthyWait: config.OpenVPN.Initial,
},
}