2021-09-20 21:02:18 +08:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"sync"
|
2023-04-17 14:08:39 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/protobytes"
|
2021-09-20 21:02:18 +08:00
|
|
|
)
|
|
|
|
|
2023-04-17 14:08:39 +08:00
|
|
|
var (
|
|
|
|
bufferPool = sync.Pool{New: func() any { return &bytes.Buffer{} }}
|
|
|
|
bytesBufferPool = sync.Pool{New: func() any { return &protobytes.BytesWriter{} }}
|
|
|
|
)
|
2021-09-20 21:02:18 +08:00
|
|
|
|
|
|
|
func GetBuffer() *bytes.Buffer {
|
|
|
|
return bufferPool.Get().(*bytes.Buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PutBuffer(buf *bytes.Buffer) {
|
|
|
|
buf.Reset()
|
|
|
|
bufferPool.Put(buf)
|
|
|
|
}
|
2023-04-17 14:08:39 +08:00
|
|
|
|
|
|
|
func GetBytesBuffer() *protobytes.BytesWriter {
|
|
|
|
return bytesBufferPool.Get().(*protobytes.BytesWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PutBytesBuffer(buf *protobytes.BytesWriter) {
|
|
|
|
buf.Reset()
|
2023-04-20 11:07:21 +08:00
|
|
|
bytesBufferPool.Put(buf)
|
2023-04-17 14:08:39 +08:00
|
|
|
}
|