1 // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
3 // Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this file,
7 // You can obtain one at http://mozilla.org/MPL/2.0/.
17 const defaultBufSize = 4096
19 // A buffer which is used for both reading and writing.
20 // This is possible since communication on each connection is synchronous.
21 // In other words, we can't write and read simultaneously on the same connection.
22 // The buffer is similar to bufio.Reader / Writer but zero-copy-ish
23 // Also highly optimized for this particular use case.
32 func newBuffer(nc net.Conn) buffer {
33 var b [defaultBufSize]byte
40 // fill reads into the buffer until at least _need_ bytes are in it
41 func (b *buffer) fill(need int) error {
44 // move existing data to the beginning
45 if n > 0 && b.idx > 0 {
46 copy(b.buf[0:n], b.buf[b.idx:])
49 // grow buffer if necessary
50 // TODO: let the buffer shrink again at some point
51 // Maybe keep the org buf slice and swap back?
52 if need > len(b.buf) {
53 // Round up to the next multiple of the default size
54 newBuf := make([]byte, ((need/defaultBufSize)+1)*defaultBufSize)
63 if err := b.nc.SetReadDeadline(time.Now().Add(b.timeout)); err != nil {
68 nn, err := b.nc.Read(b.buf[n:])
84 return io.ErrUnexpectedEOF
92 // returns next N bytes from buffer.
93 // The returned slice is only guaranteed to be valid until the next read
94 func (b *buffer) readNext(need int) ([]byte, error) {
97 if err := b.fill(need); err != nil {
105 return b.buf[offset:b.idx], nil
108 // returns a buffer with the requested size.
109 // If possible, a slice from the existing buffer is returned.
110 // Otherwise a bigger buffer is made.
111 // Only one buffer (total) can be used at a time.
112 func (b *buffer) takeBuffer(length int) []byte {
117 // test (cheap) general case first
118 if length <= defaultBufSize || length <= cap(b.buf) {
119 return b.buf[:length]
122 if length < maxPacketSize {
123 b.buf = make([]byte, length)
126 return make([]byte, length)
129 // shortcut which can be used if the requested buffer is guaranteed to be
130 // smaller than defaultBufSize
131 // Only one buffer (total) can be used at a time.
132 func (b *buffer) takeSmallBuffer(length int) []byte {
136 return b.buf[:length]
139 // takeCompleteBuffer returns the complete existing buffer.
140 // This can be used if the necessary buffer size is unknown.
141 // Only one buffer (total) can be used at a time.
142 func (b *buffer) takeCompleteBuffer() []byte {