From b6a713c1a33e1dc77fa8dc5b4dc101954ea947a3 Mon Sep 17 00:00:00 2001 From: xiaobiao Date: Fri, 10 Dec 2021 11:31:11 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=AF=BB=E5=8F=96=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E5=8C=85=E5=86=85=E5=AD=98=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packet/conn.go | 26 ++++++++++++++++++++------ utils/bytes_buffer_pool.go | 8 ++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/packet/conn.go b/packet/conn.go index c60f68e66..deed85f41 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,25 @@ 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) + len(dst) + result := make([]byte, 0, readSize) + if len(dst) > 0 { + result = append(result, dst...) + result = append(result, readBytes...) + } else { - result := append(dst, buf.Bytes()...) - return result, nil + 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 + result = readBytes + buf = nil + } } + 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: From dee5b730d42048fd28119716a89d15f76b226f99 Mon Sep 17 00:00:00 2001 From: xiaobiao Date: Sat, 11 Dec 2021 12:59:21 +0800 Subject: [PATCH 2/3] fix bug of caching reused buffer --- packet/conn.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packet/conn.go b/packet/conn.go index deed85f41..e3c2203fc 100644 --- a/packet/conn.go +++ b/packet/conn.go @@ -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 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 } From 1719d546f1b03c7855b222e75275a878f64fdeae Mon Sep 17 00:00:00 2001 From: xiaobiao Date: Wed, 15 Dec 2021 12:45:06 +0800 Subject: [PATCH 3/3] fix golangci warnings --- packet/conn.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/packet/conn.go b/packet/conn.go index e3c2203fc..60de437c4 100644 --- a/packet/conn.go +++ b/packet/conn.go @@ -107,13 +107,11 @@ func (c *Conn) ReadPacketReuseMem(dst []byte) ([]byte, error) { if readSize > utils.TooBigBlockSize { buf = nil } - } else { 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...) }