Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[optimize] rpc函数注册优化 #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 24 additions & 34 deletions rpc/base/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down