mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
优化服务自我rpc调用
This commit is contained in:
@@ -164,6 +164,10 @@ func (client *Client) removePending(seq uint64) *Call {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) FindPending(seq uint64) *Call {
|
func (client *Client) FindPending(seq uint64) *Call {
|
||||||
|
if seq == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
client.pendingLock.Lock()
|
client.pendingLock.Lock()
|
||||||
v, ok := client.pending[seq]
|
v, ok := client.pending[seq]
|
||||||
if ok == false {
|
if ok == false {
|
||||||
|
|||||||
@@ -315,54 +315,37 @@ func (handler *RpcHandler) CallMethod(client *Client,ServiceMethod string, param
|
|||||||
if v.hasResponder == true {
|
if v.hasResponder == true {
|
||||||
paramList = append(paramList, reflect.ValueOf(handler.GetRpcHandler())) //接受者
|
paramList = append(paramList, reflect.ValueOf(handler.GetRpcHandler())) //接受者
|
||||||
pCall = MakeCall()
|
pCall = MakeCall()
|
||||||
|
pCall.callback = &callBack
|
||||||
pCall.Seq = client.generateSeq()
|
pCall.Seq = client.generateSeq()
|
||||||
callSeq = pCall.Seq
|
callSeq = pCall.Seq
|
||||||
|
|
||||||
client.AddPending(pCall)
|
client.AddPending(pCall)
|
||||||
|
|
||||||
//有返回值时
|
//有返回值时
|
||||||
if reply != nil {
|
if reply != nil {
|
||||||
//如果是Call同步调用
|
//如果是Call同步调用
|
||||||
hander :=func(Returns interface{}, Err RpcError) {
|
hander :=func(Returns interface{}, Err RpcError) {
|
||||||
//如果返回错误
|
rpcCall := client.RemovePending(callSeq)
|
||||||
if len(Err)!=0 {
|
if rpcCall == nil {
|
||||||
if callBack!=requestHandlerNull {
|
log.SError("cannot find call seq ",callSeq)
|
||||||
callBack.Call([]reflect.Value{reflect.ValueOf(reply), reflect.ValueOf(errors.New(Err.Error()))})
|
|
||||||
}else{
|
|
||||||
rpcCall := client.FindPending(callSeq)
|
|
||||||
//如果找不到,说明已经超时
|
|
||||||
if rpcCall!= nil {
|
|
||||||
rpcCall.Err = errors.New(Err.Error())
|
|
||||||
rpcCall.Done()
|
|
||||||
}else{
|
|
||||||
log.SError("cannot find call seq ",callSeq)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//解析数据
|
//解析数据
|
||||||
_, processor := GetProcessorType(Returns)
|
if len(Err)!=0 {
|
||||||
bytes,errs := processor.Marshal(Returns)
|
rpcCall.Err = Err
|
||||||
if errs == nil {
|
}else if Returns != nil {
|
||||||
errs = processor.Unmarshal(bytes,reply)
|
_, processor := GetProcessorType(Returns)
|
||||||
}
|
var bytes []byte
|
||||||
|
bytes,rpcCall.Err = processor.Marshal(Returns)
|
||||||
if callBack!=requestHandlerNull {
|
if rpcCall.Err == nil {
|
||||||
if errs != nil {
|
rpcCall.Err = processor.Unmarshal(bytes,reply)
|
||||||
callBack.Call([]reflect.Value{reflect.ValueOf(reply), reflect.ValueOf(errs)})
|
|
||||||
} else{
|
|
||||||
callBack.Call([]reflect.Value{reflect.ValueOf(reply), nilError})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rpcCall := client.FindPending(callSeq)
|
|
||||||
//如果找不到,说明已经超时
|
//如果找不到,说明已经超时
|
||||||
if rpcCall!= nil {
|
rpcCall.Reply = reply
|
||||||
rpcCall.Err = errs
|
rpcCall.done<-rpcCall
|
||||||
rpcCall.done<-rpcCall
|
|
||||||
}else{
|
|
||||||
log.SError("cannot find call seq ",callSeq)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
paramList = append(paramList, reflect.ValueOf(hander))
|
paramList = append(paramList, reflect.ValueOf(hander))
|
||||||
}else{//无返回值时,是一个requestHandlerNull空回调
|
}else{//无返回值时,是一个requestHandlerNull空回调
|
||||||
@@ -409,10 +392,18 @@ func (handler *RpcHandler) CallMethod(client *Client,ServiceMethod string, param
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if pCall != nil {
|
rpcCall := client.FindPending(callSeq)
|
||||||
err = pCall.Done().Err
|
if rpcCall!=nil {
|
||||||
client.RemovePending(pCall.Seq)
|
err = rpcCall.Done().Err
|
||||||
ReleaseCall(pCall)
|
if rpcCall.callback!= nil {
|
||||||
|
valErr := nilError
|
||||||
|
if rpcCall.Err != nil {
|
||||||
|
valErr = reflect.ValueOf(rpcCall.Err)
|
||||||
|
}
|
||||||
|
rpcCall.callback.Call([]reflect.Value{reflect.ValueOf(rpcCall.Reply), valErr})
|
||||||
|
}
|
||||||
|
client.RemovePending(rpcCall.Seq)
|
||||||
|
ReleaseCall(rpcCall)
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user