Maint: rename health OpenVPN names to VPN

- `HEALTH_OPENVPN_DURATION_INITIAL` renamed to `HEALTH_VPN_DURATION_INITIAL` with retro-compatiblity
- `HEALTH_OPENVPN_DURATION_ADDITION` renamed to `HEALTH_VPN_DURATION_ADDITION` with retro-compatiblity
This commit is contained in:
Quentin McGaw (desktop)
2021-09-11 21:04:21 +00:00
parent 87f4b9e422
commit 0eccd068e5
6 changed files with 53 additions and 51 deletions

View File

@@ -12,7 +12,7 @@ import (
// Health contains settings for the healthcheck and health server. // Health contains settings for the healthcheck and health server.
type Health struct { type Health struct {
ServerAddress string ServerAddress string
OpenVPN HealthyWait VPN HealthyWait
} }
func (settings *Health) String() string { func (settings *Health) String() string {
@@ -24,8 +24,8 @@ func (settings *Health) lines() (lines []string) {
lines = append(lines, indent+lastIndent+"Server address: "+settings.ServerAddress) lines = append(lines, indent+lastIndent+"Server address: "+settings.ServerAddress)
lines = append(lines, indent+lastIndent+"OpenVPN:") lines = append(lines, indent+lastIndent+"VPN:")
for _, line := range settings.OpenVPN.lines() { for _, line := range settings.VPN.lines() {
lines = append(lines, indent+indent+line) lines = append(lines, indent+indent+line)
} }
@@ -49,14 +49,16 @@ func (settings *Health) read(r reader) (err error) {
return fmt.Errorf("environment variable HEALTH_SERVER_ADDRESS: %w", err) return fmt.Errorf("environment variable HEALTH_SERVER_ADDRESS: %w", err)
} }
settings.OpenVPN.Initial, err = r.env.Duration("HEALTH_OPENVPN_DURATION_INITIAL", params.Default("6s")) retroKeyOption := params.RetroKeys([]string{"HEALTH_OPENVPN_DURATION_INITIAL"}, r.onRetroActive)
settings.VPN.Initial, err = r.env.Duration("HEALTH_VPN_DURATION_INITIAL", params.Default("6s"), retroKeyOption)
if err != nil { if err != nil {
return fmt.Errorf("environment variable HEALTH_OPENVPN_DURATION_INITIAL: %w", err) return fmt.Errorf("environment variable HEALTH_VPN_DURATION_INITIAL: %w", err)
} }
settings.OpenVPN.Addition, err = r.env.Duration("HEALTH_OPENVPN_DURATION_ADDITION", params.Default("5s")) retroKeyOption = params.RetroKeys([]string{"HEALTH_OPENVPN_DURATION_ADDITION"}, r.onRetroActive)
settings.VPN.Addition, err = r.env.Duration("HEALTH_VPN_DURATION_ADDITION", params.Default("5s"), retroKeyOption)
if err != nil { if err != nil {
return fmt.Errorf("environment variable HEALTH_OPENVPN_DURATION_ADDITION: %w", err) return fmt.Errorf("environment variable HEALTH_VPN_DURATION_ADDITION: %w", err)
} }
return nil return nil

View File

@@ -16,7 +16,7 @@ func Test_Health_String(t *testing.T) {
t.Parallel() t.Parallel()
var health Health var health Health
const expected = "|--Health:\n |--Server address: \n |--OpenVPN:\n |--Initial duration: 0s" const expected = "|--Health:\n |--Server address: \n |--VPN:\n |--Initial duration: 0s"
s := health.String() s := health.String()
@@ -34,14 +34,14 @@ func Test_Health_lines(t *testing.T) {
lines: []string{ lines: []string{
"|--Health:", "|--Health:",
" |--Server address: ", " |--Server address: ",
" |--OpenVPN:", " |--VPN:",
" |--Initial duration: 0s", " |--Initial duration: 0s",
}, },
}, },
"filled settings": { "filled settings": {
settings: Health{ settings: Health{
ServerAddress: "address:9999", ServerAddress: "address:9999",
OpenVPN: HealthyWait{ VPN: HealthyWait{
Initial: time.Second, Initial: time.Second,
Addition: time.Minute, Addition: time.Minute,
}, },
@@ -49,7 +49,7 @@ func Test_Health_lines(t *testing.T) {
lines: []string{ lines: []string{
"|--Health:", "|--Health:",
" |--Server address: address:9999", " |--Server address: address:9999",
" |--OpenVPN:", " |--VPN:",
" |--Initial duration: 1s", " |--Initial duration: 1s",
" |--Addition duration: 1m0s", " |--Addition duration: 1m0s",
}, },
@@ -74,61 +74,61 @@ func Test_Health_read(t *testing.T) {
errDummy := errors.New("dummy") errDummy := errors.New("dummy")
testCases := map[string]struct { testCases := map[string]struct {
openvpnInitialDuration time.Duration vpnInitialDuration time.Duration
openvpnInitialErr error vpnInitialErr error
openvpnAdditionDuration time.Duration vpnAdditionDuration time.Duration
openvpnAdditionErr error vpnAdditionErr error
serverAddress string serverAddress string
serverAddressWarning string serverAddressWarning string
serverAddressErr error serverAddressErr error
expected Health expected Health
err error err error
}{ }{
"success": { "success": {
openvpnInitialDuration: time.Second, vpnInitialDuration: time.Second,
openvpnAdditionDuration: time.Minute, vpnAdditionDuration: time.Minute,
serverAddress: "127.0.0.1:9999", serverAddress: "127.0.0.1:9999",
expected: Health{ expected: Health{
ServerAddress: "127.0.0.1:9999", ServerAddress: "127.0.0.1:9999",
OpenVPN: HealthyWait{ VPN: HealthyWait{
Initial: time.Second, Initial: time.Second,
Addition: time.Minute, Addition: time.Minute,
}, },
}, },
}, },
"listening address error": { "listening address error": {
openvpnInitialDuration: time.Second, vpnInitialDuration: time.Second,
openvpnAdditionDuration: time.Minute, vpnAdditionDuration: time.Minute,
serverAddress: "127.0.0.1:9999", serverAddress: "127.0.0.1:9999",
serverAddressWarning: "warning", serverAddressWarning: "warning",
serverAddressErr: errDummy, serverAddressErr: errDummy,
expected: Health{ expected: Health{
ServerAddress: "127.0.0.1:9999", ServerAddress: "127.0.0.1:9999",
}, },
err: errors.New("environment variable HEALTH_SERVER_ADDRESS: dummy"), err: errors.New("environment variable HEALTH_SERVER_ADDRESS: dummy"),
}, },
"initial error": { "initial error": {
openvpnInitialDuration: time.Second, vpnInitialDuration: time.Second,
openvpnInitialErr: errDummy, vpnInitialErr: errDummy,
openvpnAdditionDuration: time.Minute, vpnAdditionDuration: time.Minute,
expected: Health{ expected: Health{
OpenVPN: HealthyWait{ VPN: HealthyWait{
Initial: time.Second, Initial: time.Second,
}, },
}, },
err: errors.New("environment variable HEALTH_OPENVPN_DURATION_INITIAL: dummy"), err: errors.New("environment variable HEALTH_VPN_DURATION_INITIAL: dummy"),
}, },
"addition error": { "addition error": {
openvpnInitialDuration: time.Second, vpnInitialDuration: time.Second,
openvpnAdditionDuration: time.Minute, vpnAdditionDuration: time.Minute,
openvpnAdditionErr: errDummy, vpnAdditionErr: errDummy,
expected: Health{ expected: Health{
OpenVPN: HealthyWait{ VPN: HealthyWait{
Initial: time.Second, Initial: time.Second,
Addition: time.Minute, Addition: time.Minute,
}, },
}, },
err: errors.New("environment variable HEALTH_OPENVPN_DURATION_ADDITION: dummy"), err: errors.New("environment variable HEALTH_VPN_DURATION_ADDITION: dummy"),
}, },
} }
@@ -151,12 +151,12 @@ func Test_Health_read(t *testing.T) {
if testCase.serverAddressErr == nil { if testCase.serverAddressErr == nil {
env.EXPECT(). env.EXPECT().
Duration("HEALTH_OPENVPN_DURATION_INITIAL", gomock.Any()). Duration("HEALTH_VPN_DURATION_INITIAL", gomock.Any()).
Return(testCase.openvpnInitialDuration, testCase.openvpnInitialErr) Return(testCase.vpnInitialDuration, testCase.vpnInitialErr)
if testCase.openvpnInitialErr == nil { if testCase.vpnInitialErr == nil {
env.EXPECT(). env.EXPECT().
Duration("HEALTH_OPENVPN_DURATION_ADDITION", gomock.Any()). Duration("HEALTH_VPN_DURATION_ADDITION", gomock.Any()).
Return(testCase.openvpnAdditionDuration, testCase.openvpnAdditionErr) Return(testCase.vpnAdditionDuration, testCase.vpnAdditionErr)
} }
} }

View File

@@ -51,7 +51,7 @@ func Test_Settings_lines(t *testing.T) {
" |--Timezone: NOT SET ⚠️ - it can cause time related issues", " |--Timezone: NOT SET ⚠️ - it can cause time related issues",
"|--Health:", "|--Health:",
" |--Server address: ", " |--Server address: ",
" |--OpenVPN:", " |--VPN:",
" |--Initial duration: 0s", " |--Initial duration: 0s",
"|--HTTP control server:", "|--HTTP control server:",
" |--Listening port: 0", " |--Listening port: 0",

View File

@@ -23,7 +23,7 @@ func (s *Server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) {
if previousErr != nil && err == nil { if previousErr != nil && err == nil {
s.logger.Info("healthy!") s.logger.Info("healthy!")
s.vpn.healthyTimer.Stop() s.vpn.healthyTimer.Stop()
s.vpn.healthyWait = s.config.OpenVPN.Initial s.vpn.healthyWait = s.config.VPN.Initial
} else if previousErr == nil && err != nil { } else if previousErr == nil && err != nil {
s.logger.Info("unhealthy: " + err.Error()) s.logger.Info("unhealthy: " + err.Error())
s.vpn.healthyTimer.Stop() s.vpn.healthyTimer.Stop()
@@ -40,7 +40,7 @@ func (s *Server) runHealthcheckLoop(ctx context.Context, done chan<- struct{}) {
return return
case <-timer.C: case <-timer.C:
case <-s.vpn.healthyTimer.C: case <-s.vpn.healthyTimer.C:
s.onUnhealthyOpenvpn(ctx) s.onUnhealthyVPN(ctx)
} }
continue continue
} }

View File

@@ -14,11 +14,11 @@ type vpnHealth struct {
healthyTimer *time.Timer healthyTimer *time.Timer
} }
func (s *Server) onUnhealthyOpenvpn(ctx context.Context) { func (s *Server) onUnhealthyVPN(ctx context.Context) {
s.logger.Info("program has been unhealthy for " + s.logger.Info("program has been unhealthy for " +
s.vpn.healthyWait.String() + ": restarting OpenVPN") s.vpn.healthyWait.String() + ": restarting VPN")
_, _ = s.vpn.looper.ApplyStatus(ctx, constants.Stopped) _, _ = s.vpn.looper.ApplyStatus(ctx, constants.Stopped)
_, _ = s.vpn.looper.ApplyStatus(ctx, constants.Running) _, _ = s.vpn.looper.ApplyStatus(ctx, constants.Running)
s.vpn.healthyWait += s.config.OpenVPN.Addition s.vpn.healthyWait += s.config.VPN.Addition
s.vpn.healthyTimer = time.NewTimer(s.vpn.healthyWait) s.vpn.healthyTimer = time.NewTimer(s.vpn.healthyWait)
} }

View File

@@ -32,7 +32,7 @@ func NewServer(config configuration.Health,
config: config, config: config,
vpn: vpnHealth{ vpn: vpnHealth{
looper: vpnLooper, looper: vpnLooper,
healthyWait: config.OpenVPN.Initial, healthyWait: config.VPN.Initial,
}, },
} }
} }