bufpool.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 = p.normalizeSize(score)
  39. p.defaultSize = maxInt(p.defaultSize, p.calls[0])
  40. p.maxSize = maxInt(p.defaultSize, p.maxSize)
  41. cleaned := false
  42. for i, buf := range p.buffers {
  43. if buf != nil && buf.Cap() > p.maxSize {
  44. p.buffers[i] = nil
  45. cleaned = true
  46. }
  47. }
  48. if cleaned {
  49. runtime.GC()
  50. }
  51. setPrometheusBufferDefaultSize(p.name, p.defaultSize)
  52. setPrometheusBufferMaxSize(p.name, p.maxSize)
  53. }
  54. func (p *bufPool) Get(size int) *bytes.Buffer {
  55. p.mutex.Lock()
  56. defer p.mutex.Unlock()
  57. size = p.normalizeSize(size)
  58. minSize, maxSize, minInd, maxInd := -1, -1, -1, -1
  59. for i := 0; i < len(p.buffers); i++ {
  60. if p.buffers[i] != nil {
  61. cap := p.buffers[i].Cap()
  62. if size > 0 && cap >= size && (minSize > cap || minSize == -1) {
  63. minSize = cap
  64. minInd = i
  65. }
  66. if cap > maxSize {
  67. maxSize = cap
  68. maxInd = i
  69. }
  70. }
  71. }
  72. var buf *bytes.Buffer
  73. switch {
  74. case minInd >= 0:
  75. // We found buffer with the desired size
  76. buf = p.buffers[minInd]
  77. p.buffers[minInd] = nil
  78. case maxInd >= 0:
  79. // We didn't find buffer with the desired size
  80. buf = p.buffers[maxInd]
  81. p.buffers[maxInd] = nil
  82. default:
  83. // We didn't find buffers at all
  84. buf = new(bytes.Buffer)
  85. }
  86. buf.Reset()
  87. growSize := maxInt(size, p.defaultSize)
  88. if growSize > buf.Cap() {
  89. buf.Grow(growSize)
  90. }
  91. return buf
  92. }
  93. func (p *bufPool) Put(buf *bytes.Buffer) {
  94. p.mutex.Lock()
  95. defer p.mutex.Unlock()
  96. if buf.Len() > 0 {
  97. p.calls[p.callInd] = buf.Len()
  98. p.callInd++
  99. if p.callInd == len(p.calls) {
  100. p.calibrateAndClean()
  101. }
  102. }
  103. if p.maxSize > 0 && buf.Cap() > p.maxSize {
  104. return
  105. }
  106. for i, b := range p.buffers {
  107. if b == nil {
  108. p.buffers[i] = buf
  109. if buf.Cap() > 0 {
  110. observePrometheusBufferSize(p.name, buf.Cap())
  111. }
  112. return
  113. }
  114. }
  115. }
  116. func (p *bufPool) normalizeSize(n int) int {
  117. return (n/bytes.MinRead + 2) * bytes.MinRead
  118. }