buffer.go 3.3 KB

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