From 77f15a197acc7bc4aa475bae9b04e11ecaebdd83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Thu, 15 Feb 2024 10:35:57 +0100 Subject: [PATCH 1/2] Added mutex locking around modifications to websocket connections --- ws_conn_manager/pkg.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ws_conn_manager/pkg.go b/ws_conn_manager/pkg.go index 386482e..5a4af9e 100644 --- a/ws_conn_manager/pkg.go +++ b/ws_conn_manager/pkg.go @@ -10,6 +10,7 @@ import ( "net/http" "slices" "strings" + "sync" ) type ReadHandler func(*ConnectionManager, *WsConnection, []byte) @@ -28,6 +29,7 @@ type ConnectionManager struct { logger *slog.Logger domains []string readHandlers []ReadHandler + connSync sync.Mutex } type SendRequest struct { WsConn *WsConnection @@ -67,7 +69,9 @@ func (cm *ConnectionManager) NewConnection(w http.ResponseWriter, r *http.Reques } // Keep track of all connections. + cm.connSync.Lock() cm.connections[wsConn.UUID] = &wsConn + cm.connSync.Unlock() // Successfully upgraded to a websocket connection. cm.logger.Info("websocket", "uuid", wsConn.UUID, "remote_addr", r.RemoteAddr) @@ -97,7 +101,9 @@ func (cm *ConnectionManager) Prune(wsConn *WsConnection, err error) { // {{{ cm.logger.Info("websocket", "op", "prune", "uuid", wsConn.UUID) wsConn.Conn.Close() wsConn.Pruned = true + cm.connSync.Lock() delete(cm.connections, wsConn.UUID) + cm.connSync.Unlock() } // }}} func (cm *ConnectionManager) ReadLoop(wsConn *WsConnection) { // {{{ var data []byte From f28b5188a6a3b03a8e37e95ab7ceb2471bfaa72c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Thu, 15 Feb 2024 13:25:58 +0100 Subject: [PATCH 2/2] Create user without confilct --- database/pkg.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/database/pkg.go b/database/pkg.go index efa3cc8..fc7b0fc 100644 --- a/database/pkg.go +++ b/database/pkg.go @@ -274,6 +274,8 @@ func (db *T) CreateUser(username, password, name string) (userID int64, err erro ), $3 ) + ON CONFLICT (username) DO UPDATE + SET username = EXCLUDED.username RETURNING id `, username,