优化gateway-减少GC

This commit is contained in:
boyce
2020-11-03 14:53:49 +08:00
parent 060095baea
commit 974fbd3584
12 changed files with 155 additions and 119 deletions

View File

@@ -3,6 +3,7 @@ package processor
import (
"encoding/json"
"fmt"
"github.com/duanhf2012/origin/network"
"reflect"
)
@@ -53,6 +54,7 @@ func (jsonProcessor *JsonProcessor ) MsgRoute(msg interface{},userdata interface
func (jsonProcessor *JsonProcessor) Unmarshal(data []byte) (interface{}, error) {
typeStruct := struct {Type int `json:"typ"`}{}
defer network.ReleaseByteSlice(data)
err := json.Unmarshal(data, &typeStruct)
if err != nil {
return nil, err
@@ -64,13 +66,13 @@ func (jsonProcessor *JsonProcessor) Unmarshal(data []byte) (interface{}, error)
return nil,fmt.Errorf("Cannot find register %d msgType!",msgType)
}
msg := reflect.New(info.msgType.Elem()).Interface()
err = json.Unmarshal(data, msg)
msgData := reflect.New(info.msgType.Elem()).Interface()
err = json.Unmarshal(data, msgData)
if err != nil {
return nil,err
}
return &JsonPackInfo{typ:msgType,msg:msg},nil
return &JsonPackInfo{typ:msgType,msg:msgData},nil
}
func (jsonProcessor *JsonProcessor) Marshal(msg interface{}) ([]byte, error) {

View File

@@ -3,6 +3,7 @@ package processor
import (
"encoding/binary"
"fmt"
"github.com/duanhf2012/origin/network"
"github.com/golang/protobuf/proto"
"reflect"
)
@@ -63,6 +64,7 @@ func (pbProcessor *PBProcessor ) MsgRoute(msg interface{},userdata interface{})
// must goroutine safe
func (pbProcessor *PBProcessor ) Unmarshal(data []byte) (interface{}, error) {
defer network.ReleaseByteSlice(data)
var msgType uint16
if pbProcessor.LittleEndian == true {
msgType = binary.LittleEndian.Uint16(data[:2])

View File

@@ -53,7 +53,7 @@ func (pbRawProcessor *PBRawProcessor ) Unmarshal(data []byte) (interface{}, erro
msgType = binary.BigEndian.Uint16(data[:2])
}
return &PBRawPackInfo{typ:msgType,rawMsg:data[2:]},nil
return &PBRawPackInfo{typ:msgType,rawMsg:data},nil
}
// must goroutine safe