mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
优化gateway-减少GC
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -37,8 +37,7 @@ func newTCPConn(conn net.Conn, pendingWriteNum int, msgParser *MsgParser) *TCPCo
|
||||
break
|
||||
}
|
||||
_, err := conn.Write(b)
|
||||
releaseByteSlice(b)
|
||||
|
||||
ReleaseByteSlice(b)
|
||||
|
||||
if err != nil {
|
||||
break
|
||||
@@ -126,7 +125,7 @@ func (tcpConn *TCPConn) ReadMsg() ([]byte, error) {
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) ReleaseReadMsg(byteBuff []byte){
|
||||
releaseByteSlice(byteBuff)
|
||||
ReleaseByteSlice(byteBuff)
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) WriteMsg(args ...[]byte) error {
|
||||
|
||||
@@ -101,7 +101,7 @@ func (p *MsgParser) Read(conn *TCPConn) ([]byte, error) {
|
||||
//msgData := make([]byte, msgLen)
|
||||
msgData := makeByteSlice(int(msgLen))
|
||||
if _, err := io.ReadFull(conn, msgData); err != nil {
|
||||
releaseByteSlice(msgData)
|
||||
ReleaseByteSlice(msgData)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ func (p *MsgParser) Write(conn *TCPConn, args ...[]byte) error {
|
||||
} else if msgLen < p.minMsgLen {
|
||||
return errors.New("message too short")
|
||||
}
|
||||
|
||||
|
||||
//msg := make([]byte, uint32(p.lenMsgLen)+msgLen)
|
||||
msg := makeByteSlice(p.lenMsgLen+int(msgLen))
|
||||
// write len
|
||||
|
||||
Reference in New Issue
Block a user