From 90241521077b3d673d5d64276ec035ca54476f12 Mon Sep 17 00:00:00 2001 From: lirui Date: Sun, 18 Aug 2024 10:25:23 +0800 Subject: [PATCH] fix: read lock -> write lock --- znet/ws_connection.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/znet/ws_connection.go b/znet/ws_connection.go index d6d1e125..59eb0b07 100644 --- a/znet/ws_connection.go +++ b/znet/ws_connection.go @@ -54,7 +54,7 @@ type WsConnection struct { // msgLock is used for locking when users send and receive messages. // (用户收发消息的Lock) - msgLock sync.RWMutex + msgLock sync.Mutex // property is the connection attribute. (链接属性) property map[string]interface{} @@ -338,8 +338,8 @@ func (c *WsConnection) LocalAddr() net.Addr { } func (c *WsConnection) Send(data []byte) error { - c.msgLock.RLock() - defer c.msgLock.RUnlock() + c.msgLock.Lock() + defer c.msgLock.Unlock() if c.isClosed == true { return errors.New("WsConnection closed when send msg") } @@ -354,8 +354,8 @@ func (c *WsConnection) Send(data []byte) error { } func (c *WsConnection) SendToQueue(data []byte) error { - c.msgLock.RLock() - defer c.msgLock.RUnlock() + c.msgLock.Lock() + defer c.msgLock.Unlock() if c.msgBuffChan == nil { c.msgBuffChan = make(chan []byte, zconf.GlobalObject.MaxMsgChanLen) @@ -389,8 +389,8 @@ func (c *WsConnection) SendToQueue(data []byte) error { // SendMsg directly sends the Message data to the remote TCP client. // (直接将Message数据发送数据给远程的TCP客户端) func (c *WsConnection) SendMsg(msgID uint32, data []byte) error { - c.msgLock.RLock() - defer c.msgLock.RUnlock() + c.msgLock.Lock() + defer c.msgLock.Unlock() if c.isClosed == true { return errors.New("WsConnection closed when send msg") } @@ -415,8 +415,8 @@ func (c *WsConnection) SendMsg(msgID uint32, data []byte) error { // SendBuffMsg sends BuffMsg func (c *WsConnection) SendBuffMsg(msgID uint32, data []byte) error { - c.msgLock.RLock() - defer c.msgLock.RUnlock() + c.msgLock.Lock() + defer c.msgLock.Unlock() if c.msgBuffChan == nil { c.msgBuffChan = make(chan []byte, zconf.GlobalObject.MaxMsgChanLen)