package watchdog import ( "fmt" "time" "super-frpc/global" "super-frpc/postLog" ) func StartKeepAlive() error { ticker := time.NewTicker(3 * time.Second) defer ticker.Stop() for { <-ticker.C if err := isProcessAlive(); err != nil { global.Is.WatchdogConnected = false postLog.Warning(fmt.Sprintf("[watchdog] keepalive check failed: %v", err)) _ = Disconnect() var lastErr error for i := 0; i < 5; i++ { if err := ensureWatchdogProcess(); err != nil { lastErr = err time.Sleep(500 * time.Millisecond) continue } if err = Init(); err == nil && Connect() { global.Is.WatchdogConnected = true postLog.Info("[watchdog] successfully reconnected to watchdog") lastErr = nil break } lastErr = fmt.Errorf("failed to connect to watchdog") time.Sleep(1000 * time.Millisecond) } if lastErr != nil { postLog.Error(fmt.Sprintf("[watchdog] failed to recover watchdog connection: %v", lastErr)) return fmt.Errorf("watchdog recovery failed after 5 attempts: %w", lastErr) } } } } func isProcessAlive() error { if watchdogName, err := getWatchdogBinaryName(); err == nil && isWatchdogProcessRunning(watchdogName) { resp, err := sendMsg("watchdogAgentConnectionTest", 3) if err != nil || resp != "success" { return fmt.Errorf("watchdog not responding to connection test") } } else { return fmt.Errorf("watchdog process not running") } return nil }