Files
Archive/echo/pkg/buffer/buffer.go
2024-08-16 20:33:05 +02:00

58 lines
1.2 KiB
Go

package buffer
import (
"github.com/Ehco1996/ehco/internal/constant"
)
// 全局pool
var (
BufferPool *BytePool
UDPBufferPool *BytePool
)
func init() {
BufferPool = NewBytePool(constant.BUFFER_POOL_SIZE, constant.BUFFER_SIZE)
UDPBufferPool = NewBytePool(constant.BUFFER_POOL_SIZE, constant.UDPBufSize)
}
// BytePool implements a leaky pool of []byte in the form of a bounded channel
type BytePool struct {
c chan []byte
size int
}
// NewBytePool creates a new BytePool bounded to the given maxSize, with new
// byte arrays sized based on width.
func NewBytePool(maxSize int, size int) (bp *BytePool) {
return &BytePool{
c: make(chan []byte, maxSize),
size: size,
}
}
// Get gets a []byte from the BytePool, or creates a new one if none are available in the pool.
func (bp *BytePool) Get() (b []byte) {
select {
case b = <-bp.c:
// reuse existing buffer
default:
// create new buffer
b = make([]byte, bp.size)
}
return
}
// Put returns the given Buffer to the BytePool.
func (bp *BytePool) Put(b []byte) {
select {
case bp.c <- b:
// buffer went back into pool
default:
// buffer didn't go back into pool, just discard
}
}
func ReplaceBufferPool(size int) {
BufferPool = NewBytePool(constant.BUFFER_POOL_SIZE, size)
}