added healthy check in main.go

added healthy check in main.go
extended the ping check that creates a /tmp/healthy file if ping successfull and removes that file if ping failes 3 times.

With this you can add the following to the newt docker compose to do the health check:
healthcheck:
  test: ["CMD-SHELL", "test -f /tmp/healthy"]
  interval: 30s
  timeout: 10s
  retries: 3
This commit is contained in:
Wouter van Elten
2025-06-25 19:43:27 +02:00
committed by GitHub
parent 678d82fa68
commit a76e6c9637

11
main.go
View File

@@ -127,7 +127,7 @@ func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{})
go func() {
for {
select {
case <-ticker.C:
case <-ticker.C:
err := ping(tnet, serverIP)
if err != nil {
consecutiveFailures++
@@ -137,8 +137,9 @@ func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{})
if consecutiveFailures >= 3 {
_ = os.Remove("/tmp/healthy")
}
// increase interval if it keeps failing
// Increase interval if we have consistent failures, with a maximum cap
if consecutiveFailures >= 3 && currentInterval < maxInterval {
// Increase by 50% each time, up to the maximum
currentInterval = time.Duration(float64(currentInterval) * 1.5)
if currentInterval > maxInterval {
currentInterval = maxInterval
@@ -152,7 +153,7 @@ func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{})
if err != nil {
logger.Warn("Failed to write health file: %v", err)
}
// Reset interval if we increased it
// On success, if we've backed off, gradually return to normal interval
if currentInterval > initialInterval {
currentInterval = time.Duration(float64(currentInterval) * 0.8)
if currentInterval < initialInterval {
@@ -163,9 +164,9 @@ func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{})
}
consecutiveFailures = 0
}
case <-stopChan:
case <-stopChan:
logger.Info("Stopping ping check")
return
return
}
}
}()