Skip to content

Commit

Permalink
fix bug of caching reused buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaobiao committed Dec 11, 2021
1 parent b6a713c commit f136121
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packet/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,26 @@ func (c *Conn) ReadPacketReuseMem(dst []byte) ([]byte, error) {
}

readBytes := buf.Bytes()
readSize := len(readBytes) + len(dst)
result := make([]byte, 0, readSize)
readSize := len(readBytes)
var result []byte
if len(dst) > 0 {
result = append(result, dst...)
result = append(result, readBytes...)
result = append(dst, readBytes...)
// if read block is big, do not cache buf any more
if readSize > utils.TooBigBlockSize {
buf = nil
}

} else {
if readSize <= utils.TooBigBlockSize {
result = append(result, readBytes...)
} else {
// if read block is big, use read block as result and do not cache buf any more
if readSize > utils.TooBigBlockSize {
// if read block is big, use read block as result and do not cache buf any more, and do not copy read bytes
result = readBytes
buf = nil

} else {
result = append(dst, readBytes...)
}
}

return result, nil
}

Expand Down

0 comments on commit f136121

Please sign in to comment.