|
| 1 | +package mysql |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +const defaultBufSize = 4096 |
| 10 | +const maxCachedBufSize = 256 * 1024 |
| 11 | + |
| 12 | +// A buffer which is used for both reading and writing. |
| 13 | +// This is possible since communication on each connection is synchronous. |
| 14 | +// In other words, we can't write and read simultaneously on the same connection. |
| 15 | +// The buffer is similar to bufio.Reader / Writer but zero-copy-ish |
| 16 | +// Also highly optimized for this particular use case. |
| 17 | +// This buffer is backed by two byte slices in a double-buffering scheme |
| 18 | +type buffer struct { |
| 19 | + buf []byte // buf is a byte buffer who's length and capacity are equal. |
| 20 | + nc net.Conn |
| 21 | + idx int |
| 22 | + length int |
| 23 | + timeout time.Duration |
| 24 | + dbuf [2][]byte // dbuf is an array with the two byte slices that back this buffer |
| 25 | + flipcnt uint // flipccnt is the current buffer counter for double-buffering |
| 26 | +} |
| 27 | + |
| 28 | +// newBuffer allocates and returns a new buffer. |
| 29 | +func newBuffer(nc net.Conn) buffer { |
| 30 | + fg := make([]byte, defaultBufSize) |
| 31 | + return buffer{ |
| 32 | + buf: fg, |
| 33 | + nc: nc, |
| 34 | + dbuf: [2][]byte{fg, nil}, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// flip replaces the active buffer with the background buffer |
| 39 | +// this is a delayed flip that simply increases the buffer counter; |
| 40 | +// the actual flip will be performed the next time we call `buffer.fill` |
| 41 | +func (b *buffer) flip() { |
| 42 | + b.flipcnt += 1 |
| 43 | +} |
| 44 | + |
| 45 | +// fill reads into the buffer until at least _need_ bytes are in it |
| 46 | +func (b *buffer) fill(need int) error { |
| 47 | + n := b.length |
| 48 | + // fill data into its double-buffering target: if we've called |
| 49 | + // flip on this buffer, we'll be copying to the background buffer, |
| 50 | + // and then filling it with network data; otherwise we'll just move |
| 51 | + // the contents of the current buffer to the front before filling it |
| 52 | + dest := b.dbuf[b.flipcnt&1] |
| 53 | + |
| 54 | + // grow buffer if necessary to fit the whole packet. |
| 55 | + if need > len(dest) { |
| 56 | + // Round up to the next multiple of the default size |
| 57 | + dest = make([]byte, ((need/defaultBufSize)+1)*defaultBufSize) |
| 58 | + |
| 59 | + // if the allocated buffer is not too large, move it to backing storage |
| 60 | + // to prevent extra allocations on applications that perform large reads |
| 61 | + if len(dest) <= maxCachedBufSize { |
| 62 | + b.dbuf[b.flipcnt&1] = dest |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // if we're filling the fg buffer, move the existing data to the start of it. |
| 67 | + // if we're filling the bg buffer, copy over the data |
| 68 | + if n > 0 { |
| 69 | + copy(dest[:n], b.buf[b.idx:]) |
| 70 | + } |
| 71 | + |
| 72 | + b.buf = dest |
| 73 | + b.idx = 0 |
| 74 | + |
| 75 | + for { |
| 76 | + if b.timeout > 0 { |
| 77 | + if err := b.nc.SetReadDeadline(time.Now().Add(b.timeout)); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + nn, err := b.nc.Read(b.buf[n:]) |
| 83 | + n += nn |
| 84 | + |
| 85 | + switch err { |
| 86 | + case nil: |
| 87 | + if n < need { |
| 88 | + continue |
| 89 | + } |
| 90 | + b.length = n |
| 91 | + return nil |
| 92 | + |
| 93 | + case io.EOF: |
| 94 | + if n >= need { |
| 95 | + b.length = n |
| 96 | + return nil |
| 97 | + } |
| 98 | + return io.ErrUnexpectedEOF |
| 99 | + |
| 100 | + default: |
| 101 | + return err |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// returns next N bytes from buffer. |
| 107 | +// The returned slice is only guaranteed to be valid until the next read |
| 108 | +func (b *buffer) readNext(need int) ([]byte, error) { |
| 109 | + if b.length < need { |
| 110 | + // refill |
| 111 | + if err := b.fill(need); err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + offset := b.idx |
| 117 | + b.idx += need |
| 118 | + b.length -= need |
| 119 | + return b.buf[offset:b.idx], nil |
| 120 | +} |
| 121 | + |
| 122 | +// takeBuffer returns a buffer with the requested size. |
| 123 | +// If possible, a slice from the existing buffer is returned. |
| 124 | +// Otherwise a bigger buffer is made. |
| 125 | +// Only one buffer (total) can be used at a time. |
| 126 | +func (b *buffer) takeBuffer(length int) ([]byte, error) { |
| 127 | + if b.length > 0 { |
| 128 | + return nil, ErrBusyBuffer |
| 129 | + } |
| 130 | + |
| 131 | + // test (cheap) general case first |
| 132 | + if length <= cap(b.buf) { |
| 133 | + return b.buf[:length], nil |
| 134 | + } |
| 135 | + |
| 136 | + if length < maxPacketSize { |
| 137 | + b.buf = make([]byte, length) |
| 138 | + return b.buf, nil |
| 139 | + } |
| 140 | + |
| 141 | + // buffer is larger than we want to store. |
| 142 | + return make([]byte, length), nil |
| 143 | +} |
| 144 | + |
| 145 | +// takeSmallBuffer is shortcut which can be used if length is |
| 146 | +// known to be smaller than defaultBufSize. |
| 147 | +// Only one buffer (total) can be used at a time. |
| 148 | +func (b *buffer) takeSmallBuffer(length int) ([]byte, error) { |
| 149 | + if b.length > 0 { |
| 150 | + return nil, ErrBusyBuffer |
| 151 | + } |
| 152 | + return b.buf[:length], nil |
| 153 | +} |
| 154 | + |
| 155 | +// takeCompleteBuffer returns the complete existing buffer. |
| 156 | +// This can be used if the necessary buffer size is unknown. |
| 157 | +// cap and len of the returned buffer will be equal. |
| 158 | +// Only one buffer (total) can be used at a time. |
| 159 | +func (b *buffer) takeCompleteBuffer() ([]byte, error) { |
| 160 | + if b.length > 0 { |
| 161 | + return nil, ErrBusyBuffer |
| 162 | + } |
| 163 | + return b.buf, nil |
| 164 | +} |
| 165 | + |
| 166 | +// store stores buf, an updated buffer, if its suitable to do so. |
| 167 | +func (b *buffer) store(buf []byte) error { |
| 168 | + if b.length > 0 { |
| 169 | + return ErrBusyBuffer |
| 170 | + } else if cap(buf) <= maxPacketSize && cap(buf) > cap(b.buf) { |
| 171 | + b.buf = buf[:cap(buf)] |
| 172 | + } |
| 173 | + return nil |
| 174 | +} |
0 commit comments