feat(watchdog): implement keepalive mechanism and enhance connection handling
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user