Skip to content

Commit 38536a6

Browse files
author
xiaobiao
committed
fix bug of caching reused buffer
1 parent b6a713c commit 38536a6

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

packet/conn.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,26 @@ func (c *Conn) ReadPacketReuseMem(dst []byte) ([]byte, error) {
9999
}
100100

101101
readBytes := buf.Bytes()
102-
readSize := len(readBytes) + len(dst)
103-
result := make([]byte, 0, readSize)
102+
readSize := len(readBytes)
103+
var result []byte
104104
if len(dst) > 0 {
105-
result = append(result, dst...)
106-
result = append(result, readBytes...)
105+
result = append(dst, readBytes...)
106+
// if read block is big, use read block as result and do not cache buf any more
107+
if readSize > utils.TooBigBlockSize {
108+
buf = nil
109+
}
107110

108111
} else {
109-
if readSize <= utils.TooBigBlockSize {
110-
result = append(result, readBytes...)
111-
} else {
112-
// if read block is big, use read block as result and do not cache buf any more
112+
if readSize > utils.TooBigBlockSize {
113+
// if read block is big, use read block as result and do not cache buf any more, and do not copy read bytes
113114
result = readBytes
114115
buf = nil
116+
117+
} else {
118+
result = append(dst, readBytes...)
115119
}
116120
}
121+
117122
return result, nil
118123
}
119124

0 commit comments

Comments
 (0)