优化rpc返回协议

Signed-off-by: boyce <boyce@atbc.com>
This commit is contained in:
boyce
2020-07-10 16:17:56 +08:00
parent 7e72782c16
commit 93b9c4f89a
9 changed files with 46 additions and 42 deletions

View File

@@ -105,7 +105,7 @@ func (slf *Client) generateSeq() uint64{
return atomic.AddUint64(&slf.startSeq,1)
}
func (slf *Client) AsycGo(rpcHandler IRpcHandler,serviceMethod string,callback reflect.Value, args interface{},replyParam interface{}) error {
func (slf *Client) AsycCall(rpcHandler IRpcHandler,serviceMethod string,callback reflect.Value, args interface{},replyParam interface{}) error {
call := MakeCall()
call.Reply = replyParam
call.callback = &callback
@@ -161,21 +161,27 @@ func (slf *Client) Go(noReply bool,serviceMethod string, args interface{},reply
request := &RpcRequest{}
call.Arg = args
call.Seq = slf.generateSeq()
if noReply == false {
slf.AddPending(call)
}
request.RpcRequestData = processor.MakeRpcRequest(slf.startSeq,serviceMethod,noReply,InParam)
bytes,err := processor.Marshal(request.RpcRequestData)
processor.ReleaseRpcRequest(request.RpcRequestData)
if err != nil {
call.Err = err
slf.RemovePending(call.Seq)
return call
}
if slf.conn == nil {
call.Err = fmt.Errorf("call %s is fail,rpc client is disconnect.",serviceMethod)
slf.RemovePending(call.Seq)
return call
}
err = slf.conn.WriteMsg(bytes)
if err != nil {
slf.RemovePending(call.Seq)
call.Err = err
}

View File

@@ -77,7 +77,7 @@ func (slf *JsonProcessor) ReleaseRpcRespose(rpcRequestData IRpcResponseData){
rpcJsonResponeDataPool.Put(rpcRequestData)
}
func (slf *JsonRpcRequestData) IsReply() bool{
func (slf *JsonRpcRequestData) IsNoReply() bool{
return slf.NoReply
}

View File

@@ -84,7 +84,7 @@ func (slf *MsgpProcessor) ReleaseRpcRespose(rpcRequestData IRpcResponseData){
rpcResponeDataPool.Put(rpcRequestData)
}
func (slf *MsgpRpcRequestData) IsReply() bool{
func (slf *MsgpRpcRequestData) IsNoReply() bool{
return slf.NoReply
}

View File

@@ -69,7 +69,7 @@ func (slf *PBProcessor) ReleaseRpcRespose(rpcRequestData IRpcRequestData){
}
func (slf *PBRpcRequestData) IsReply() bool{
func (slf *PBRpcRequestData) IsNoReply() bool{
return slf.GetNoReply()
}

View File

@@ -22,7 +22,7 @@ type IRpcRequestData interface {
GetSeq() uint64
GetServiceMethod() string
GetInParam() []byte
IsReply() bool
IsNoReply() bool
}
type IRpcResponseData interface {

View File

@@ -10,7 +10,7 @@ import (
"unicode/utf8"
)
type FuncRpcClient func(nodeid int,serviceMethod string) ([]*Client,error)
type FuncRpcClient func(nodeid int,serviceMethod string,client *[]*Client) error
type FuncRpcServer func() (*Server)
var NilError = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem())
@@ -279,7 +279,8 @@ func (slf *RpcHandler) CallMethod(ServiceMethod string,param interface{},reply i
}
func (slf *RpcHandler) goRpc(bCast bool,nodeId int,serviceMethod string,args interface{}) error {
pClientList,err := slf.funcRpcClient(nodeId,serviceMethod)
var pClientList []*Client
err := slf.funcRpcClient(nodeId,serviceMethod,&pClientList)
if err != nil {
log.Error("Call serviceMethod is error:%+v!",err)
return err
@@ -319,7 +320,7 @@ func (slf *RpcHandler) goRpc(bCast bool,nodeId int,serviceMethod string,args int
}
//跨node调用
pCall := pClient.Go(false,serviceMethod,args,nil)
pCall := pClient.Go(true,serviceMethod,args,nil)
if pCall.Err!=nil {
err = pCall.Err
}
@@ -330,7 +331,8 @@ func (slf *RpcHandler) goRpc(bCast bool,nodeId int,serviceMethod string,args int
}
func (slf *RpcHandler) callRpc(nodeId int,serviceMethod string,args interface{},reply interface{}) error {
pClientList,err := slf.funcRpcClient(nodeId,serviceMethod)
var pClientList []*Client
err := slf.funcRpcClient(nodeId,serviceMethod,&pClientList)
if err != nil {
log.Error("Call serviceMethod is error:%+v!",err)
return err
@@ -367,6 +369,7 @@ func (slf *RpcHandler) callRpc(nodeId int,serviceMethod string,args interface{},
//跨node调用
pCall := pClient.Go(false,serviceMethod,args,reply)
if pCall.Err != nil {
ReleaseCall(pCall)
return pCall.Err
}
err = pCall.Done().Err
@@ -395,14 +398,15 @@ func (slf *RpcHandler) asyncCallRpc(nodeid int,serviceMethod string,args interfa
}
reply := reflect.New(fVal.Type().In(0).Elem()).Interface()
pClientList,err := slf.funcRpcClient(nodeid,serviceMethod)
var pClientList []*Client
err := slf.funcRpcClient(nodeid,serviceMethod,&pClientList)
if err != nil {
fVal.Call([]reflect.Value{reflect.ValueOf(reply),reflect.ValueOf(err)})
log.Error("Call serviceMethod is error:%+v!",err)
return nil
}
if len(pClientList) > 1 {
if pClientList== nil || len(pClientList) > 1 {
err := fmt.Errorf("Cannot call more then 1 node!")
fVal.Call([]reflect.Value{reflect.ValueOf(reply),reflect.ValueOf(err)})
log.Error("Cannot call more then 1 node!")
@@ -449,7 +453,7 @@ func (slf *RpcHandler) asyncCallRpc(nodeid int,serviceMethod string,args interfa
}
//跨node调用
err = pClient.AsycGo(slf,serviceMethod,fVal,args,reply)
err = pClient.AsycCall(slf,serviceMethod,fVal,args,reply)
if err != nil {
fVal.Call([]reflect.Value{reflect.ValueOf(reply),reflect.ValueOf(err)})
}

View File

@@ -140,7 +140,7 @@ func (agent *RpcAgent) Run() {
continue
}
if req.RpcRequestData.IsReply()== false {
if req.RpcRequestData.IsNoReply()==false {
req.requestHandle = func(Returns interface{},Err *RpcError){
agent.WriteRespone(req.RpcRequestData.GetServiceMethod(),req.RpcRequestData.GetSeq(),Returns,Err)
}
@@ -149,7 +149,11 @@ func (agent *RpcAgent) Run() {
err = rpcHandler.PushRequest(req)
if err != nil {
rpcError := RpcError(err.Error())
agent.WriteRespone(req.RpcRequestData.GetServiceMethod(),req.RpcRequestData.GetSeq(),nil,&rpcError)
if req.RpcRequestData.IsNoReply() {
agent.WriteRespone(req.RpcRequestData.GetServiceMethod(),req.RpcRequestData.GetSeq(),nil,&rpcError)
}
processor.ReleaseRpcRequest(req.RpcRequestData)
ReleaseRpcRequest(req)
}