From 12411839369d634b2cfd1e0c8d6fbceec7943fb6 Mon Sep 17 00:00:00 2001 From: chenyongjia Date: Mon, 25 Jan 2021 17:52:47 +0800 Subject: [PATCH 1/3] Refactor Proxy with ImplementFunc to allow override impl --- common/proxy/proxy.go | 80 +++++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/common/proxy/proxy.go b/common/proxy/proxy.go index 55f533cb2b..56ba890b6a 100644 --- a/common/proxy/proxy.go +++ b/common/proxy/proxy.go @@ -40,23 +40,47 @@ import ( type Proxy struct { rpc common.RPCService invoke protocol.Invoker - callBack interface{} + callback interface{} attachments map[string]string - - once sync.Once + implement ImplementFunc + once sync.Once } +type ( + // ProxyOption a function to init Proxy with options + // nolint + ProxyOption func(p *Proxy) + // ImplementFunc function for proxy impl of RPCService functions + ImplementFunc func(p *Proxy, v common.RPCService) +) + var ( typError = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem()).Type() ) // NewProxy create service proxy. -func NewProxy(invoke protocol.Invoker, callBack interface{}, attachments map[string]string) *Proxy { - return &Proxy{ +func NewProxy(invoke protocol.Invoker, callback interface{}, attachments map[string]string) *Proxy { + return NewProxyWith(invoke, callback, attachments, + WithProxyImplementFunc(DefaultProxyImplementFunc)) +} + +// NewProxy create service proxy. +func NewProxyWith(invoke protocol.Invoker, callback interface{}, attachments map[string]string, opts ...ProxyOption) *Proxy { + p := &Proxy{ invoke: invoke, - callBack: callBack, + callback: callback, attachments: attachments, } + for _, opt := range opts { + opt(p) + } + return p +} + +func WithProxyImplementFunc(f ImplementFunc) ProxyOption { + return func(p *Proxy) { + p.implement = f + } } // Implement @@ -66,6 +90,29 @@ func NewProxy(invoke protocol.Invoker, callBack interface{}, attachments map[str // Yyy func(ctx context.Context, args []interface{}, rsp *Zzz) error // } func (p *Proxy) Implement(v common.RPCService) { + p.once.Do(func() { + p.implement(p, v) + p.rpc = v + }) + +} + +// Get gets rpc service instance. +func (p *Proxy) Get() common.RPCService { + return p.rpc +} + +// GetCallback gets callback. +func (p *Proxy) GetCallback() interface{} { + return p.callback +} + +// GetInvoker gets Invoker. +func (p *Proxy) GetInvoker() protocol.Invoker { + return p.invoke +} + +func DefaultProxyImplementFunc(p *Proxy, v common.RPCService) { // check parameters, incoming interface must be a elem's pointer. valueOf := reflect.ValueOf(v) logger.Debugf("[Implement] reflect.TypeOf: %s", valueOf.String()) @@ -138,7 +185,7 @@ func (p *Proxy) Implement(v common.RPCService) { inv = invocation_impl.NewRPCInvocationWithOptions(invocation_impl.WithMethodName(methodName), invocation_impl.WithArguments(inIArr), invocation_impl.WithReply(reply.Interface()), - invocation_impl.WithCallBack(p.callBack), invocation_impl.WithParameterValues(inVArr)) + invocation_impl.WithCallBack(p.callback), invocation_impl.WithParameterValues(inVArr)) for k, value := range p.attachments { inv.SetAttachments(k, value) @@ -215,23 +262,4 @@ func (p *Proxy) Implement(v common.RPCService) { } } - p.once.Do(func() { - p.rpc = v - }) - -} - -// Get gets rpc service instance. -func (p *Proxy) Get() common.RPCService { - return p.rpc -} - -// GetCallback gets callback. -func (p *Proxy) GetCallback() interface{} { - return p.callBack -} - -// GetInvoker gets Invoker. -func (p *Proxy) GetInvoker() protocol.Invoker { - return p.invoke } From 465a91afadf20b878d193d1f150bc155328d9408 Mon Sep 17 00:00:00 2001 From: chenyongjia Date: Wed, 27 Jan 2021 10:17:34 +0800 Subject: [PATCH 2/3] Rename and format code --- common/proxy/proxy.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/common/proxy/proxy.go b/common/proxy/proxy.go index 56ba890b6a..71f465bcf4 100644 --- a/common/proxy/proxy.go +++ b/common/proxy/proxy.go @@ -48,7 +48,6 @@ type Proxy struct { type ( // ProxyOption a function to init Proxy with options - // nolint ProxyOption func(p *Proxy) // ImplementFunc function for proxy impl of RPCService functions ImplementFunc func(p *Proxy, v common.RPCService) @@ -60,12 +59,12 @@ var ( // NewProxy create service proxy. func NewProxy(invoke protocol.Invoker, callback interface{}, attachments map[string]string) *Proxy { - return NewProxyWith(invoke, callback, attachments, + return NewProxyWithOptions(invoke, callback, attachments, WithProxyImplementFunc(DefaultProxyImplementFunc)) } // NewProxy create service proxy. -func NewProxyWith(invoke protocol.Invoker, callback interface{}, attachments map[string]string, opts ...ProxyOption) *Proxy { +func NewProxyWithOptions(invoke protocol.Invoker, callback interface{}, attachments map[string]string, opts ...ProxyOption) *Proxy { p := &Proxy{ invoke: invoke, callback: callback, @@ -94,7 +93,6 @@ func (p *Proxy) Implement(v common.RPCService) { p.implement(p, v) p.rpc = v }) - } // Get gets rpc service instance. From 926af0a5afb0e0db1c65d2058c6737ed5ca9a3a5 Mon Sep 17 00:00:00 2001 From: chenyongjia Date: Wed, 27 Jan 2021 10:23:27 +0800 Subject: [PATCH 3/3] Doc: add comments --- common/proxy/proxy.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/common/proxy/proxy.go b/common/proxy/proxy.go index 71f465bcf4..fd3481021c 100644 --- a/common/proxy/proxy.go +++ b/common/proxy/proxy.go @@ -63,7 +63,7 @@ func NewProxy(invoke protocol.Invoker, callback interface{}, attachments map[str WithProxyImplementFunc(DefaultProxyImplementFunc)) } -// NewProxy create service proxy. +// NewProxyWithOptions create service proxy with options. func NewProxyWithOptions(invoke protocol.Invoker, callback interface{}, attachments map[string]string, opts ...ProxyOption) *Proxy { p := &Proxy{ invoke: invoke, @@ -76,6 +76,7 @@ func NewProxyWithOptions(invoke protocol.Invoker, callback interface{}, attachme return p } +// WithProxyImplementFunc an option function to setup proxy.ImplementFunc func WithProxyImplementFunc(f ImplementFunc) ProxyOption { return func(p *Proxy) { p.implement = f @@ -110,6 +111,7 @@ func (p *Proxy) GetInvoker() protocol.Invoker { return p.invoke } +// DefaultProxyImplementFunc the default function for proxy impl func DefaultProxyImplementFunc(p *Proxy, v common.RPCService) { // check parameters, incoming interface must be a elem's pointer. valueOf := reflect.ValueOf(v)