2016-11-03 16:16:01 -06:00
|
|
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
|
|
|
//
|
|
|
|
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBufSize = 4096
|
|
|
|
|
|
|
|
// A buffer which is used for both reading and writing.
|
|
|
|
// This is possible since communication on each connection is synchronous.
|
|
|
|
// In other words, we can't write and read simultaneously on the same connection.
|
|
|
|
// The buffer is similar to bufio.Reader / Writer but zero-copy-ish
|
|
|
|
// Also highly optimized for this particular use case.
|
|
|
|
type buffer struct {
|
2019-06-23 09:22:43 -06:00
|
|
|
buf []byte
|
2016-11-03 16:16:01 -06:00
|
|
|
nc net.Conn
|
|
|
|
idx int
|
|
|
|
length int
|
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBuffer(nc net.Conn) buffer {
|
2019-06-23 09:22:43 -06:00
|
|
|
var b [defaultBufSize]byte
|
2016-11-03 16:16:01 -06:00
|
|
|
return buffer{
|
2019-06-23 09:22:43 -06:00
|
|
|
buf: b[:],
|
2016-11-03 16:16:01 -06:00
|
|
|
nc: nc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill reads into the buffer until at least _need_ bytes are in it
|
|
|
|
func (b *buffer) fill(need int) error {
|
|
|
|
n := b.length
|
|
|
|
|
|
|
|
// move existing data to the beginning
|
|
|
|
if n > 0 && b.idx > 0 {
|
|
|
|
copy(b.buf[0:n], b.buf[b.idx:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// grow buffer if necessary
|
|
|
|
// TODO: let the buffer shrink again at some point
|
|
|
|
// Maybe keep the org buf slice and swap back?
|
|
|
|
if need > len(b.buf) {
|
|
|
|
// Round up to the next multiple of the default size
|
|
|
|
newBuf := make([]byte, ((need/defaultBufSize)+1)*defaultBufSize)
|
|
|
|
copy(newBuf, b.buf)
|
|
|
|
b.buf = newBuf
|
|
|
|
}
|
|
|
|
|
|
|
|
b.idx = 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
if b.timeout > 0 {
|
|
|
|
if err := b.nc.SetReadDeadline(time.Now().Add(b.timeout)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nn, err := b.nc.Read(b.buf[n:])
|
|
|
|
n += nn
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
if n < need {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b.length = n
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case io.EOF:
|
|
|
|
if n >= need {
|
|
|
|
b.length = n
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return io.ErrUnexpectedEOF
|
|
|
|
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns next N bytes from buffer.
|
|
|
|
// The returned slice is only guaranteed to be valid until the next read
|
|
|
|
func (b *buffer) readNext(need int) ([]byte, error) {
|
|
|
|
if b.length < need {
|
|
|
|
// refill
|
|
|
|
if err := b.fill(need); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offset := b.idx
|
|
|
|
b.idx += need
|
|
|
|
b.length -= need
|
|
|
|
return b.buf[offset:b.idx], nil
|
|
|
|
}
|
|
|
|
|
2019-06-23 09:22:43 -06:00
|
|
|
// returns a buffer with the requested size.
|
2016-11-03 16:16:01 -06:00
|
|
|
// If possible, a slice from the existing buffer is returned.
|
|
|
|
// Otherwise a bigger buffer is made.
|
|
|
|
// Only one buffer (total) can be used at a time.
|
2019-06-23 09:22:43 -06:00
|
|
|
func (b *buffer) takeBuffer(length int) []byte {
|
2016-11-03 16:16:01 -06:00
|
|
|
if b.length > 0 {
|
2019-06-23 09:22:43 -06:00
|
|
|
return nil
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// test (cheap) general case first
|
2019-06-23 09:22:43 -06:00
|
|
|
if length <= defaultBufSize || length <= cap(b.buf) {
|
|
|
|
return b.buf[:length]
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if length < maxPacketSize {
|
|
|
|
b.buf = make([]byte, length)
|
2019-06-23 09:22:43 -06:00
|
|
|
return b.buf
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
2019-06-23 09:22:43 -06:00
|
|
|
return make([]byte, length)
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
2019-06-23 09:22:43 -06:00
|
|
|
// shortcut which can be used if the requested buffer is guaranteed to be
|
|
|
|
// smaller than defaultBufSize
|
2016-11-03 16:16:01 -06:00
|
|
|
// Only one buffer (total) can be used at a time.
|
2019-06-23 09:22:43 -06:00
|
|
|
func (b *buffer) takeSmallBuffer(length int) []byte {
|
2018-07-03 15:58:31 -06:00
|
|
|
if b.length > 0 {
|
2019-06-23 09:22:43 -06:00
|
|
|
return nil
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
2019-06-23 09:22:43 -06:00
|
|
|
return b.buf[:length]
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// takeCompleteBuffer returns the complete existing buffer.
|
|
|
|
// This can be used if the necessary buffer size is unknown.
|
|
|
|
// Only one buffer (total) can be used at a time.
|
2019-06-23 09:22:43 -06:00
|
|
|
func (b *buffer) takeCompleteBuffer() []byte {
|
2018-07-03 15:58:31 -06:00
|
|
|
if b.length > 0 {
|
2019-06-23 09:22:43 -06:00
|
|
|
return nil
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
2019-06-23 09:22:43 -06:00
|
|
|
return b.buf
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|