Added mutex locking around modifications to websocket connections
This commit is contained in:
parent
92c8ac444f
commit
77f15a197a
@ -10,6 +10,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ReadHandler func(*ConnectionManager, *WsConnection, []byte)
|
type ReadHandler func(*ConnectionManager, *WsConnection, []byte)
|
||||||
@ -28,6 +29,7 @@ type ConnectionManager struct {
|
|||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
domains []string
|
domains []string
|
||||||
readHandlers []ReadHandler
|
readHandlers []ReadHandler
|
||||||
|
connSync sync.Mutex
|
||||||
}
|
}
|
||||||
type SendRequest struct {
|
type SendRequest struct {
|
||||||
WsConn *WsConnection
|
WsConn *WsConnection
|
||||||
@ -67,7 +69,9 @@ func (cm *ConnectionManager) NewConnection(w http.ResponseWriter, r *http.Reques
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Keep track of all connections.
|
// Keep track of all connections.
|
||||||
|
cm.connSync.Lock()
|
||||||
cm.connections[wsConn.UUID] = &wsConn
|
cm.connections[wsConn.UUID] = &wsConn
|
||||||
|
cm.connSync.Unlock()
|
||||||
|
|
||||||
// Successfully upgraded to a websocket connection.
|
// Successfully upgraded to a websocket connection.
|
||||||
cm.logger.Info("websocket", "uuid", wsConn.UUID, "remote_addr", r.RemoteAddr)
|
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)
|
cm.logger.Info("websocket", "op", "prune", "uuid", wsConn.UUID)
|
||||||
wsConn.Conn.Close()
|
wsConn.Conn.Close()
|
||||||
wsConn.Pruned = true
|
wsConn.Pruned = true
|
||||||
|
cm.connSync.Lock()
|
||||||
delete(cm.connections, wsConn.UUID)
|
delete(cm.connections, wsConn.UUID)
|
||||||
|
cm.connSync.Unlock()
|
||||||
} // }}}
|
} // }}}
|
||||||
func (cm *ConnectionManager) ReadLoop(wsConn *WsConnection) { // {{{
|
func (cm *ConnectionManager) ReadLoop(wsConn *WsConnection) { // {{{
|
||||||
var data []byte
|
var data []byte
|
||||||
|
Loading…
Reference in New Issue
Block a user