From f52ced59bf123dbcaafac43e72a23a33e48ca13d Mon Sep 17 00:00:00 2001 From: lqqyt2423 <974923609@qq.com> Date: Wed, 6 Jul 2022 20:52:08 +0800 Subject: [PATCH] fix concurrent map writes --- web/conn.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/web/conn.go b/web/conn.go index ec78d47..8d8bea8 100644 --- a/web/conn.go +++ b/web/conn.go @@ -36,15 +36,16 @@ func newConn(c *websocket.Conn) *concurrentConn { } func (c *concurrentConn) trySendConnMessage(f *proxy.Flow) { + c.mu.Lock() + defer c.mu.Unlock() + key := f.ConnContext.Id().String() if send := c.sendConnMessageMap[key]; send { return } c.sendConnMessageMap[key] = true msg := newMessageFlow(messageTypeConn, f) - c.mu.Lock() err := c.conn.WriteMessage(websocket.BinaryMessage, msg.bytes()) - c.mu.Unlock() if err != nil { log.Error(err) return @@ -52,12 +53,13 @@ func (c *concurrentConn) trySendConnMessage(f *proxy.Flow) { } func (c *concurrentConn) whenConnClose(connCtx *proxy.ConnContext) { + c.mu.Lock() + defer c.mu.Unlock() + delete(c.sendConnMessageMap, connCtx.Id().String()) msg := newMessageConnClose(connCtx) - c.mu.Lock() err := c.conn.WriteMessage(websocket.BinaryMessage, msg.bytes()) - c.mu.Unlock() if err != nil { log.Error(err) return