Skip to content

Commit 55f4cae

Browse files
committed
Reset internal buffer when rows.Close() is called.
Fixes #903
1 parent c0f6b44 commit 55f4cae

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

buffer.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ func newBuffer(nc net.Conn) buffer {
3737
}
3838
}
3939

40+
// discard trims b.buf[:b.idx] to prohibit it reused.
41+
//
42+
// This is required by Rows.Close().
43+
// See https://github.com/golang/go/commit/651ddbdb5056ded455f47f9c494c67b389622a47
44+
func (b *buffer) discard() {
45+
if len(b.buf)-b.idx >= defaultBufSize {
46+
b.buf = b.buf[b.idx:]
47+
b.idx = 0
48+
return
49+
}
50+
51+
bufSize := defaultBufSize
52+
if bufSize < b.length {
53+
bufSize = b.length
54+
}
55+
newBuf := make([]byte, bufSize)
56+
copy(newBuf, b.buf[b.idx:b.idx+b.length])
57+
b.buf = newBuf
58+
b.idx = 0
59+
}
60+
4061
// fill reads into the buffer until at least _need_ bytes are in it
4162
func (b *buffer) fill(need int) error {
4263
n := b.length

rows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ func (rows *mysqlRows) Close() (err error) {
111111
return err
112112
}
113113

114+
// We can't reuse receive buffer when rows.Close() is called.
115+
// See https://github.com/golang/go/commit/651ddbdb5056ded455f47f9c494c67b389622a47
116+
mc.buf.discard()
117+
114118
// Remove unread packets from stream
115119
if !rows.rs.done {
116120
err = mc.readUntilEOF()

0 commit comments

Comments
 (0)