feat(watchdog): implement keepalive mechanism and enhance connection handling

This commit is contained in:
2026-05-09 17:29:02 +08:00
parent 21a4b03c57
commit 9c684241d1
5 changed files with 105 additions and 18 deletions

View File

@@ -1,6 +1,8 @@
package watchdog
import (
"fmt"
"super-frpc/postLog"
"time"
)
@@ -13,17 +15,32 @@ func Connect(ipaddr string, port int) bool {
return false
}
if err := tcpConnect(ipaddr, port); err != nil {
return false
deadline := time.Now().Add(5 * time.Second)
var lastErr error
for {
if err := tcpConnect(ipaddr, port); err == nil {
break
} else {
lastErr = err
}
if time.Now().After(deadline) {
postLog.Error(fmt.Sprintf("[watchdog] failed to connect before timeout: %v", lastErr))
return false
}
time.Sleep(200 * time.Millisecond)
}
response, err := sendMsg("watchdogAgentConnectionTest", 3)
if err != nil {
postLog.Error(fmt.Sprintf("[watchdog] connection test failed: %v", err))
Destroy()
return false
}
if response != "success" {
postLog.Error(fmt.Sprintf("[watchdog] connection test returned unexpected response: %s", response))
Destroy()
return false
}