You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

buffer.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  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/.
  8. package mysql
  9. import (
  10. "io"
  11. "net"
  12. "time"
  13. )
  14. const defaultBufSize = 4096
  15. // A buffer which is used for both reading and writing.
  16. // This is possible since communication on each connection is synchronous.
  17. // In other words, we can't write and read simultaneously on the same connection.
  18. // The buffer is similar to bufio.Reader / Writer but zero-copy-ish
  19. // Also highly optimized for this particular use case.
  20. type buffer struct {
  21. buf []byte // buf is a byte buffer who's length and capacity are equal.
  22. nc net.Conn
  23. idx int
  24. length int
  25. timeout time.Duration
  26. }
  27. // newBuffer allocates and returns a new buffer.
  28. func newBuffer(nc net.Conn) buffer {
  29. return buffer{
  30. buf: make([]byte, defaultBufSize),
  31. nc: nc,
  32. }
  33. }
  34. // fill reads into the buffer until at least _need_ bytes are in it
  35. func (b *buffer) fill(need int) error {
  36. n := b.length
  37. // move existing data to the beginning
  38. if n > 0 && b.idx > 0 {
  39. copy(b.buf[0:n], b.buf[b.idx:])
  40. }
  41. // grow buffer if necessary
  42. // TODO: let the buffer shrink again at some point
  43. // Maybe keep the org buf slice and swap back?
  44. if need > len(b.buf) {
  45. // Round up to the next multiple of the default size
  46. newBuf := make([]byte, ((need/defaultBufSize)+1)*defaultBufSize)
  47. copy(newBuf, b.buf)
  48. b.buf = newBuf
  49. }
  50. b.idx = 0
  51. for {
  52. if b.timeout > 0 {
  53. if err := b.nc.SetReadDeadline(time.Now().Add(b.timeout)); err != nil {
  54. return err
  55. }
  56. }
  57. nn, err := b.nc.Read(b.buf[n:])
  58. n += nn
  59. switch err {
  60. case nil:
  61. if n < need {
  62. continue
  63. }
  64. b.length = n
  65. return nil
  66. case io.EOF:
  67. if n >= need {
  68. b.length = n
  69. return nil
  70. }
  71. return io.ErrUnexpectedEOF
  72. default:
  73. return err
  74. }
  75. }
  76. }
  77. // returns next N bytes from buffer.
  78. // The returned slice is only guaranteed to be valid until the next read
  79. func (b *buffer) readNext(need int) ([]byte, error) {
  80. if b.length < need {
  81. // refill
  82. if err := b.fill(need); err != nil {
  83. return nil, err
  84. }
  85. }
  86. offset := b.idx
  87. b.idx += need
  88. b.length -= need
  89. return b.buf[offset:b.idx], nil
  90. }
  91. // takeBuffer returns a buffer with the requested size.
  92. // If possible, a slice from the existing buffer is returned.
  93. // Otherwise a bigger buffer is made.
  94. // Only one buffer (total) can be used at a time.
  95. func (b *buffer) takeBuffer(length int) ([]byte, error) {
  96. if b.length > 0 {
  97. return nil, ErrBusyBuffer
  98. }
  99. // test (cheap) general case first
  100. if length <= cap(b.buf) {
  101. return b.buf[:length], nil
  102. }
  103. if length < maxPacketSize {
  104. b.buf = make([]byte, length)
  105. return b.buf, nil
  106. }
  107. // buffer is larger than we want to store.
  108. return make([]byte, length), nil
  109. }
  110. // takeSmallBuffer is shortcut which can be used if length is
  111. // known to be smaller than defaultBufSize.
  112. // Only one buffer (total) can be used at a time.
  113. func (b *buffer) takeSmallBuffer(length int) ([]byte, error) {
  114. if b.length > 0 {
  115. return nil, ErrBusyBuffer
  116. }
  117. return b.buf[:length], nil
  118. }
  119. // takeCompleteBuffer returns the complete existing buffer.
  120. // This can be used if the necessary buffer size is unknown.
  121. // cap and len of the returned buffer will be equal.
  122. // Only one buffer (total) can be used at a time.
  123. func (b *buffer) takeCompleteBuffer() ([]byte, error) {
  124. if b.length > 0 {
  125. return nil, ErrBusyBuffer
  126. }
  127. return b.buf, nil
  128. }
  129. // store stores buf, an updated buffer, if its suitable to do so.
  130. func (b *buffer) store(buf []byte) error {
  131. if b.length > 0 {
  132. return ErrBusyBuffer
  133. } else if cap(buf) <= maxPacketSize && cap(buf) > cap(b.buf) {
  134. b.buf = buf[:cap(buf)]
  135. }
  136. return nil
  137. }