Skip to content

Commit

Permalink
Merge pull request #333 from aceld/fix/websocket-concurrent-write
Browse files Browse the repository at this point in the history
fix: read lock -> write lock
  • Loading branch information
aceld authored Aug 19, 2024
2 parents 3f22625 + 9024152 commit 1b464c2
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions znet/ws_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down

0 comments on commit 1b464c2

Please sign in to comment.