Skip to content

Commit a5dd227

Browse files
committed
Use a seperat mutexes for write
1 parent bc60275 commit a5dd227

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

ship/websocket.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ type websocketConnection struct {
2626
shipWriteChannel chan []byte
2727

2828
// internal handling of closed connections
29-
isConnectionClosed bool
29+
connectionClosed bool
3030

3131
remoteSki string
3232

33-
mux sync.Mutex
34-
shutdownOnce sync.Once
33+
muxConnClosed sync.Mutex
34+
muxShipWrite sync.Mutex
35+
shutdownOnce sync.Once
3536
}
3637

3738
// create a new websocket based shipDataProcessing implementation
@@ -44,10 +45,18 @@ func NewWebsocketConnection(conn *websocket.Conn, remoteSki string) *websocketCo
4445

4546
// check if the websocket connection is closed
4647
func (w *websocketConnection) isConnClosed() bool {
47-
w.mux.Lock()
48-
defer w.mux.Unlock()
48+
w.muxConnClosed.Lock()
49+
defer w.muxConnClosed.Unlock()
4950

50-
return w.isConnectionClosed
51+
return w.connectionClosed
52+
}
53+
54+
// check if the websocket connection is closed
55+
func (w *websocketConnection) setConnClosed() {
56+
w.muxConnClosed.Lock()
57+
defer w.muxConnClosed.Unlock()
58+
59+
w.connectionClosed = true
5160
}
5261

5362
func (w *websocketConnection) run() {
@@ -161,7 +170,9 @@ func (w *websocketConnection) close() {
161170
return
162171
}
163172

164-
w.mux.Lock()
173+
w.setConnClosed()
174+
175+
w.muxShipWrite.Lock()
165176

166177
if !util.IsChannelClosed(w.closeChannel) {
167178
close(w.closeChannel)
@@ -177,9 +188,7 @@ func (w *websocketConnection) close() {
177188
w.conn.Close()
178189
}
179190

180-
w.isConnectionClosed = true
181-
182-
w.mux.Unlock()
191+
w.muxShipWrite.Unlock()
183192
})
184193
}
185194

@@ -197,8 +206,8 @@ func (w *websocketConnection) WriteMessageToDataConnection(message []byte) error
197206
return errors.New("connection is closed")
198207
}
199208

200-
w.mux.Lock()
201-
defer w.mux.Unlock()
209+
w.muxShipWrite.Lock()
210+
defer w.muxShipWrite.Unlock()
202211

203212
if w.conn == nil || w.shipWriteChannel == nil {
204213
return errors.New("connection is closed")

0 commit comments

Comments
 (0)