Skip to content

Commit 5853568

Browse files
committed
Backend: use a custom type for storing WebSocket connections.
1 parent c6be726 commit 5853568

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

backend/server/mux.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ type ServeMux struct {
66
http.ServeMux
77

88
session sessionHandler
9-
ws wsHandler
9+
ws *wsHandler
1010
}
1111

1212
func NewServeMux() *ServeMux {
13-
mux := ServeMux{}
13+
mux := ServeMux{
14+
ws: newWsHandler(),
15+
}
1416
mux.Handle("/session", &mux.session)
15-
mux.Handle("GET /ws", &mux.ws)
17+
mux.Handle("GET /ws", mux.ws)
1618
return &mux
1719
}
1820

backend/server/ws.go

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

42+
type connections struct {
43+
data map[*connection]bool
44+
mutex sync.Mutex
45+
}
46+
47+
func newConnections() connections {
48+
return connections{data: make(map[*connection]bool)}
49+
}
50+
51+
func (connections *connections) store(key *connection, value bool) {
52+
connections.mutex.Lock()
53+
connections.data[key] = value
54+
connections.mutex.Unlock()
55+
}
56+
57+
func (connections *connections) delete(key *connection) {
58+
connections.mutex.Lock()
59+
delete(connections.data, key)
60+
connections.mutex.Unlock()
61+
}
62+
63+
func (connections *connections) close() {
64+
connections.mutex.Lock()
65+
defer connections.mutex.Unlock()
66+
67+
for connection := range connections.data {
68+
connection.Close(websocket.StatusNormalClosure, "server shutting down")
69+
}
70+
}
71+
4272
type wsHandler struct {
43-
connections sync.Map
73+
connections connections
4474
waitGroup sync.WaitGroup
4575
}
4676

77+
func newWsHandler() *wsHandler {
78+
return &wsHandler{
79+
connections: newConnections(),
80+
}
81+
}
82+
4783
func (handler *wsHandler) handle(writer http.ResponseWriter, request *http.Request) error {
4884
defer handler.waitGroup.Done()
4985

@@ -56,8 +92,8 @@ func (handler *wsHandler) handle(writer http.ResponseWriter, request *http.Reque
5692
connection := connection{conn, writer, request}
5793
defer connection.CloseNow()
5894

59-
handler.connections.Store(connection, true)
60-
defer handler.connections.Delete(connection)
95+
handler.connections.store(&connection, true)
96+
defer handler.connections.delete(&connection)
6197

6298
log.Printf("opened a connection with %v", request.RemoteAddr)
6399

@@ -98,14 +134,6 @@ func (handler *wsHandler) ServeHTTP(writer http.ResponseWriter, request *http.Re
98134
}
99135

100136
func (handler *wsHandler) close() {
101-
for key, _ := range handler.connections.Range {
102-
connection, ok := key.(connection)
103-
if !ok {
104-
return
105-
}
106-
107-
connection.Close(websocket.StatusNormalClosure, "server shutting down")
108-
}
109-
137+
handler.connections.close()
110138
handler.waitGroup.Wait()
111139
}

0 commit comments

Comments
 (0)