优化payload

This commit is contained in:
bjdgyc
2021-07-30 10:53:43 +08:00
parent 583ca4d635
commit 60084d499a
11 changed files with 108 additions and 80 deletions

View File

@@ -11,13 +11,13 @@ import (
// [6] => PType
var plHeader = []byte{'S', 'T', 'F', 0x01, 0x00, 0x00, 0x00, 0x00}
var plPool = &sync.Pool{
var plPool = sync.Pool{
New: func() interface{} {
b := make([]byte, BufferSize)
pl := sessdata.Payload{
LType: sessdata.LTypeIPData,
PType: 0x00,
Data: &b,
Data: b,
}
// fmt.Println("plPool-init", len(pl.Data), cap(pl.Data))
return &pl
@@ -31,20 +31,20 @@ func getPayload() *sessdata.Payload {
func putPayload(pl *sessdata.Payload) {
// 错误数据丢弃
if cap(*pl.Data) != BufferSize {
base.Warn("payload cap is err", cap(*pl.Data))
if cap(pl.Data) != BufferSize {
base.Warn("payload cap is err", cap(pl.Data))
return
}
pl.LType = sessdata.LTypeIPData
pl.PType = 0x00
*pl.Data = (*pl.Data)[:BufferSize]
pl.Data = pl.Data[:BufferSize]
plPool.Put(pl)
}
var bytePool = &sync.Pool{
var bytePool = sync.Pool{
New: func() interface{} {
b := make([]byte, 0, BufferSize)
b := make([]byte, BufferSize)
// fmt.Println("bytePool-init")
return &b
},
@@ -52,15 +52,15 @@ var bytePool = &sync.Pool{
func getByteZero() *[]byte {
b := bytePool.Get().(*[]byte)
*b = (*b)[:0]
return b
}
func getByteFull() *[]byte {
b := bytePool.Get().(*[]byte)
*b = (*b)[:BufferSize]
return b
}
func putByte(b *[]byte) {
*b = (*b)[:0]
*b = (*b)[:BufferSize]
bytePool.Put(b)
}