Skip to content

Commit

Permalink
Merge pull request #1019 from yongjiapro/refactor/proxy-impl
Browse files Browse the repository at this point in the history
Refactor Proxy with ImplementFunc to allow override impl
  • Loading branch information
cityiron authored Feb 4, 2021
2 parents 28a1d7a + 926af0a commit 80157b2
Showing 1 changed file with 54 additions and 26 deletions.
80 changes: 54 additions & 26 deletions common/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 NewProxyWithOptions(invoke, callback, attachments,
WithProxyImplementFunc(DefaultProxyImplementFunc))
}

// NewProxyWithOptions create service proxy with options.
func NewProxyWithOptions(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
}

// WithProxyImplementFunc an option function to setup proxy.ImplementFunc
func WithProxyImplementFunc(f ImplementFunc) ProxyOption {
return func(p *Proxy) {
p.implement = f
}
}

// Implement
Expand All @@ -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
}

// 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)
logger.Debugf("[Implement] reflect.TypeOf: %s", valueOf.String())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit 80157b2

Please sign in to comment.