nps/lib/common/pool.go

147 lines
2.5 KiB
Go
Raw Normal View History

package common
2019-02-09 09:07:47 +00:00
import (
2019-08-23 10:53:36 +00:00
"bytes"
2019-02-09 09:07:47 +00:00
"sync"
)
const PoolSize = 64 * 1024
const PoolSizeSmall = 100
const PoolSizeUdp = 1472
2019-03-02 14:57:05 +00:00
const PoolSizeCopy = 32 << 10
2019-02-09 09:07:47 +00:00
var BufPool = sync.Pool{
New: func() interface{} {
return make([]byte, PoolSize)
},
}
var BufPoolUdp = sync.Pool{
New: func() interface{} {
return make([]byte, PoolSizeUdp)
},
}
var BufPoolMax = sync.Pool{
New: func() interface{} {
return make([]byte, PoolSize)
},
}
var BufPoolSmall = sync.Pool{
New: func() interface{} {
return make([]byte, PoolSizeSmall)
},
}
var BufPoolCopy = sync.Pool{
New: func() interface{} {
2019-08-26 13:42:20 +00:00
return make([]byte, PoolSizeCopy)
2019-02-09 09:07:47 +00:00
},
}
2019-08-23 10:53:36 +00:00
2019-02-17 11:36:48 +00:00
func PutBufPoolUdp(buf []byte) {
if cap(buf) == PoolSizeUdp {
BufPoolUdp.Put(buf[:PoolSizeUdp])
}
}
2019-02-09 09:07:47 +00:00
func PutBufPoolCopy(buf []byte) {
if cap(buf) == PoolSizeCopy {
2019-08-26 13:42:20 +00:00
BufPoolCopy.Put(buf[:PoolSizeCopy])
2019-02-09 09:07:47 +00:00
}
}
2019-08-23 10:53:36 +00:00
func GetBufPoolCopy() []byte {
2019-08-26 13:42:20 +00:00
return (BufPoolCopy.Get().([]byte))[:PoolSizeCopy]
2019-03-15 06:03:49 +00:00
}
2019-02-17 11:36:48 +00:00
func PutBufPoolMax(buf []byte) {
if cap(buf) == PoolSize {
BufPoolMax.Put(buf[:PoolSize])
2019-02-09 09:07:47 +00:00
}
}
2019-08-23 10:53:36 +00:00
2019-08-26 15:29:22 +00:00
type CopyBufferPool struct {
pool sync.Pool
}
func (Self *CopyBufferPool) New() {
Self.pool = sync.Pool{
New: func() interface{} {
2019-08-27 12:07:37 +00:00
return make([]byte, PoolSizeCopy, PoolSizeCopy)
2019-08-26 15:29:22 +00:00
},
}
}
func (Self *CopyBufferPool) Get() []byte {
2019-08-27 12:07:37 +00:00
buf := Self.pool.Get().([]byte)
return buf[:PoolSizeCopy] // just like make a new slice, but data may not be 0
2019-08-26 15:29:22 +00:00
}
func (Self *CopyBufferPool) Put(x []byte) {
if len(x) == PoolSizeCopy {
Self.pool.Put(x)
} else {
x = nil // buf is not full, maybe truncated by gc in pool, not allowed
}
2019-08-26 15:29:22 +00:00
}
2019-08-23 10:53:36 +00:00
type BufferPool struct {
pool sync.Pool
}
func (Self *BufferPool) New() {
Self.pool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
}
func (Self *BufferPool) Get() *bytes.Buffer {
return Self.pool.Get().(*bytes.Buffer)
}
func (Self *BufferPool) Put(x *bytes.Buffer) {
x.Reset()
Self.pool.Put(x)
}
type MuxPackagerPool struct {
pool sync.Pool
}
func (Self *MuxPackagerPool) New() {
Self.pool = sync.Pool{
New: func() interface{} {
pack := MuxPackager{}
return &pack
},
}
}
func (Self *MuxPackagerPool) Get() *MuxPackager {
pack := Self.pool.Get().(*MuxPackager)
buf := CopyBuff.Get()
pack.Content = buf
return pack
}
func (Self *MuxPackagerPool) Put(pack *MuxPackager) {
CopyBuff.Put(pack.Content)
Self.pool.Put(pack)
}
2019-08-23 10:53:36 +00:00
var once = sync.Once{}
var BuffPool = BufferPool{}
2019-08-26 15:29:22 +00:00
var CopyBuff = CopyBufferPool{}
var MuxPack = MuxPackagerPool{}
2019-08-26 15:29:22 +00:00
func newPool() {
BuffPool.New()
CopyBuff.New()
MuxPack.New()
2019-08-26 15:29:22 +00:00
}
2019-08-23 10:53:36 +00:00
func init() {
2019-08-26 15:29:22 +00:00
once.Do(newPool)
2019-08-23 10:53:36 +00:00
}