Skip to content

Commit 62d4786

Browse files
committed
Backend: group WebSocket connections per user.
1 parent 767fe04 commit 62d4786

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

backend/server/ws.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,52 @@ func (connection *connection) handleFrame(ctx context.Context) error {
3939
return nil
4040
}
4141

42+
type userConnections = map[*connection]bool
43+
44+
func newUserConnections() userConnections {
45+
return make(map[*connection]bool)
46+
}
47+
4248
type connections struct {
43-
data map[*connection]bool
49+
dict map[db.UserID]userConnections
4450
mutex sync.RWMutex
4551
}
4652

4753
func newConnections() connections {
48-
return connections{data: make(map[*connection]bool)}
54+
return connections{dict: make(map[db.UserID]userConnections)}
4955
}
5056

51-
func (connections *connections) store(key *connection, value bool) {
57+
func (connections *connections) store(userID db.UserID, connection *connection) {
5258
connections.mutex.Lock()
53-
connections.data[key] = value
54-
connections.mutex.Unlock()
59+
defer connections.mutex.Unlock()
60+
61+
if userConnections, ok := connections.dict[userID]; ok {
62+
userConnections[connection] = true
63+
return
64+
}
65+
66+
userConnections := newUserConnections()
67+
userConnections[connection] = true
68+
connections.dict[userID] = userConnections
5569
}
5670

57-
func (connections *connections) delete(key *connection) {
71+
func (connections *connections) delete(userID db.UserID, connection *connection) {
5872
connections.mutex.Lock()
59-
delete(connections.data, key)
60-
connections.mutex.Unlock()
73+
defer connections.mutex.Unlock()
74+
75+
if userConnections, ok := connections.dict[userID]; ok {
76+
delete(userConnections, connection)
77+
}
6178
}
6279

6380
func (connections *connections) close() {
6481
connections.mutex.RLock()
6582
defer connections.mutex.RUnlock()
6683

67-
for connection := range connections.data {
68-
connection.Close(websocket.StatusNormalClosure, "server shutting down")
84+
for _, userConnections := range connections.dict {
85+
for connection := range userConnections {
86+
connection.Close(websocket.StatusNormalClosure, "server shutting down")
87+
}
6988
}
7089
}
7190

@@ -80,7 +99,7 @@ func newWsHandler() *wsHandler {
8099
}
81100
}
82101

83-
func (handler *wsHandler) handle(writer http.ResponseWriter, request *http.Request) error {
102+
func (handler *wsHandler) handle(userID db.UserID, writer http.ResponseWriter, request *http.Request) error {
84103
defer handler.waitGroup.Done()
85104

86105
options := websocket.AcceptOptions{InsecureSkipVerify: true}
@@ -92,8 +111,8 @@ func (handler *wsHandler) handle(writer http.ResponseWriter, request *http.Reque
92111
connection := connection{conn, writer, request}
93112
defer connection.CloseNow()
94113

95-
handler.connections.store(&connection, true)
96-
defer handler.connections.delete(&connection)
114+
handler.connections.store(userID, &connection)
115+
defer handler.connections.delete(userID, &connection)
97116

98117
log.Printf("opened a connection with %v", request.RemoteAddr)
99118

@@ -113,7 +132,7 @@ func (handler *wsHandler) ServeHTTP(writer http.ResponseWriter, request *http.Re
113132
return
114133
}
115134

116-
_, err = db.AuthenticateBySessionKey(sessionKey.Value)
135+
userID, err := db.AuthenticateBySessionKey(sessionKey.Value)
117136
if err != nil {
118137
switch err {
119138
case db.Unathorized:
@@ -126,7 +145,7 @@ func (handler *wsHandler) ServeHTTP(writer http.ResponseWriter, request *http.Re
126145

127146
handler.waitGroup.Add(1)
128147

129-
err = handler.handle(writer, request)
148+
err = handler.handle(userID, writer, request)
130149
if err != nil {
131150
log.Println(err)
132151
return

0 commit comments

Comments
 (0)