Skip to content

Commit

Permalink
修改下锁的保护范围
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Aug 13, 2023
1 parent 6eb46a0 commit 4f1965c
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,19 +569,19 @@ func (c *Conn) WriteMessageDelay(op Opcode, writeBuf []byte) (err error) {
writeBuf = out.Bytes()
}

c.delayMu.Lock()
// 初始化缓存
if c.delayBuf == nil && c.delayWriteInitBufferSize > 0 {

c.delayMu.Lock()
// TODO sync.Pool管理下, 如果size是1k 2k 3k
delayBuf := make([]byte, 0, c.delayWriteInitBufferSize)
c.delayBuf = bytes.NewBuffer(delayBuf)
c.delayMu.Unlock()
}
// 初始化定时器
if c.delayTimeout == nil && c.maxDelayWriteDuration > 0 {
c.delayTimeout = time.AfterFunc(c.maxDelayWriteDuration, c.writerDelayBufSafe)
}
c.delayMu.Unlock()

maskValue := uint32(0)
if c.client {
Expand All @@ -595,12 +595,9 @@ func (c *Conn) WriteMessageDelay(op Opcode, writeBuf []byte) (err error) {
c.delayMu.Unlock()
return err
}
c.delayMu.Unlock()

// go func() {
// 为了平衡生产者,消费者的速度,这里不再使用协程
// 为了平衡生产者,消费者的速度,这里不使用协程

c.delayMu.Lock()
if c.delayBuf != nil {
frame.WriteFrameToBytes(c.delayBuf, writeBuf, true, rsv1, c.client, op, maskValue)
}
Expand Down

0 comments on commit 4f1965c

Please sign in to comment.