You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
crate/log/logsdk/logjson/pool.go

47 lines
883 B
Go

package logjson
import (
"bytes"
"sync"
)
const (
bytesBufferInitialSize = 512
bytesBufferMaximumSize = 4096
)
var DefaultBytesBufferPool = NewBytesBufferPool(bytesBufferInitialSize, bytesBufferMaximumSize)
type BytesBufferPool interface {
Get() *bytes.Buffer
Put(buffer *bytes.Buffer)
}
// NewBytesBufferPool 创建并返回 BytesBufferPool
func NewBytesBufferPool(initialSize, maximumSize int) BytesBufferPool {
return &bytesBufferPool{
pool: sync.Pool{
New: func() any {
return bytes.NewBuffer(make([]byte, 0, initialSize))
},
},
maximumSize: maximumSize,
}
}
type bytesBufferPool struct {
pool sync.Pool
maximumSize int
}
func (pool *bytesBufferPool) Get() *bytes.Buffer {
return pool.pool.Get().(*bytes.Buffer)
}
func (pool *bytesBufferPool) Put(buf *bytes.Buffer) {
if buf.Cap() > pool.maximumSize {
return
}
pool.pool.Put(buf)
}