refactor(watchdog): remove tcp socket connection and add local unix sock connection
This commit is contained in:
@@ -16,26 +16,28 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const socketPath = "/tmp/super-frpc-watchdog.sock"
|
||||
|
||||
var (
|
||||
tcpConn net.Conn
|
||||
tcpConnMutex sync.Mutex
|
||||
isConnected bool
|
||||
recvChan chan string
|
||||
stopRecvChan chan struct{}
|
||||
recvWg sync.WaitGroup
|
||||
localConn net.Conn
|
||||
localConnMutex sync.Mutex
|
||||
isConnected bool
|
||||
recvChan chan string
|
||||
stopRecvChan chan struct{}
|
||||
recvWg sync.WaitGroup
|
||||
)
|
||||
|
||||
func Init() error {
|
||||
tcpConnMutex.Lock()
|
||||
localConnMutex.Lock()
|
||||
if recvChan != nil || stopRecvChan != nil {
|
||||
tcpConnMutex.Unlock()
|
||||
localConnMutex.Unlock()
|
||||
return nil
|
||||
}
|
||||
if tcpConn != nil {
|
||||
tcpConnMutex.Unlock()
|
||||
return fmt.Errorf("TCP client already initialized")
|
||||
if localConn != nil {
|
||||
localConnMutex.Unlock()
|
||||
return fmt.Errorf("local socket client already initialized")
|
||||
}
|
||||
tcpConnMutex.Unlock()
|
||||
localConnMutex.Unlock()
|
||||
|
||||
if global.CurrentConfig.Watchdog.Enabled {
|
||||
if err := ensureWatchdogProcess(); err != nil {
|
||||
@@ -44,8 +46,8 @@ func Init() error {
|
||||
}
|
||||
}
|
||||
|
||||
tcpConnMutex.Lock()
|
||||
defer tcpConnMutex.Unlock()
|
||||
localConnMutex.Lock()
|
||||
defer localConnMutex.Unlock()
|
||||
|
||||
recvChan = make(chan string, 100)
|
||||
stopRecvChan = make(chan struct{})
|
||||
@@ -135,21 +137,20 @@ func isWatchdogProcessRunning(watchdogName string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func tcpConnect(ipaddr string, port int) error {
|
||||
tcpConnMutex.Lock()
|
||||
defer tcpConnMutex.Unlock()
|
||||
func localSocketConnect() error {
|
||||
localConnMutex.Lock()
|
||||
defer localConnMutex.Unlock()
|
||||
|
||||
if tcpConn != nil {
|
||||
if localConn != nil {
|
||||
return fmt.Errorf("already connected")
|
||||
}
|
||||
|
||||
address := net.JoinHostPort(ipaddr, fmt.Sprintf("%d", port))
|
||||
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
|
||||
conn, err := net.DialTimeout("unix", socketPath, 3*time.Second)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect to %s: %v", address, err)
|
||||
return fmt.Errorf("failed to connect to %s: %v", socketPath, err)
|
||||
}
|
||||
|
||||
tcpConn = conn
|
||||
localConn = conn
|
||||
isConnected = true
|
||||
|
||||
recvWg.Add(1)
|
||||
@@ -159,20 +160,20 @@ func tcpConnect(ipaddr string, port int) error {
|
||||
}
|
||||
|
||||
func sendMsg(message string, timeout int) (string, error) {
|
||||
tcpConnMutex.Lock()
|
||||
localConnMutex.Lock()
|
||||
|
||||
if tcpConn == nil {
|
||||
tcpConnMutex.Unlock()
|
||||
if localConn == nil {
|
||||
localConnMutex.Unlock()
|
||||
return "", fmt.Errorf("not connected")
|
||||
}
|
||||
|
||||
_, err := tcpConn.Write([]byte(message + "\n"))
|
||||
_, err := localConn.Write([]byte(message + "\n"))
|
||||
if err != nil {
|
||||
tcpConnMutex.Unlock()
|
||||
localConnMutex.Unlock()
|
||||
return "", fmt.Errorf("failed to send message: %v", err)
|
||||
}
|
||||
|
||||
tcpConnMutex.Unlock()
|
||||
localConnMutex.Unlock()
|
||||
|
||||
select {
|
||||
case response := <-recvChan:
|
||||
@@ -188,13 +189,13 @@ func sendMsg(message string, timeout int) (string, error) {
|
||||
func recvMsg() {
|
||||
defer recvWg.Done()
|
||||
|
||||
tcpConnMutex.Lock()
|
||||
if tcpConn == nil {
|
||||
tcpConnMutex.Unlock()
|
||||
localConnMutex.Lock()
|
||||
if localConn == nil {
|
||||
localConnMutex.Unlock()
|
||||
return
|
||||
}
|
||||
conn := tcpConn
|
||||
tcpConnMutex.Unlock()
|
||||
conn := localConn
|
||||
localConnMutex.Unlock()
|
||||
|
||||
reader := bufio.NewReader(conn)
|
||||
|
||||
@@ -215,9 +216,7 @@ func recvMsg() {
|
||||
default:
|
||||
// drop the message
|
||||
}
|
||||
// Here add logic to handle the message
|
||||
if !isResponseMessage(line) {
|
||||
// postLog.Debug(fmt.Sprintf("[Watchdog] TCP Socket received message: %s", line))
|
||||
parseCommand(line)
|
||||
}
|
||||
}
|
||||
@@ -228,13 +227,13 @@ func recvMsg() {
|
||||
continue
|
||||
}
|
||||
|
||||
tcpConnMutex.Lock()
|
||||
if tcpConn != nil {
|
||||
tcpConn.Close()
|
||||
tcpConn = nil
|
||||
localConnMutex.Lock()
|
||||
if localConn != nil {
|
||||
localConn.Close()
|
||||
localConn = nil
|
||||
isConnected = false
|
||||
}
|
||||
tcpConnMutex.Unlock()
|
||||
localConnMutex.Unlock()
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -246,23 +245,23 @@ func isResponseMessage(msg string) bool {
|
||||
}
|
||||
|
||||
func Destroy() error {
|
||||
tcpConnMutex.Lock()
|
||||
localConnMutex.Lock()
|
||||
|
||||
if stopRecvChan != nil {
|
||||
close(stopRecvChan)
|
||||
stopRecvChan = nil
|
||||
}
|
||||
|
||||
tcpConnMutex.Unlock()
|
||||
localConnMutex.Unlock()
|
||||
|
||||
recvWg.Wait()
|
||||
|
||||
tcpConnMutex.Lock()
|
||||
defer tcpConnMutex.Unlock()
|
||||
localConnMutex.Lock()
|
||||
defer localConnMutex.Unlock()
|
||||
|
||||
if tcpConn != nil {
|
||||
err := tcpConn.Close()
|
||||
tcpConn = nil
|
||||
if localConn != nil {
|
||||
err := localConn.Close()
|
||||
localConn = nil
|
||||
isConnected = false
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to close connection: %v", err)
|
||||
@@ -278,7 +277,7 @@ func Destroy() error {
|
||||
}
|
||||
|
||||
func IsConnected() bool {
|
||||
tcpConnMutex.Lock()
|
||||
defer tcpConnMutex.Unlock()
|
||||
return isConnected && tcpConn != nil
|
||||
localConnMutex.Lock()
|
||||
defer localConnMutex.Unlock()
|
||||
return isConnected && localConn != nil
|
||||
}
|
||||
+4
-2
@@ -6,7 +6,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func Connect(ipaddr string, port int) bool {
|
||||
func Connect() bool {
|
||||
if IsConnected() {
|
||||
return true
|
||||
}
|
||||
@@ -18,7 +18,7 @@ func Connect(ipaddr string, port int) bool {
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
var lastErr error
|
||||
for {
|
||||
if err := tcpConnect(ipaddr, port); err == nil {
|
||||
if err := localSocketConnect(); err == nil {
|
||||
break
|
||||
} else {
|
||||
lastErr = err
|
||||
@@ -58,6 +58,8 @@ func Disconnect() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
KillWatchdogProcess()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
return true
|
||||
|
||||
@@ -29,7 +29,7 @@ func StartKeepAlive() error {
|
||||
continue
|
||||
}
|
||||
|
||||
if Connect("127.0.0.1", global.CurrentConfig.Watchdog.Port) {
|
||||
if err = Init(); err == nil && Connect() {
|
||||
global.Is.WatchdogConnected = true
|
||||
postLog.Info("[watchdog] successfully reconnected to watchdog")
|
||||
lastErr = nil
|
||||
@@ -37,7 +37,7 @@ func StartKeepAlive() error {
|
||||
}
|
||||
|
||||
lastErr = fmt.Errorf("failed to connect to watchdog")
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
time.Sleep(1000 * time.Millisecond)
|
||||
}
|
||||
|
||||
if lastErr != nil {
|
||||
|
||||
@@ -6,3 +6,13 @@ import "os/exec"
|
||||
|
||||
func configureWatchdogCommand(cmd *exec.Cmd) {
|
||||
}
|
||||
|
||||
func KillWatchdogProcess() error {
|
||||
watchdogName, err := getWatchdogBinaryName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := exec.Command("pkill", "-f", watchdogName)
|
||||
return cmd.Run()
|
||||
}
|
||||
@@ -10,3 +10,13 @@ import (
|
||||
func configureWatchdogCommand(cmd *exec.Cmd) {
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
||||
}
|
||||
|
||||
func KillWatchdogProcess() error {
|
||||
watchdogName, err := getWatchdogBinaryName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := exec.Command("taskkill", "/IM", watchdogName, "/F")
|
||||
return cmd.Run()
|
||||
}
|
||||
Reference in New Issue
Block a user