bufpool.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package main
  2. import (
  3. "bytes"
  4. "sort"
  5. "sync"
  6. )
  7. type intSlice []int
  8. func (p intSlice) Len() int { return len(p) }
  9. func (p intSlice) Less(i, j int) bool { return p[i] < p[j] }
  10. func (p intSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  11. type bufPool struct {
  12. name string
  13. defaultSize int
  14. maxSize int
  15. buffers []*bytes.Buffer
  16. calls intSlice
  17. callInd int
  18. mutex sync.Mutex
  19. }
  20. func newBufPool(name string, n int, defaultSize int) *bufPool {
  21. pool := bufPool{
  22. name: name,
  23. defaultSize: defaultSize,
  24. buffers: make([]*bytes.Buffer, n),
  25. calls: make(intSlice, 1024),
  26. }
  27. for i := range pool.buffers {
  28. pool.buffers[i] = new(bytes.Buffer)
  29. }
  30. return &pool
  31. }
  32. func (p *bufPool) calibrateAndClean() {
  33. sort.Sort(p.calls)
  34. pos := int(float64(len(p.calls)) * 0.95)
  35. score := p.calls[pos]
  36. p.callInd = 0
  37. p.maxSize = 64
  38. for {
  39. if p.maxSize > score {
  40. break
  41. }
  42. p.maxSize <<= 1
  43. }
  44. p.defaultSize = maxInt(p.defaultSize, p.calls[0])
  45. p.maxSize = maxInt(p.defaultSize, p.maxSize)
  46. for i, buf := range p.buffers {
  47. if buf != nil && buf.Cap() > p.maxSize {
  48. p.buffers[i] = nil
  49. }
  50. }
  51. if prometheusEnabled {
  52. setPrometheusBufferDefaultSize(p.name, p.defaultSize)
  53. setPrometheusBufferMaxSize(p.name, p.maxSize)
  54. }
  55. }
  56. func (p *bufPool) Get(size int) *bytes.Buffer {
  57. p.mutex.Lock()
  58. defer p.mutex.Unlock()
  59. minSize, maxSize, minInd, maxInd := -1, -1, -1, -1
  60. for i := 0; i < len(p.buffers); i++ {
  61. if p.buffers[i] != nil {
  62. cap := p.buffers[i].Cap()
  63. if size > 0 && cap >= size && (minSize > cap || minSize == -1) {
  64. minSize = cap
  65. minInd = i
  66. }
  67. if cap > maxSize {
  68. maxSize = cap
  69. maxInd = i
  70. }
  71. }
  72. }
  73. var buf *bytes.Buffer
  74. if minInd >= 0 {
  75. // We found buffer with the desired size
  76. buf = p.buffers[minInd]
  77. p.buffers[minInd] = nil
  78. } else if maxInd >= 0 {
  79. // We didn't find buffer with the desired size
  80. buf = p.buffers[maxInd]
  81. p.buffers[maxInd] = nil
  82. } else {
  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 prometheusEnabled && buf.Cap() > 0 {
  110. observePrometheusBufferSize(p.name, buf.Cap())
  111. }
  112. return
  113. }
  114. }
  115. }