1.优化pending存储结构,支持超时判断

2.protobuf与json支持临时内存池
This commit is contained in:
boyce
2020-07-10 14:09:52 +08:00
parent 323e5313fb
commit c7a1d86039
4 changed files with 137 additions and 49 deletions

View File

@@ -2,11 +2,26 @@ package rpc
import (
"github.com/golang/protobuf/proto"
"sync"
)
type PBProcessor struct {
}
var rpcPbResponeDataPool sync.Pool
var rpcPbRequestDataPool sync.Pool
func init(){
rpcPbResponeDataPool.New = func()interface{}{
return &JsonRpcResponseData{}
}
rpcPbRequestDataPool.New = func()interface{}{
return &JsonRpcRequestData{}
}
}
func (slf *PBRpcRequestData) MakeRequest(seq uint64,serviceMethod string,noReply bool,inParam []byte) *PBRpcRequestData{
slf.Seq = proto.Uint64(seq)
slf.ServiceMethod = proto.String(serviceMethod)
@@ -23,15 +38,6 @@ func (slf *PBRpcResponseData) MakeRespone(seq uint64,err *RpcError,reply []byte)
return slf
}
func (slf *PBRpcRequestData) ReleaseRpcRequest(rpcRequestData IRpcRequestData){
}
func (slf *PBRpcRequestData) ReleaseRpcRespose(rpcRequestData IRpcRequestData){
}
func (slf *PBProcessor) Marshal(v interface{}) ([]byte, error){
return proto.Marshal(v.(proto.Message))
}
@@ -43,13 +49,26 @@ func (slf *PBProcessor) Unmarshal(data []byte, msg interface{}) error{
func (slf *PBProcessor) MakeRpcRequest(seq uint64,serviceMethod string,noReply bool,inParam []byte) IRpcRequestData{
return (&PBRpcRequestData{}).MakeRequest(seq,serviceMethod,noReply,inParam)
pPbRpcRequestData := rpcPbRequestDataPool.Get().(*PBRpcRequestData)
pPbRpcRequestData.MakeRequest(seq,serviceMethod,noReply,inParam)
return pPbRpcRequestData
}
func (slf *PBProcessor) MakeRpcResponse(seq uint64,err *RpcError,reply []byte) IRpcResponseData {
return (&PBRpcResponseData{}).MakeRespone(seq,err,reply)
pPBRpcResponseData := rpcPbResponeDataPool.Get().(*PBRpcResponseData)
pPBRpcResponseData.MakeRespone(seq,err,reply)
return pPBRpcResponseData
}
func (slf *PBProcessor) ReleaseRpcRequest(rpcRequestData IRpcRequestData){
rpcPbRequestDataPool.Put(rpcRequestData)
}
func (slf *PBProcessor) ReleaseRpcRespose(rpcRequestData IRpcRequestData){
rpcPbResponeDataPool.Put(rpcRequestData)
}
func (slf *PBRpcRequestData) IsReply() bool{
return slf.GetNoReply()
}