2019-08-30 12:05:09 +00:00
|
|
|
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-10-08 13:41:25 +00:00
|
|
|
const PoolSizeBuffer = 4096
|
2019-11-22 16:42:46 +00:00
|
|
|
const PoolSizeWindow = PoolSizeBuffer - 2 - 4 - 4 - 1
|
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-09-08 15:49:16 +00:00
|
|
|
type copyBufferPool struct {
|
2019-08-26 15:29:22 +00:00
|
|
|
pool sync.Pool
|
|
|
|
}
|
|
|
|
|
2019-09-08 15:49:16 +00:00
|
|
|
func (Self *copyBufferPool) New() {
|
2019-08-26 15:29:22 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 15:49:16 +00:00
|
|
|
func (Self *copyBufferPool) Get() []byte {
|
2019-08-27 12:07:37 +00:00
|
|
|
buf := Self.pool.Get().([]byte)
|
2019-08-30 12:05:09 +00:00
|
|
|
return buf[:PoolSizeCopy] // just like make a new slice, but data may not be 0
|
2019-08-26 15:29:22 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 15:49:16 +00:00
|
|
|
func (Self *copyBufferPool) Put(x []byte) {
|
2019-08-30 12:05:09 +00:00
|
|
|
if len(x) == PoolSizeCopy {
|
|
|
|
Self.pool.Put(x)
|
|
|
|
} else {
|
2019-09-08 15:49:16 +00:00
|
|
|
x = nil // buf is not full, not allowed, New method returns a full buf
|
2019-08-30 12:05:09 +00:00
|
|
|
}
|
2019-08-26 15:29:22 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 15:49:16 +00:00
|
|
|
type windowBufferPool struct {
|
2019-08-23 10:53:36 +00:00
|
|
|
pool sync.Pool
|
|
|
|
}
|
|
|
|
|
2019-09-08 15:49:16 +00:00
|
|
|
func (Self *windowBufferPool) New() {
|
|
|
|
Self.pool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
2019-10-08 13:41:25 +00:00
|
|
|
return make([]byte, PoolSizeWindow, PoolSizeWindow)
|
2019-09-08 15:49:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Self *windowBufferPool) Get() (buf []byte) {
|
|
|
|
buf = Self.pool.Get().([]byte)
|
2019-10-08 13:41:25 +00:00
|
|
|
return buf[:PoolSizeWindow]
|
2019-09-08 15:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (Self *windowBufferPool) Put(x []byte) {
|
2019-10-12 14:56:37 +00:00
|
|
|
Self.pool.Put(x[:PoolSizeWindow]) // make buf to full
|
2019-09-08 15:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type bufferPool struct {
|
|
|
|
pool sync.Pool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Self *bufferPool) New() {
|
2019-08-23 10:53:36 +00:00
|
|
|
Self.pool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
2019-10-08 13:41:25 +00:00
|
|
|
return bytes.NewBuffer(make([]byte, 0, PoolSizeBuffer))
|
2019-08-23 10:53:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 15:49:16 +00:00
|
|
|
func (Self *bufferPool) Get() *bytes.Buffer {
|
2019-08-23 10:53:36 +00:00
|
|
|
return Self.pool.Get().(*bytes.Buffer)
|
|
|
|
}
|
|
|
|
|
2019-09-08 15:49:16 +00:00
|
|
|
func (Self *bufferPool) Put(x *bytes.Buffer) {
|
2019-08-23 10:53:36 +00:00
|
|
|
x.Reset()
|
|
|
|
Self.pool.Put(x)
|
|
|
|
}
|
|
|
|
|
2019-09-08 15:49:16 +00:00
|
|
|
type muxPackagerPool struct {
|
2019-08-30 12:05:09 +00:00
|
|
|
pool sync.Pool
|
|
|
|
}
|
|
|
|
|
2019-09-08 15:49:16 +00:00
|
|
|
func (Self *muxPackagerPool) New() {
|
2019-08-30 12:05:09 +00:00
|
|
|
Self.pool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
pack := MuxPackager{}
|
|
|
|
return &pack
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 15:49:16 +00:00
|
|
|
func (Self *muxPackagerPool) Get() *MuxPackager {
|
2019-10-12 14:56:37 +00:00
|
|
|
return Self.pool.Get().(*MuxPackager)
|
2019-08-30 12:05:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 15:49:16 +00:00
|
|
|
func (Self *muxPackagerPool) Put(pack *MuxPackager) {
|
2019-08-30 12:05:09 +00:00
|
|
|
Self.pool.Put(pack)
|
|
|
|
}
|
|
|
|
|
2019-11-09 15:02:29 +00:00
|
|
|
type ListElement struct {
|
|
|
|
Buf []byte
|
|
|
|
L uint16
|
|
|
|
Part bool
|
2019-11-02 14:59:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 15:02:29 +00:00
|
|
|
type listElementPool struct {
|
|
|
|
pool sync.Pool
|
2019-11-02 14:59:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 15:02:29 +00:00
|
|
|
func (Self *listElementPool) New() {
|
|
|
|
Self.pool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
element := ListElement{}
|
|
|
|
return &element
|
|
|
|
},
|
2019-11-02 14:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 15:02:29 +00:00
|
|
|
func (Self *listElementPool) Get() *ListElement {
|
|
|
|
return Self.pool.Get().(*ListElement)
|
2019-11-02 14:59:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 15:02:29 +00:00
|
|
|
func (Self *listElementPool) Put(element *ListElement) {
|
|
|
|
element.L = 0
|
|
|
|
element.Buf = nil
|
|
|
|
element.Part = false
|
|
|
|
Self.pool.Put(element)
|
2019-11-02 14:59:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 10:53:36 +00:00
|
|
|
var once = sync.Once{}
|
2019-09-08 15:49:16 +00:00
|
|
|
var BuffPool = bufferPool{}
|
|
|
|
var CopyBuff = copyBufferPool{}
|
|
|
|
var MuxPack = muxPackagerPool{}
|
|
|
|
var WindowBuff = windowBufferPool{}
|
2019-11-09 15:02:29 +00:00
|
|
|
var ListElementPool = listElementPool{}
|
2019-08-26 15:29:22 +00:00
|
|
|
|
|
|
|
func newPool() {
|
|
|
|
BuffPool.New()
|
|
|
|
CopyBuff.New()
|
2019-08-30 12:05:09 +00:00
|
|
|
MuxPack.New()
|
2019-09-08 15:49:16 +00:00
|
|
|
WindowBuff.New()
|
2019-11-09 15:02:29 +00:00
|
|
|
ListElementPool.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
|
|
|
}
|