mirror of
https://github.com/fosrl/newt.git
synced 2026-03-05 10:16:44 +00:00
Merge pull request #71 from woutervanelten/add-healthcheck
Add healthcheck
This commit is contained in:
21
main.go
21
main.go
@@ -115,7 +115,7 @@ func ping(tnet *netstack.Net, dst string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{}) {
|
func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{}, healthFile string) {
|
||||||
initialInterval := 10 * time.Second
|
initialInterval := 10 * time.Second
|
||||||
maxInterval := 60 * time.Second
|
maxInterval := 60 * time.Second
|
||||||
currentInterval := initialInterval
|
currentInterval := initialInterval
|
||||||
@@ -135,6 +135,11 @@ func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{})
|
|||||||
consecutiveFailures, err)
|
consecutiveFailures, err)
|
||||||
logger.Warn("HINT: Do you have UDP port 51820 (or the port in config.yml) open on your Pangolin server?")
|
logger.Warn("HINT: Do you have UDP port 51820 (or the port in config.yml) open on your Pangolin server?")
|
||||||
|
|
||||||
|
// Only remove file if healthFile is set
|
||||||
|
if consecutiveFailures >= 3 && healthFile != "" {
|
||||||
|
_ = os.Remove(healthFile)
|
||||||
|
}
|
||||||
|
|
||||||
// Increase interval if we have consistent failures, with a maximum cap
|
// Increase interval if we have consistent failures, with a maximum cap
|
||||||
if consecutiveFailures >= 3 && currentInterval < maxInterval {
|
if consecutiveFailures >= 3 && currentInterval < maxInterval {
|
||||||
// Increase by 50% each time, up to the maximum
|
// Increase by 50% each time, up to the maximum
|
||||||
@@ -147,6 +152,13 @@ func startPingCheck(tnet *netstack.Net, serverIP string, stopChan chan struct{})
|
|||||||
currentInterval)
|
currentInterval)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Only write file if healthFile is set
|
||||||
|
if healthFile != "" {
|
||||||
|
err := os.WriteFile(healthFile, []byte("ok"), 0644)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("Failed to write health file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
// On success, if we've backed off, gradually return to normal interval
|
// On success, if we've backed off, gradually return to normal interval
|
||||||
if currentInterval > initialInterval {
|
if currentInterval > initialInterval {
|
||||||
currentInterval = time.Duration(float64(currentInterval) * 0.8)
|
currentInterval = time.Duration(float64(currentInterval) * 0.8)
|
||||||
@@ -355,6 +367,7 @@ var (
|
|||||||
dockerSocket string
|
dockerSocket string
|
||||||
dockerEnforceNetworkValidation string
|
dockerEnforceNetworkValidation string
|
||||||
dockerEnforceNetworkValidationBool bool
|
dockerEnforceNetworkValidationBool bool
|
||||||
|
healthFile string
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -369,6 +382,7 @@ func main() {
|
|||||||
tlsPrivateKey = os.Getenv("TLS_CLIENT_CERT")
|
tlsPrivateKey = os.Getenv("TLS_CLIENT_CERT")
|
||||||
dockerSocket = os.Getenv("DOCKER_SOCKET")
|
dockerSocket = os.Getenv("DOCKER_SOCKET")
|
||||||
dockerEnforceNetworkValidation = os.Getenv("DOCKER_ENFORCE_NETWORK_VALIDATION")
|
dockerEnforceNetworkValidation = os.Getenv("DOCKER_ENFORCE_NETWORK_VALIDATION")
|
||||||
|
healthFile = os.Getenv("HEALTH_FILE")
|
||||||
|
|
||||||
if endpoint == "" {
|
if endpoint == "" {
|
||||||
flag.StringVar(&endpoint, "endpoint", "", "Endpoint of your pangolin server")
|
flag.StringVar(&endpoint, "endpoint", "", "Endpoint of your pangolin server")
|
||||||
@@ -400,6 +414,9 @@ func main() {
|
|||||||
if dockerEnforceNetworkValidation == "" {
|
if dockerEnforceNetworkValidation == "" {
|
||||||
flag.StringVar(&dockerEnforceNetworkValidation, "docker-enforce-network-validation", "false", "Enforce validation of container on newt network (true or false)")
|
flag.StringVar(&dockerEnforceNetworkValidation, "docker-enforce-network-validation", "false", "Enforce validation of container on newt network (true or false)")
|
||||||
}
|
}
|
||||||
|
if healthFile == "" {
|
||||||
|
flag.StringVar(&healthFile, "health-file", "", "Path to health file (if unset, health file won’t be written)")
|
||||||
|
}
|
||||||
|
|
||||||
// do a --version check
|
// do a --version check
|
||||||
version := flag.Bool("version", false, "Print the version")
|
version := flag.Bool("version", false, "Print the version")
|
||||||
@@ -560,7 +577,7 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
// as the pings will continue in the background
|
// as the pings will continue in the background
|
||||||
if !connected {
|
if !connected {
|
||||||
logger.Info("Starting ping check")
|
logger.Info("Starting ping check")
|
||||||
startPingCheck(tnet, wgData.ServerIP, pingStopChan)
|
startPingCheck(tnet, wgData.ServerIP, pingStopChan, healthFile)
|
||||||
|
|
||||||
// Start connection monitoring in a separate goroutine
|
// Start connection monitoring in a separate goroutine
|
||||||
go monitorConnectionStatus(tnet, wgData.ServerIP, client)
|
go monitorConnectionStatus(tnet, wgData.ServerIP, client)
|
||||||
|
|||||||
Reference in New Issue
Block a user