1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00

字节数组池

This commit is contained in:
2022-03-27 10:18:50 +08:00
parent 0c3a00ba36
commit 59ea343843
2 changed files with 47 additions and 0 deletions

36
bytePool/byte_pool.go Normal file
View File

@ -0,0 +1,36 @@
package bytecache
type BytePool struct {
c chan []byte
w int
wcap int
}
func NewBytePool(poolSize, size, cap int) *BytePool {
return &BytePool{
c: make(chan []byte, poolSize),
w: size,
wcap: cap,
}
}
func (bp *BytePool) Get() (b []byte) {
select {
case b = <-bp.c:
default:
if bp.wcap > 0 {
b = make([]byte, bp.w, bp.wcap)
} else {
b = make([]byte, bp.w)
}
}
return
}
func (bp *BytePool) Put(b []byte) {
select {
case bp.c <- b:
default:
}
}

View File

@ -0,0 +1,11 @@
package bytecache
import "testing"
func TestByteCache(t *testing.T) {
bp := NewBytePool(512, 1024, 1024)
buffer := bp.Get()
defer bp.Put(buffer)
t.Log(len(buffer))
}