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

40 lines
714 B
Go

package logjson
import (
"bytes"
"sync"
)
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)
}