feat(watchdog): implement keepalive mechanism and enhance connection handling
This commit is contained in:
@@ -1,2 +1,62 @@
|
||||
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 Connect("127.0.0.1", global.CurrentConfig.Watchdog.Port) {
|
||||
global.Is.WatchdogConnected = true
|
||||
postLog.Info("[watchdog] successfully reconnected to watchdog")
|
||||
lastErr = nil
|
||||
break
|
||||
}
|
||||
|
||||
lastErr = fmt.Errorf("failed to connect to watchdog")
|
||||
time.Sleep(500 * 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user