1
0

bufpool.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package main
  2. import (
  3. "bytes"
  4. "runtime"
  5. "sort"
  6. "sync"
  7. )
  8. type intSlice []int
  9. func (p intSlice) Len() int { return len(p) }
  10. func (p intSlice) Less(i, j int) bool { return p[i] < p[j] }
  11. func (p intSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  12. type bufPool struct {
  13. name string
  14. defaultSize int
  15. maxSize int
  16. buffers []*bytes.Buffer
  17. calls intSlice
  18. callInd int
  19. mutex sync.Mutex
  20. }
  21. func newBufPool(name string, n int, defaultSize int) *bufPool {
  22. pool := bufPool{
  23. name: name,
  24. defaultSize: defaultSize,
  25. buffers: make([]*bytes.Buffer, n),
  26. calls: make(intSlice, conf.BufferPoolCalibrationThreshold),
  27. }
  28. for i := range pool.buffers {
  29. pool.buffers[i] = new(bytes.Buffer)
  30. }
  31. return &pool
  32. }
  33. func (p *bufPool) calibrateAndClean() {
  34. sort.Sort(p.calls)
  35. pos := int(float64(len(p.calls)) * 0.95)
  36. score := p.calls[pos]
  37. p.callInd = 0
  38. p.maxSize = 64
  39. for {
  40. if p.maxSize > score {
  41. break
  42. }
  43. p.maxSize <<= 1
  44. }
  45. p.defaultSize = maxInt(p.defaultSize, p.calls[0])
  46. p.maxSize = maxInt(p.defaultSize, p.maxSize)
  47. cleaned := false
  48. for i, buf := range p.buffers {
  49. if buf != nil && buf.Cap() > p.maxSize {
  50. p.buffers[i] = nil
  51. cleaned = true
  52. }
  53. }
  54. if cleaned {
  55. runtime.GC()
  56. }
  57. if prometheusEnabled {
  58. setPrometheusBufferDefaultSize(p.name, p.defaultSize)
  59. setPrometheusBufferMaxSize(p.name, p.maxSize)
  60. }
  61. }
  62. func (p *bufPool) Get(size int) *bytes.Buffer {
  63. p.mutex.Lock()
  64. defer p.mutex.Unlock()
  65. minSize, maxSize, minInd, maxInd := -1, -1, -1, -1
  66. for i := 0; i < len(p.buffers); i++ {
  67. if p.buffers[i] != nil {
  68. cap := p.buffers[i].Cap()
  69. if size > 0 && cap >= size && (minSize > cap || minSize == -1) {
  70. minSize = cap
  71. minInd = i
  72. }
  73. if cap > maxSize {
  74. maxSize = cap
  75. maxInd = i
  76. }
  77. }
  78. }
  79. var buf *bytes.Buffer
  80. if minInd >= 0 {
  81. // We found buffer with the desired size
  82. buf = p.buffers[minInd]
  83. p.buffers[minInd] = nil
  84. } else if maxInd >= 0 {
  85. // We didn't find buffer with the desired size
  86. buf = p.buffers[maxInd]
  87. p.buffers[maxInd] = nil
  88. } else {
  89. // We didn't find buffers at all
  90. buf = new(bytes.Buffer)
  91. }
  92. buf.Reset()
  93. growSize := maxInt(size, p.defaultSize)
  94. if growSize > buf.Cap() {
  95. buf.Grow(growSize)
  96. }
  97. return buf
  98. }
  99. func (p *bufPool) Put(buf *bytes.Buffer) {
  100. p.mutex.Lock()
  101. defer p.mutex.Unlock()
  102. if buf.Len() > 0 {
  103. p.calls[p.callInd] = buf.Len()
  104. p.callInd++
  105. if p.callInd == len(p.calls) {
  106. p.calibrateAndClean()
  107. }
  108. }
  109. if p.maxSize > 0 && buf.Cap() > p.maxSize {
  110. return
  111. }
  112. for i, b := range p.buffers {
  113. if b == nil {
  114. p.buffers[i] = buf
  115. if prometheusEnabled && buf.Cap() > 0 {
  116. observePrometheusBufferSize(p.name, buf.Cap())
  117. }
  118. return
  119. }
  120. }
  121. }