2019-01-17 10:51:19 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-02-01 12:04:28 +02:00
|
|
|
"runtime"
|
2019-01-30 10:36:19 +02:00
|
|
|
"sort"
|
2019-01-17 10:51:19 +02:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2019-01-30 10:36:19 +02:00
|
|
|
type intSlice []int
|
|
|
|
|
|
|
|
func (p intSlice) Len() int { return len(p) }
|
|
|
|
func (p intSlice) Less(i, j int) bool { return p[i] < p[j] }
|
|
|
|
func (p intSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
|
|
2019-01-17 10:51:19 +02:00
|
|
|
type bufPool struct {
|
2019-01-31 10:00:31 +02:00
|
|
|
name string
|
|
|
|
defaultSize int
|
|
|
|
maxSize int
|
|
|
|
buffers []*bytes.Buffer
|
2019-01-30 10:36:19 +02:00
|
|
|
|
|
|
|
calls intSlice
|
|
|
|
callInd int
|
2019-01-17 10:51:19 +02:00
|
|
|
|
2019-01-31 10:00:31 +02:00
|
|
|
mutex sync.Mutex
|
2019-01-17 10:51:19 +02:00
|
|
|
}
|
|
|
|
|
2019-01-31 10:00:31 +02:00
|
|
|
func newBufPool(name string, n int, defaultSize int) *bufPool {
|
2019-01-30 10:36:19 +02:00
|
|
|
pool := bufPool{
|
2019-01-31 10:00:31 +02:00
|
|
|
name: name,
|
|
|
|
defaultSize: defaultSize,
|
|
|
|
buffers: make([]*bytes.Buffer, n),
|
2019-02-01 13:13:00 +02:00
|
|
|
calls: make(intSlice, conf.BufferPoolCalibrationThreshold),
|
2019-01-30 10:36:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range pool.buffers {
|
2019-01-31 10:00:31 +02:00
|
|
|
pool.buffers[i] = new(bytes.Buffer)
|
2019-01-17 10:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &pool
|
|
|
|
}
|
|
|
|
|
2019-01-30 10:36:19 +02:00
|
|
|
func (p *bufPool) calibrateAndClean() {
|
|
|
|
sort.Sort(p.calls)
|
2019-01-28 18:18:54 +02:00
|
|
|
|
2019-01-30 12:31:00 +02:00
|
|
|
pos := int(float64(len(p.calls)) * 0.95)
|
|
|
|
score := p.calls[pos]
|
2019-01-30 10:36:19 +02:00
|
|
|
|
2019-01-30 12:31:00 +02:00
|
|
|
p.callInd = 0
|
2019-03-11 10:30:00 +02:00
|
|
|
p.maxSize = p.normalizeSize(score)
|
2019-01-30 10:36:19 +02:00
|
|
|
|
2019-01-31 10:00:31 +02:00
|
|
|
p.defaultSize = maxInt(p.defaultSize, p.calls[0])
|
|
|
|
p.maxSize = maxInt(p.defaultSize, p.maxSize)
|
|
|
|
|
2019-02-01 12:04:28 +02:00
|
|
|
cleaned := false
|
|
|
|
|
2019-01-30 12:31:00 +02:00
|
|
|
for i, buf := range p.buffers {
|
2019-01-31 10:00:31 +02:00
|
|
|
if buf != nil && buf.Cap() > p.maxSize {
|
2019-01-30 12:31:00 +02:00
|
|
|
p.buffers[i] = nil
|
2019-02-01 12:04:28 +02:00
|
|
|
cleaned = true
|
2019-01-30 12:31:00 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-31 10:00:31 +02:00
|
|
|
|
2019-02-01 12:04:28 +02:00
|
|
|
if cleaned {
|
|
|
|
runtime.GC()
|
|
|
|
}
|
|
|
|
|
2019-01-31 10:00:31 +02:00
|
|
|
if prometheusEnabled {
|
|
|
|
setPrometheusBufferDefaultSize(p.name, p.defaultSize)
|
|
|
|
setPrometheusBufferMaxSize(p.name, p.maxSize)
|
|
|
|
}
|
2019-01-17 10:51:19 +02:00
|
|
|
}
|
|
|
|
|
2019-01-30 12:31:00 +02:00
|
|
|
func (p *bufPool) Get(size int) *bytes.Buffer {
|
2019-01-17 10:51:19 +02:00
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
|
2019-03-11 10:30:00 +02:00
|
|
|
size = p.normalizeSize(size)
|
|
|
|
|
2019-01-30 10:36:19 +02:00
|
|
|
minSize, maxSize, minInd, maxInd := -1, -1, -1, -1
|
|
|
|
|
|
|
|
for i := 0; i < len(p.buffers); i++ {
|
|
|
|
if p.buffers[i] != nil {
|
|
|
|
cap := p.buffers[i].Cap()
|
|
|
|
|
|
|
|
if size > 0 && cap >= size && (minSize > cap || minSize == -1) {
|
|
|
|
minSize = cap
|
|
|
|
minInd = i
|
|
|
|
}
|
|
|
|
|
|
|
|
if cap > maxSize {
|
|
|
|
maxSize = cap
|
|
|
|
maxInd = i
|
|
|
|
}
|
|
|
|
}
|
2019-01-17 10:51:19 +02:00
|
|
|
}
|
|
|
|
|
2019-01-30 10:36:19 +02:00
|
|
|
var buf *bytes.Buffer
|
2019-01-17 10:51:19 +02:00
|
|
|
|
2019-01-30 10:36:19 +02:00
|
|
|
if minInd >= 0 {
|
|
|
|
// We found buffer with the desired size
|
|
|
|
buf = p.buffers[minInd]
|
|
|
|
p.buffers[minInd] = nil
|
|
|
|
} else if maxInd >= 0 {
|
|
|
|
// We didn't find buffer with the desired size
|
|
|
|
buf = p.buffers[maxInd]
|
|
|
|
p.buffers[maxInd] = nil
|
|
|
|
} else {
|
|
|
|
// We didn't find buffers at all
|
2019-01-31 10:00:31 +02:00
|
|
|
buf = new(bytes.Buffer)
|
2019-01-30 10:36:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buf.Reset()
|
2019-01-17 10:51:19 +02:00
|
|
|
|
2019-01-31 10:00:31 +02:00
|
|
|
growSize := maxInt(size, p.defaultSize)
|
|
|
|
|
|
|
|
if growSize > buf.Cap() {
|
|
|
|
buf.Grow(growSize)
|
2019-01-30 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-17 10:51:19 +02:00
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2019-01-30 12:31:00 +02:00
|
|
|
func (p *bufPool) Put(buf *bytes.Buffer) {
|
2019-01-17 10:51:19 +02:00
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
|
2019-01-30 12:31:00 +02:00
|
|
|
if buf.Len() > 0 {
|
|
|
|
p.calls[p.callInd] = buf.Len()
|
|
|
|
p.callInd++
|
2019-01-30 10:36:19 +02:00
|
|
|
|
2019-01-30 12:31:00 +02:00
|
|
|
if p.callInd == len(p.calls) {
|
|
|
|
p.calibrateAndClean()
|
|
|
|
}
|
2019-01-30 10:36:19 +02:00
|
|
|
}
|
|
|
|
|
2019-01-31 10:00:31 +02:00
|
|
|
if p.maxSize > 0 && buf.Cap() > p.maxSize {
|
2019-01-30 10:36:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, b := range p.buffers {
|
|
|
|
if b == nil {
|
|
|
|
p.buffers[i] = buf
|
|
|
|
|
2019-01-30 12:31:00 +02:00
|
|
|
if prometheusEnabled && buf.Cap() > 0 {
|
2019-01-31 10:00:31 +02:00
|
|
|
observePrometheusBufferSize(p.name, buf.Cap())
|
2019-01-30 10:36:19 +02:00
|
|
|
}
|
2019-01-28 18:18:54 +02:00
|
|
|
|
2019-01-30 10:36:19 +02:00
|
|
|
return
|
|
|
|
}
|
2019-01-28 18:18:54 +02:00
|
|
|
}
|
2019-01-17 10:51:19 +02:00
|
|
|
}
|
2019-03-11 10:30:00 +02:00
|
|
|
|
|
|
|
func (p *bufPool) normalizeSize(n int) int {
|
|
|
|
return (n/bytes.MinRead + 2) * bytes.MinRead
|
|
|
|
}
|