Fix concurrancy problem and add +1 back

This commit is contained in:
Owen
2025-04-12 17:51:29 -04:00
parent 3b0eef6d60
commit 4cb31df3c8
2 changed files with 8 additions and 2 deletions

View File

@@ -406,7 +406,7 @@ func main() {
// take the first part of the allowedIp and the port from the endpoint and put them together // take the first part of the allowedIp and the port from the endpoint and put them together
monitorAddress := strings.Split(site.ServerIP, "/")[0] 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{ wgConfig := &peermonitor.WireGuardConfig{
SiteID: site.SiteId, SiteID: site.SiteId,

View File

@@ -22,6 +22,7 @@ type Client struct {
handlers map[string]MessageHandler handlers map[string]MessageHandler
done chan struct{} done chan struct{}
handlersMux sync.RWMutex handlersMux sync.RWMutex
writeMux sync.Mutex
reconnectInterval time.Duration reconnectInterval time.Duration
isConnected bool isConnected bool
@@ -110,6 +111,8 @@ func (c *Client) SendMessage(messageType string, data interface{}) error {
Data: data, Data: data,
} }
c.writeMux.Lock()
defer c.writeMux.Unlock()
return c.conn.WriteJSON(msg) return c.conn.WriteJSON(msg)
} }
@@ -334,7 +337,10 @@ func (c *Client) pingMonitor() {
case <-c.done: case <-c.done:
return return
case <-ticker.C: 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) logger.Error("Ping failed: %v", err)
c.reconnect() c.reconnect()
return return