From 4cb31df3c80d9b76c3703ddb148aef43a439fe6f Mon Sep 17 00:00:00 2001 From: Owen Date: Sat, 12 Apr 2025 17:51:29 -0400 Subject: [PATCH] Fix concurrancy problem and add +1 back --- main.go | 2 +- websocket/client.go | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 89fa99c..c94de41 100644 --- a/main.go +++ b/main.go @@ -406,7 +406,7 @@ func main() { // take the first part of the allowedIp and the port from the endpoint and put them together monitorAddress := strings.Split(site.ServerIP, "/")[0] - monitorPeer := fmt.Sprintf("%s:%d", monitorAddress, site.ServerPort) + monitorPeer := fmt.Sprintf("%s:%d", monitorAddress, site.ServerPort+1) // +1 for the monitor port wgConfig := &peermonitor.WireGuardConfig{ SiteID: site.SiteId, diff --git a/websocket/client.go b/websocket/client.go index 9725c50..14d36bd 100644 --- a/websocket/client.go +++ b/websocket/client.go @@ -22,6 +22,7 @@ type Client struct { handlers map[string]MessageHandler done chan struct{} handlersMux sync.RWMutex + writeMux sync.Mutex reconnectInterval time.Duration isConnected bool @@ -110,6 +111,8 @@ func (c *Client) SendMessage(messageType string, data interface{}) error { Data: data, } + c.writeMux.Lock() + defer c.writeMux.Unlock() return c.conn.WriteJSON(msg) } @@ -334,7 +337,10 @@ func (c *Client) pingMonitor() { case <-c.done: return case <-ticker.C: - if err := c.conn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(10*time.Second)); err != nil { + c.writeMux.Lock() + err := c.conn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(10*time.Second)) + c.writeMux.Unlock() + if err != nil { logger.Error("Ping failed: %v", err) c.reconnect() return