🚑 Fix concurent writes in wsClients map
This commit is contained in:
12
api.go
12
api.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@ -15,6 +16,7 @@ type WsClient struct {
|
|||||||
SendUpdates bool
|
SendUpdates bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var wsClientsMutex = new(sync.Mutex)
|
||||||
var wsClients = make(map[*websocket.Conn]WsClient)
|
var wsClients = make(map[*websocket.Conn]WsClient)
|
||||||
|
|
||||||
func initApi() {
|
func initApi() {
|
||||||
@ -428,13 +430,17 @@ func handleWs(c *gin.Context) {
|
|||||||
c.JSON(500, gin.H{"success": false, "error": "Handling websocket connection failed!"})
|
c.JSON(500, gin.H{"success": false, "error": "Handling websocket connection failed!"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wsClientsMutex.Lock()
|
||||||
wsClients[conn] = WsClient{SendUpdates: false}
|
wsClients[conn] = WsClient{SendUpdates: false}
|
||||||
|
wsClientsMutex.Unlock()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
var content WebsocketMessage
|
var content WebsocketMessage
|
||||||
jsonErr := conn.ReadJSON(&content)
|
jsonErr := conn.ReadJSON(&content)
|
||||||
if jsonErr != nil {
|
if jsonErr != nil {
|
||||||
|
wsClientsMutex.Lock()
|
||||||
delete(wsClients, conn)
|
delete(wsClients, conn)
|
||||||
|
wsClientsMutex.Unlock()
|
||||||
conn.Close()
|
conn.Close()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -455,10 +461,12 @@ func handleWs(c *gin.Context) {
|
|||||||
case "deleteRoute":
|
case "deleteRoute":
|
||||||
err = deleteRoute(content.UUID, content.ToUUID)
|
err = deleteRoute(content.UUID, content.ToUUID)
|
||||||
case "enableUpdates":
|
case "enableUpdates":
|
||||||
|
wsClientsMutex.Lock()
|
||||||
if clientOpts, ok := wsClients[conn]; ok {
|
if clientOpts, ok := wsClients[conn]; ok {
|
||||||
clientOpts.SendUpdates = true
|
clientOpts.SendUpdates = true
|
||||||
wsClients[conn] = clientOpts
|
wsClients[conn] = clientOpts
|
||||||
}
|
}
|
||||||
|
wsClientsMutex.Unlock()
|
||||||
default:
|
default:
|
||||||
err = gin.H{"success": false, "error": "Method is not implemented"}
|
err = gin.H{"success": false, "error": "Method is not implemented"}
|
||||||
}
|
}
|
||||||
@ -467,6 +475,7 @@ func handleWs(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func onPortChange(port *Port) {
|
func onPortChange(port *Port) {
|
||||||
|
wsClientsMutex.Lock()
|
||||||
for conn := range wsClients {
|
for conn := range wsClients {
|
||||||
if !wsClients[conn].SendUpdates {
|
if !wsClients[conn].SendUpdates {
|
||||||
continue
|
continue
|
||||||
@ -474,9 +483,11 @@ func onPortChange(port *Port) {
|
|||||||
jsonData, _ := json.Marshal(WebsocketResponse{Type: "portChange", ResponseData: port})
|
jsonData, _ := json.Marshal(WebsocketResponse{Type: "portChange", ResponseData: port})
|
||||||
conn.WriteMessage(1, jsonData)
|
conn.WriteMessage(1, jsonData)
|
||||||
}
|
}
|
||||||
|
wsClientsMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func onPortListChange() {
|
func onPortListChange() {
|
||||||
|
wsClientsMutex.Lock()
|
||||||
for conn := range wsClients {
|
for conn := range wsClients {
|
||||||
if !wsClients[conn].SendUpdates {
|
if !wsClients[conn].SendUpdates {
|
||||||
continue
|
continue
|
||||||
@ -487,4 +498,5 @@ func onPortListChange() {
|
|||||||
})
|
})
|
||||||
conn.WriteMessage(1, jsonData)
|
conn.WriteMessage(1, jsonData)
|
||||||
}
|
}
|
||||||
|
wsClientsMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user