聊聊dubbo-go的RPCInvocation

/ distributedgolang / 没有评论 / 10浏览

本文主要研究一下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
}

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
}

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,
	}
}

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
}

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
	}
}

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
}

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
}

小结

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

doc