gorpc之AddFunc 函数注册

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类型.

你可能感兴趣的:(gorpc之AddFunc 函数注册)