序
本文主要研究一下dubbo-go的RPCInvocation
Invocation
dubbo-go-v1.4.2/protocol/invocation.go
代码语言:javascript复制// 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
代码语言:javascript复制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
代码语言:javascript复制// 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
代码语言:javascript复制// 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
代码语言:javascript复制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
代码语言:javascript复制// 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
代码语言:javascript复制// 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
- invocation