sequence
This paper mainly studies the RPCInvocation of dubo-go
Invocation
Dubbo – go – v1.4.2 / protocol/invocation. Go
// Invocation ...
typeInvocation interface { MethodName() string ParameterTypes() []reflect.Type ParameterValues() []reflect.Value Arguments() []interface{} Reply() interface{} Attachments() map[string]string AttachmentsByKey(string, string) string Invoker() Invoker }Copy the code
- The Invocation defines MethodName, ParameterTypes, ParameterValues, Arguments, Reply, Attachments, AttachmentsByKey, Invoker methods
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
}
Copy the code
- The RPCInvocation defines methodName, parameterTypes, parameterValues, Arguments, Reply, callBack, attachments, Invoker, lock attributes
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,
}
}
Copy the code
- The NewRPCInvocation method instantiates the 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
}
Copy the code
- Through option instantiation RPCInvocation NewRPCInvocationWithOptions method
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
}
}
Copy the code
- Option provides methods WithMethodName, WithParameterTypes, WithParameterValues, WithReply, WithCallBack, WithAttachments, and 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
}
Copy the code
- The AttachmentsByKey method obtains the corresponding key value through R. Atachments [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
}
Copy the code
- The SetAttachments method assigns the specified key to r.Atachments [key], or creates it by make if R.atachments is nil
summary
RPCInvocation defines methodName, parameterTypes, parameterValues, Arguments, Reply, callBack, attachments, Invoker, lock; The NewRPCInvocation method instantiates the RPCInvocation; Through option instantiation RPCInvocation NewRPCInvocationWithOptions method
doc
- invocation