type Dispatcher struct {
serviceMap map[string]*serviceData
}
type serviceData struct {
sv reflect.Value
funcMap map[string]*funcData
}
type funcData struct {
inNum int
reqt reflect.Type
fv reflect.Value
}
func (d *Dispatcher) AddFunc(funcName string, f interface{}) {
sd, ok := d.serviceMap[""]
if !ok {
sd = &serviceData{
funcMap: make(map[string]* ),
}
d.serviceMap[""] = sd
}
if _, ok := sd.funcMap[funcName]; ok {
logPanic("gorpc.Dispatcher: function %s has been already registered", funcName)
}
fd := &funcData{
fv: reflect.Indirect(reflect.ValueOf(f)),
}
var err error
if fd.inNum, fd.reqt, err = validateFunc(funcName, fd.fv, false); err != nil {
logPanic("gorpc.Dispatcher: %s", err)
}
sd.funcMap[funcName] = fd
}
- 函數對func進行注冊,serviceMap[""]作為函數路由
- 首先會在funcMap進行查找,避免重復
- fd作為函數的具體類型,inNum是參數個數,reqt是函數傳參類型,fv是函數類型
- fd注冊操作再validateFunc中進行
- serviceMap[""].funcMap[funcName] = fd 完成最終注冊
func validateFunc(funcName string, fv reflect.Value, isMethod bool) (inNum int, reqt reflect.Type, err error) {
if funcName == "" {
err = fmt.Errorf("funcName cannot be empty")
return
}
ft := fv.Type()
if ft.Kind() != reflect.Func {
err = fmt.Errorf("function [%s] must be a function instead of %s", funcName, ft)
return
}
inNum = ft.NumIn()
outNum := ft.NumOut()
dt := 0
if isMethod {
dt = 1
}
if inNum == 2+dt {
if ft.In(dt).Kind() != reflect.String {
err = fmt.Errorf("unexpected type for the first argument of the function [%s]: [%s]. Expected string", funcName, ft.In(dt))
return
}
} else if inNum > 2+dt {
err = fmt.Errorf("unexpected number of arguments in the function [%s]: %d. Expected 0, 1 (request) or 2 (clientAddr, request)", funcName, inNum-dt)
return
}
if outNum == 2 {
if !isErrorType(ft.Out(1)) {
err = fmt.Errorf("unexpected type for the second return value of the function [%s]: [%s]. Expected [%s]", funcName, ft.Out(1), errt)
return
}
} else if outNum > 2 {
err = fmt.Errorf("unexpected number of return values for the function %s: %d. Expected 0, 1 (response) or 2 (response, error)", funcName, outNum)
return
}
if inNum > dt {
reqt = ft.In(inNum - 1)
if err = registerType("request", funcName, reqt); err != nil {
return
}
}
if outNum > 0 {
respt := ft.Out(0)
if !isErrorType(respt) {
if err = registerType("response", funcName, ft.Out(0)); err != nil {
return
}
}
}
return
}
- 看了一下有些限制,首先inNum都為1,outNum小于等于2
即參數1個,返回值最多2個,且如果2個,則第2個必為error類型.