🚑 Fix concurrent writes in websocket connection
This commit is contained in:
11
api.go
11
api.go
@ -14,6 +14,7 @@ var upgrader = websocket.Upgrader{}
|
|||||||
|
|
||||||
type WsClient struct {
|
type WsClient struct {
|
||||||
SendUpdates bool
|
SendUpdates bool
|
||||||
|
WriteMutex *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var wsClientsMutex = new(sync.Mutex)
|
var wsClientsMutex = new(sync.Mutex)
|
||||||
@ -405,7 +406,9 @@ func wsSendRes(conn *websocket.Conn, res gin.H, err gin.H, request *WebsocketMes
|
|||||||
RequestMethod: request.Method,
|
RequestMethod: request.Method,
|
||||||
ResponseData: err,
|
ResponseData: err,
|
||||||
})
|
})
|
||||||
|
wsClients[conn].WriteMutex.Lock()
|
||||||
conn.WriteMessage(1, data)
|
conn.WriteMessage(1, data)
|
||||||
|
wsClients[conn].WriteMutex.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -420,7 +423,9 @@ func wsSendRes(conn *websocket.Conn, res gin.H, err gin.H, request *WebsocketMes
|
|||||||
}
|
}
|
||||||
|
|
||||||
data, _ := json.Marshal(response)
|
data, _ := json.Marshal(response)
|
||||||
|
wsClients[conn].WriteMutex.Lock()
|
||||||
conn.WriteMessage(1, data)
|
conn.WriteMessage(1, data)
|
||||||
|
wsClients[conn].WriteMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleWs(c *gin.Context) {
|
func handleWs(c *gin.Context) {
|
||||||
@ -431,7 +436,7 @@ func handleWs(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wsClientsMutex.Lock()
|
wsClientsMutex.Lock()
|
||||||
wsClients[conn] = WsClient{SendUpdates: false}
|
wsClients[conn] = WsClient{SendUpdates: false, WriteMutex: new(sync.Mutex)}
|
||||||
wsClientsMutex.Unlock()
|
wsClientsMutex.Unlock()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -481,7 +486,9 @@ func onPortChange(port *Port) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
jsonData, _ := json.Marshal(WebsocketResponse{Type: "portChange", ResponseData: port})
|
jsonData, _ := json.Marshal(WebsocketResponse{Type: "portChange", ResponseData: port})
|
||||||
|
wsClients[conn].WriteMutex.Lock()
|
||||||
conn.WriteMessage(1, jsonData)
|
conn.WriteMessage(1, jsonData)
|
||||||
|
wsClients[conn].WriteMutex.Unlock()
|
||||||
}
|
}
|
||||||
wsClientsMutex.Unlock()
|
wsClientsMutex.Unlock()
|
||||||
}
|
}
|
||||||
@ -496,7 +503,9 @@ func onPortListChange() {
|
|||||||
Type: "portListChange",
|
Type: "portListChange",
|
||||||
ResponseData: gin.H{"inputs": portConfig.Input, "outputs": portConfig.Output},
|
ResponseData: gin.H{"inputs": portConfig.Input, "outputs": portConfig.Output},
|
||||||
})
|
})
|
||||||
|
wsClients[conn].WriteMutex.Lock()
|
||||||
conn.WriteMessage(1, jsonData)
|
conn.WriteMessage(1, jsonData)
|
||||||
|
wsClients[conn].WriteMutex.Unlock()
|
||||||
}
|
}
|
||||||
wsClientsMutex.Unlock()
|
wsClientsMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user