mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 19:17:37 +08:00
28 lines
490 B
Go
28 lines
490 B
Go
package global
|
|
|
|
import (
|
|
"bytes"
|
|
"sync"
|
|
)
|
|
|
|
var bufferPool = sync.Pool{
|
|
New: func() interface{} {
|
|
return new(bytes.Buffer)
|
|
},
|
|
}
|
|
|
|
// NewBuffer 从池中获取新 bytes.Buffer
|
|
func NewBuffer() *bytes.Buffer {
|
|
return bufferPool.Get().(*bytes.Buffer)
|
|
}
|
|
|
|
// PutBuffer 将 Buffer放入池中
|
|
func PutBuffer(buf *bytes.Buffer) {
|
|
// See https://golang.org/issue/23199
|
|
const maxSize = 1 << 16
|
|
if buf.Cap() < maxSize { // 对于大Buffer直接丢弃
|
|
buf.Reset()
|
|
bufferPool.Put(buf)
|
|
}
|
|
}
|