Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Jan 9, 2023
2 parents 072d5c4 + ce1cc41 commit d1f2178
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
vendor/
bin/
examples/debug/
examples/testsuite/config/*.json
examples/testsuite/reports
9 changes: 6 additions & 3 deletions compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ type compressor struct {
fw *flate.Writer
}

// Compress 压缩
func (c *compressor) Compress(content []byte) ([]byte, error) {
func (c *compressor) reset() {
if c.writeBuffer.Cap() > internal.Lv4 {
c.writeBuffer = internal.NewBuffer(nil)
}

c.writeBuffer.Reset()
c.fw.Reset(c.writeBuffer)
}

// Compress 压缩
func (c *compressor) Compress(content []byte) ([]byte, error) {
c.reset()
if err := writeN(c.fw, content, len(content)); err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func (c *Conn) WriteMessage(opcode Opcode, payload []byte) {
}

func (c *Conn) writeMessage(opcode Opcode, payload []byte) error {
c.wmu.Lock()
defer c.wmu.Unlock()

var enableCompress = c.compressEnabled && opcode.IsDataFrame()
if enableCompress {
compressedContent, err := c.compressor.Compress(payload)
Expand All @@ -55,9 +58,6 @@ func (c *Conn) writeMessage(opcode Opcode, payload []byte) error {
// 加锁是为了防止frame header和payload并发写入后乱序
// write a websocket frame, content is prepared
func (c *Conn) writeFrame(opcode Opcode, payload []byte, enableCompress bool) error {
c.wmu.Lock()
defer c.wmu.Unlock()

var header = frameHeader{}
var n = len(payload)
var headerLength = header.GenerateServerHeader(true, enableCompress, opcode, n)
Expand Down

0 comments on commit d1f2178

Please sign in to comment.