From a76e6c9637ddd28b169b2ca898ebe69abb53b7a9 Mon Sep 17 00:00:00 2001 From: Wouter van Elten Date: Wed, 25 Jun 2025 19:43:27 +0200 Subject: [PATCH] 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 --- main.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index cb4eab4..e8b8ede 100644 --- a/main.go +++ b/main.go @@ -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 } } }()