diff --git a/packet/conn.go b/packet/conn.go index c60f68e66..60de437c4 100644 --- a/packet/conn.go +++ b/packet/conn.go @@ -3,15 +3,14 @@ package packet import ( "bufio" "bytes" - "io" - "net" - "sync" - "crypto/rand" "crypto/rsa" "crypto/sha1" "crypto/x509" "encoding/pem" + "io" + "net" + "sync" . "github.com/go-mysql-org/go-mysql/mysql" "github.com/go-mysql-org/go-mysql/utils" @@ -97,10 +96,28 @@ func (c *Conn) ReadPacketReuseMem(dst []byte) ([]byte, error) { if err := c.ReadPacketTo(buf); err != nil { return nil, errors.Trace(err) + } + + readBytes := buf.Bytes() + readSize := len(readBytes) + var result []byte + if len(dst) > 0 { + result = append(dst, readBytes...) + // if read block is big, do not cache buf any more + if readSize > utils.TooBigBlockSize { + buf = nil + } } else { - result := append(dst, buf.Bytes()...) - return result, nil + if readSize > utils.TooBigBlockSize { + // if read block is big, use read block as result and do not cache buf any more + result = readBytes + buf = nil + } else { + result = append(dst, readBytes...) + } } + + return result, nil } func (c *Conn) copyN(dst io.Writer, src io.Reader, n int64) (written int64, err error) { diff --git a/utils/bytes_buffer_pool.go b/utils/bytes_buffer_pool.go index a1ca8707d..fe0066ed2 100644 --- a/utils/bytes_buffer_pool.go +++ b/utils/bytes_buffer_pool.go @@ -5,6 +5,10 @@ import ( "sync" ) +const ( + TooBigBlockSize = 1024 * 1024 * 4 +) + var ( bytesBufferPool = sync.Pool{ New: func() interface{} { @@ -27,6 +31,10 @@ func BytesBufferGet() (data *bytes.Buffer) { } func BytesBufferPut(data *bytes.Buffer) { + if data == nil || len(data.Bytes()) > TooBigBlockSize { + return + } + select { case bytesBufferChan <- data: default: