优化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

@@ -2,10 +2,10 @@ package tcpgateway
import (
"github.com/duanhf2012/origin/log"
"github.com/duanhf2012/origin/network"
"github.com/duanhf2012/origin/node"
"github.com/duanhf2012/origin/rpc"
"github.com/duanhf2012/origin/sysservice/tcpservice"
"github.com/golang/protobuf/proto"
"strings"
)
@@ -187,6 +187,31 @@ func (r *Router) SetRouterId(clientId uint64,serviceName *string,routerId int){
r.mapClientRouterCache[clientId][*serviceName] = routerId
}
type RawInputArgs struct {
rawData []byte
clientId uint64
}
var rawInputEmpty []byte
func (args RawInputArgs) GetRawData() []byte{
if len(args.rawData) < 2 {
return args.rawData
}
return args.rawData[2:]
}
func (args RawInputArgs) GetAdditionParam() interface{}{
return args.clientId
}
func (args RawInputArgs) DoGc() {
if len(args.rawData) < 2 {
return
}
network.ReleaseByteSlice(args.rawData)
}
func (r *Router) RouterMessage(clientId uint64,msgType uint16,msg []byte) {
routerInfo:= r.GetMsgRouterService(msgType)
if routerInfo==nil {
@@ -201,7 +226,7 @@ func (r *Router) RouterMessage(clientId uint64,msgType uint16,msg []byte) {
}
if routerId>0 {
r.rpcHandler.RawGoNode(rpc.RpcProcessorPb,routerId,routerInfo.Rpc,msg,proto.Uint64(clientId))
r.rpcHandler.RawGoNode(rpc.RpcProcessorPb,routerId,routerInfo.Rpc,RawInputArgs{rawData: msg,clientId: clientId})
}
}
@@ -222,7 +247,7 @@ func (r *Router) RouterEvent(clientId uint64,eventType string) bool{
}
if routerId>0 {
r.rpcHandler.RawGoNode(rpc.RpcProcessorPb,routerId,routerInfo.Rpc,[]byte{},proto.Uint64(clientId))
r.rpcHandler.RawGoNode(rpc.RpcProcessorPb,routerId,routerInfo.Rpc,RawInputArgs{rawData: rawInputEmpty,clientId: clientId})
return true
}

View File

@@ -45,7 +45,7 @@ var seedLocker sync.Mutex
type TcpPack struct {
Type TcpPackType //0表示连接 1表示断开 2表示数据
MsgProcessor processor.IProcessor
//MsgProcessor processor.IProcessor
ClientId uint64
Data interface{}
}
@@ -118,16 +118,16 @@ func (tcpService *TcpService) OnInit() error{
}
func (tcpService *TcpService) TcpEventHandler(ev *event.Event) {
pack := ev.Data.(*TcpPack)
pack := ev.Data.(TcpPack)
switch pack.Type {
case TPT_Connected:
pack.MsgProcessor.ConnectedRoute(pack.ClientId)
tcpService.process.ConnectedRoute(pack.ClientId)
case TPT_DisConnected:
pack.MsgProcessor.DisConnectedRoute(pack.ClientId)
tcpService.process.DisConnectedRoute(pack.ClientId)
case TPT_UnknownPack:
pack.MsgProcessor.UnknownMsgRoute(pack.Data,pack.ClientId)
tcpService.process.UnknownMsgRoute(pack.Data,pack.ClientId)
case TPT_Pack:
pack.MsgProcessor.MsgRoute(pack.Data, pack.ClientId)
tcpService.process.MsgRoute(pack.Data, pack.ClientId)
}
}
@@ -162,7 +162,7 @@ func (slf *Client) GetId() uint64 {
}
func (slf *Client) Run() {
slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:&TcpPack{ClientId:slf.id,Type:TPT_Connected,MsgProcessor:slf.tcpService.process}})
slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:TcpPack{ClientId:slf.id,Type:TPT_Connected}})
for{
if slf.tcpConn == nil {
break
@@ -173,17 +173,17 @@ func (slf *Client) Run() {
break
}
data,err:=slf.tcpService.process.Unmarshal(bytes)
slf.tcpConn.ReleaseReadMsg(bytes)
if err != nil {
slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:&TcpPack{ClientId:slf.id,Type:TPT_UnknownPack,Data:bytes,MsgProcessor:slf.tcpService.process}})
slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:TcpPack{ClientId:slf.id,Type:TPT_UnknownPack,Data:bytes}})
continue
}
slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:&TcpPack{ClientId:slf.id,Type:TPT_Pack,Data:data,MsgProcessor:slf.tcpService.process}})
slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:TcpPack{ClientId:slf.id,Type:TPT_Pack,Data:data}})
}
}
func (slf *Client) OnClose(){
slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:&TcpPack{ClientId:slf.id,Type:TPT_DisConnected,MsgProcessor:slf.tcpService.process}})
slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:TcpPack{ClientId:slf.id,Type:TPT_DisConnected}})
slf.tcpService.mapClientLocker.Lock()
defer slf.tcpService.mapClientLocker.Unlock()
delete (slf.tcpService.mapClient,slf.GetId())