本文主要研究一下dubbo-go的RPCInvocation

Invocation

dubbo-go-v1.4.2/protocol/invocation.go

// Invocation ...
type Invocation interface {
    MethodName() string
    ParameterTypes() []reflect.Type
    ParameterValues() []reflect.Value
    Arguments() []interface{}
    Reply() interface{}
    Attachments() map[string]string
    AttachmentsByKey(string, string) string
    Invoker() Invoker
}
  • Invocation定义了MethodName、ParameterTypes、ParameterValues、Arguments、Reply、Attachments、AttachmentsByKey、Invoker方法

RPCInvocation

dubbo-go-v1.4.2/protocol/invocation/rpcinvocation.go

type RPCInvocation struct {
    methodName      string
    parameterTypes  []reflect.Type
    parameterValues []reflect.Value
    arguments       []interface{}
    reply           interface{}
    callBack        interface{}
    attachments     map[string]string
    invoker         protocol.Invoker
    lock            sync.RWMutex
}
  • RPCInvocation定义了methodName、parameterTypes、parameterValues、arguments、reply、callBack、attachments、invoker、lock属性

NewRPCInvocation

dubbo-go-v1.4.2/protocol/invocation/rpcinvocation.go

// NewRPCInvocation ...
func NewRPCInvocation(methodName string, arguments []interface{}, attachments map[string]string) *RPCInvocation {
    return &RPCInvocation{
        methodName:  methodName,
        arguments:   arguments,
        attachments: attachments,
    }
}
  • NewRPCInvocation方法实例化RPCInvocation

NewRPCInvocationWithOptions

dubbo-go-v1.4.2/protocol/invocation/rpcinvocation.go

// NewRPCInvocationWithOptions ...
func NewRPCInvocationWithOptions(opts ...option) *RPCInvocation {
    invo := &RPCInvocation{}
    for _, opt := range opts {
        opt(invo)
    }
    return invo
}
  • NewRPCInvocationWithOptions方法通过option实例化RPCInvocation

option

dubbo-go-v1.4.2/protocol/invocation/rpcinvocation.go

type option func(invo *RPCInvocation)

// WithMethodName ...
func WithMethodName(methodName string) option {
    return func(invo *RPCInvocation) {
        invo.methodName = methodName
    }
}

// WithParameterTypes ...
func WithParameterTypes(parameterTypes []reflect.Type) option {
    return func(invo *RPCInvocation) {
        invo.parameterTypes = parameterTypes
    }
}

// WithParameterValues ...
func WithParameterValues(parameterValues []reflect.Value) option {
    return func(invo *RPCInvocation) {
        invo.parameterValues = parameterValues
    }
}

// WithArguments ...
func WithArguments(arguments []interface{}) option {
    return func(invo *RPCInvocation) {
        invo.arguments = arguments
    }
}

// WithReply ...
func WithReply(reply interface{}) option {
    return func(invo *RPCInvocation) {
        invo.reply = reply
    }
}

// WithCallBack ...
func WithCallBack(callBack interface{}) option {
    return func(invo *RPCInvocation) {
        invo.callBack = callBack
    }
}

// WithAttachments ...
func WithAttachments(attachments map[string]string) option {
    return func(invo *RPCInvocation) {
        invo.attachments = attachments
    }
}

// WithInvoker ...
func WithInvoker(invoker protocol.Invoker) option {
    return func(invo *RPCInvocation) {
        invo.invoker = invoker
    }
}
  • option提供了WithMethodName、WithParameterTypes、WithParameterValues、WithReply、WithCallBack、WithAttachments、WithInvoker方法

AttachmentsByKey

dubbo-go-v1.4.2/protocol/invocation/rpcinvocation.go

// AttachmentsByKey ...
func (r *RPCInvocation) AttachmentsByKey(key string, defaultValue string) string {
    r.lock.RLock()
    defer r.lock.RUnlock()
    if r.attachments == nil {
        return defaultValue
    }
    value, ok := r.attachments[key]
    if ok {
        return value
    }
    return defaultValue
}
  • AttachmentsByKey方法通过r.attachments[key]获取对应key的值

SetAttachments

dubbo-go-v1.4.2/protocol/invocation/rpcinvocation.go

// SetAttachments ...
func (r *RPCInvocation) SetAttachments(key string, value string) {
    r.lock.Lock()
    defer r.lock.Unlock()
    if r.attachments == nil {
        r.attachments = make(map[string]string)
    }
    r.attachments[key] = value
}
  • SetAttachments方法通过r.attachments[key]给指定key赋值,如果r.attachments为nil则通过make创建

小结

RPCInvocation定义了methodName、parameterTypes、parameterValues、arguments、reply、callBack、attachments、invoker、lock属性;NewRPCInvocation方法实例化RPCInvocation;NewRPCInvocationWithOptions方法通过option实例化RPCInvocation

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论