Commit b640a570 authored by Xin.Zh's avatar Xin.Zh Committed by AlexStocks

limit bytes pool object size (#70)

parent dbf1d9e3
......@@ -16,6 +16,7 @@
*.idea
*.iml
*.vscode
target/
classes
......
......@@ -22,22 +22,50 @@ import (
"sync"
)
var defaultPool *ObjectPool
import (
uatomic "go.uber.org/atomic"
)
var (
poolObjectNumber uatomic.Int64
defaultPool *ObjectPool
minBufCap = 512
maxBufCap = 20 * 1024
maxPoolObjectNum = int64(4000)
)
func init() {
defaultPool = NewObjectPool(func() PoolObject {
return new(bytes.Buffer)
})
poolObjectNumber.Store(0)
}
// GetBytesBuffer returns bytes.Buffer from pool
func GetBytesBuffer() *bytes.Buffer {
return defaultPool.Get().(*bytes.Buffer)
buf := defaultPool.Get().(*bytes.Buffer)
bufCap := buf.Cap()
if bufCap >= minBufCap && bufCap <= maxBufCap && poolObjectNumber.Load() > 0 {
poolObjectNumber.Dec()
}
return buf
}
// PutIoBuffer returns IoBuffer to pool
func PutBytesBuffer(buf *bytes.Buffer) {
if poolObjectNumber.Load() > maxPoolObjectNum {
return
}
bufCap := buf.Cap()
if bufCap < minBufCap || bufCap > maxBufCap {
return
}
defaultPool.Put(buf)
poolObjectNumber.Add(1)
}
// Pool object
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment