From ffe4df1e20e76495816ba54ef499f7a7d73c554f Mon Sep 17 00:00:00 2001 From: yc90s <1050676515@qq.com> Date: Thu, 7 Jul 2022 17:20:39 +0800 Subject: [PATCH] =?UTF-8?q?[optimize]=20rpc=E5=87=BD=E6=95=B0=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rpc/base/rpc_server.go | 58 +++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/rpc/base/rpc_server.go b/rpc/base/rpc_server.go index 41db962..8f14f07 100644 --- a/rpc/base/rpc_server.go +++ b/rpc/base/rpc_server.go @@ -80,44 +80,12 @@ func (s *RPCServer) GetExecuting() int64 { // you must call the function before calling Open and Go func (s *RPCServer) Register(id string, f interface{}) { - - if _, ok := s.functions[id]; ok { - panic(fmt.Sprintf("function id %v: already registered", id)) - } - finfo := &mqrpc.FunctionInfo{ - Function: reflect.ValueOf(f), - FuncType: reflect.ValueOf(f).Type(), - Goroutine: false, - } - - finfo.InType = []reflect.Type{} - for i := 0; i < finfo.FuncType.NumIn(); i++ { - rv := finfo.FuncType.In(i) - finfo.InType = append(finfo.InType, rv) - } - s.functions[id] = finfo - + s._register(id, f, false) } // you must call the function before calling Open and Go func (s *RPCServer) RegisterGO(id string, f interface{}) { - - if _, ok := s.functions[id]; ok { - panic(fmt.Sprintf("function id %v: already registered", id)) - } - - finfo := &mqrpc.FunctionInfo{ - Function: reflect.ValueOf(f), - FuncType: reflect.ValueOf(f).Type(), - Goroutine: true, - } - - finfo.InType = []reflect.Type{} - for i := 0; i < finfo.FuncType.NumIn(); i++ { - rv := finfo.FuncType.In(i) - finfo.InType = append(finfo.InType, rv) - } - s.functions[id] = finfo + s._register(id, f, true) } func (s *RPCServer) Done() (err error) { @@ -182,6 +150,28 @@ func (s *RPCServer) doCallback(callInfo *mqrpc.CallInfo) { } } +func (s *RPCServer) _register(id string, f interface{}, goroutine bool) { + + if _, ok := s.functions[id]; ok { + panic(fmt.Sprintf("function id %v: already registered", id)) + } + + finfo := &mqrpc.FunctionInfo{ + Function: reflect.ValueOf(f), + FuncType: reflect.ValueOf(f).Type(), + Goroutine: goroutine, + } + + finfo.InType = []reflect.Type{} + for i := 0; i < finfo.FuncType.NumIn(); i++ { + rv := finfo.FuncType.In(i) + finfo.InType = append(finfo.InType, rv) + } + s.functions[id] = finfo +} + + + func (s *RPCServer) _errorCallback(start time.Time, callInfo *mqrpc.CallInfo, Cid string, Error string) { //异常日志都应该打印 //log.TError(span, "rpc Exec ModuleType = %v Func = %v Elapsed = %v ERROR:\n%v", s.module.GetType(), callInfo.RPCInfo.Fn, time.Since(start), Error)