forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-sql-driver/mysql/buffer.go

308 lines
4.9 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This Source Code Form is subject to the terms of the Mozilla Public
2024-02-18 10:42:21 +00:00
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
2024-02-18 10:42:21 +00:00
// You can obtain one at http://mozilla.org/MPL/2.0/.
package mysql
import (
"io"
"net"
"time"
)
const defaultBufSize = 4096
2024-02-18 10:42:21 +00:00
const maxCachedBufSize = 256 * 1024
// A buffer which is used for both reading and writing.
2024-02-18 10:42:21 +00:00
// This is possible since communication on each connection is synchronous.
2024-02-18 10:42:21 +00:00
// In other words, we can't write and read simultaneously on the same connection.
2024-02-18 10:42:21 +00:00
// The buffer is similar to bufio.Reader / Writer but zero-copy-ish
2024-02-18 10:42:21 +00:00
// Also highly optimized for this particular use case.
2024-02-18 10:42:21 +00:00
// This buffer is backed by two byte slices in a double-buffering scheme
2024-02-18 10:42:21 +00:00
type buffer struct {
buf []byte // buf is a byte buffer who's length and capacity are equal.
nc net.Conn
idx int
length int
2024-02-18 10:42:21 +00:00
timeout time.Duration
dbuf [2][]byte // dbuf is an array with the two byte slices that back this buffer
flipcnt uint // flipccnt is the current buffer counter for double-buffering
2024-02-18 10:42:21 +00:00
}
// newBuffer allocates and returns a new buffer.
2024-02-18 10:42:21 +00:00
func newBuffer(nc net.Conn) buffer {
2024-02-18 10:42:21 +00:00
fg := make([]byte, defaultBufSize)
2024-02-18 10:42:21 +00:00
return buffer{
buf: fg,
nc: nc,
2024-02-18 10:42:21 +00:00
dbuf: [2][]byte{fg, nil},
}
2024-02-18 10:42:21 +00:00
}
// flip replaces the active buffer with the background buffer
2024-02-18 10:42:21 +00:00
// this is a delayed flip that simply increases the buffer counter;
2024-02-18 10:42:21 +00:00
// the actual flip will be performed the next time we call `buffer.fill`
2024-02-18 10:42:21 +00:00
func (b *buffer) flip() {
2024-02-18 10:42:21 +00:00
b.flipcnt += 1
2024-02-18 10:42:21 +00:00
}
// fill reads into the buffer until at least _need_ bytes are in it
2024-02-18 10:42:21 +00:00
func (b *buffer) fill(need int) error {
2024-02-18 10:42:21 +00:00
n := b.length
2024-02-18 10:42:21 +00:00
// fill data into its double-buffering target: if we've called
2024-02-18 10:42:21 +00:00
// flip on this buffer, we'll be copying to the background buffer,
2024-02-18 10:42:21 +00:00
// and then filling it with network data; otherwise we'll just move
2024-02-18 10:42:21 +00:00
// the contents of the current buffer to the front before filling it
2024-02-18 10:42:21 +00:00
dest := b.dbuf[b.flipcnt&1]
// grow buffer if necessary to fit the whole packet.
2024-02-18 10:42:21 +00:00
if need > len(dest) {
2024-02-18 10:42:21 +00:00
// Round up to the next multiple of the default size
2024-02-18 10:42:21 +00:00
dest = make([]byte, ((need/defaultBufSize)+1)*defaultBufSize)
// if the allocated buffer is not too large, move it to backing storage
2024-02-18 10:42:21 +00:00
// to prevent extra allocations on applications that perform large reads
2024-02-18 10:42:21 +00:00
if len(dest) <= maxCachedBufSize {
2024-02-18 10:42:21 +00:00
b.dbuf[b.flipcnt&1] = dest
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// if we're filling the fg buffer, move the existing data to the start of it.
2024-02-18 10:42:21 +00:00
// if we're filling the bg buffer, copy over the data
2024-02-18 10:42:21 +00:00
if n > 0 {
2024-02-18 10:42:21 +00:00
copy(dest[:n], b.buf[b.idx:])
2024-02-18 10:42:21 +00:00
}
b.buf = dest
2024-02-18 10:42:21 +00:00
b.idx = 0
for {
2024-02-18 10:42:21 +00:00
if b.timeout > 0 {
2024-02-18 10:42:21 +00:00
if err := b.nc.SetReadDeadline(time.Now().Add(b.timeout)); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
nn, err := b.nc.Read(b.buf[n:])
2024-02-18 10:42:21 +00:00
n += nn
switch err {
2024-02-18 10:42:21 +00:00
case nil:
2024-02-18 10:42:21 +00:00
if n < need {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
b.length = n
2024-02-18 10:42:21 +00:00
return nil
case io.EOF:
2024-02-18 10:42:21 +00:00
if n >= need {
2024-02-18 10:42:21 +00:00
b.length = n
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return io.ErrUnexpectedEOF
default:
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// returns next N bytes from buffer.
2024-02-18 10:42:21 +00:00
// The returned slice is only guaranteed to be valid until the next read
2024-02-18 10:42:21 +00:00
func (b *buffer) readNext(need int) ([]byte, error) {
2024-02-18 10:42:21 +00:00
if b.length < need {
2024-02-18 10:42:21 +00:00
// refill
2024-02-18 10:42:21 +00:00
if err := b.fill(need); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
offset := b.idx
2024-02-18 10:42:21 +00:00
b.idx += need
2024-02-18 10:42:21 +00:00
b.length -= need
2024-02-18 10:42:21 +00:00
return b.buf[offset:b.idx], nil
2024-02-18 10:42:21 +00:00
}
// takeBuffer returns a buffer with the requested size.
2024-02-18 10:42:21 +00:00
// If possible, a slice from the existing buffer is returned.
2024-02-18 10:42:21 +00:00
// Otherwise a bigger buffer is made.
2024-02-18 10:42:21 +00:00
// Only one buffer (total) can be used at a time.
2024-02-18 10:42:21 +00:00
func (b *buffer) takeBuffer(length int) ([]byte, error) {
2024-02-18 10:42:21 +00:00
if b.length > 0 {
2024-02-18 10:42:21 +00:00
return nil, ErrBusyBuffer
2024-02-18 10:42:21 +00:00
}
// test (cheap) general case first
2024-02-18 10:42:21 +00:00
if length <= cap(b.buf) {
2024-02-18 10:42:21 +00:00
return b.buf[:length], nil
2024-02-18 10:42:21 +00:00
}
if length < maxPacketSize {
2024-02-18 10:42:21 +00:00
b.buf = make([]byte, length)
2024-02-18 10:42:21 +00:00
return b.buf, nil
2024-02-18 10:42:21 +00:00
}
// buffer is larger than we want to store.
2024-02-18 10:42:21 +00:00
return make([]byte, length), nil
2024-02-18 10:42:21 +00:00
}
// takeSmallBuffer is shortcut which can be used if length is
2024-02-18 10:42:21 +00:00
// known to be smaller than defaultBufSize.
2024-02-18 10:42:21 +00:00
// Only one buffer (total) can be used at a time.
2024-02-18 10:42:21 +00:00
func (b *buffer) takeSmallBuffer(length int) ([]byte, error) {
2024-02-18 10:42:21 +00:00
if b.length > 0 {
2024-02-18 10:42:21 +00:00
return nil, ErrBusyBuffer
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return b.buf[:length], nil
2024-02-18 10:42:21 +00:00
}
// takeCompleteBuffer returns the complete existing buffer.
2024-02-18 10:42:21 +00:00
// This can be used if the necessary buffer size is unknown.
2024-02-18 10:42:21 +00:00
// cap and len of the returned buffer will be equal.
2024-02-18 10:42:21 +00:00
// Only one buffer (total) can be used at a time.
2024-02-18 10:42:21 +00:00
func (b *buffer) takeCompleteBuffer() ([]byte, error) {
2024-02-18 10:42:21 +00:00
if b.length > 0 {
2024-02-18 10:42:21 +00:00
return nil, ErrBusyBuffer
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return b.buf, nil
2024-02-18 10:42:21 +00:00
}
// store stores buf, an updated buffer, if its suitable to do so.
2024-02-18 10:42:21 +00:00
func (b *buffer) store(buf []byte) error {
2024-02-18 10:42:21 +00:00
if b.length > 0 {
2024-02-18 10:42:21 +00:00
return ErrBusyBuffer
2024-02-18 10:42:21 +00:00
} else if cap(buf) <= maxPacketSize && cap(buf) > cap(b.buf) {
2024-02-18 10:42:21 +00:00
b.buf = buf[:cap(buf)]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}