Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Proxy with ImplementFunc to allow override impl #1019

Merged
merged 3 commits into from
Feb 4, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Refactor Proxy with ImplementFunc to allow override impl
  • Loading branch information
chenyongjia committed Jan 25, 2021
commit 78652c767c5d0459c92fc5a4c07b2695a274f78b
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
// 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
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
}

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
}