聊聊dubbo-go的EchoFilter

2020-07-16 10:08:55 浏览数 (1)

本文主要研究一下dubbo-go的EchoFilter

EchoFilter

dubbo-go-v1.4.2/filter/filter_impl/echo_filter.go

代码语言:javascript复制
const (
    // ECHO echo module name
    ECHO = "echo"
)
​
func init() {
    extension.SetFilter(ECHO, GetFilter)
}
​
// EchoFilter
// RPCService need a Echo method in consumer, if you want to use EchoFilter
// eg:
//      Echo func(ctx context.Context, arg interface{}, rsp *Xxx) error
type EchoFilter struct{}
  • EchoFilter没有定义属性

Invoke

dubbo-go-v1.4.2/filter/filter_impl/echo_filter.go

代码语言:javascript复制
// Invoke ...
func (ef *EchoFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
    logger.Infof("invoking echo filter.")
    logger.Debugf("%v,%v", invocation.MethodName(), len(invocation.Arguments()))
    if invocation.MethodName() == constant.ECHO && len(invocation.Arguments()) == 1 {
        return &protocol.RPCResult{
            Rest:  invocation.Arguments()[0],
            Attrs: invocation.Attachments(),
        }
    }
​
    return invoker.Invoke(ctx, invocation)
}
  • Invoke方法判断invocation.MethodName是否是echo,且有一个参数,是的话则返回protocol.RPCResult

OnResponse

dubbo-go-v1.4.2/filter/filter_impl/echo_filter.go

代码语言:javascript复制
// OnResponse ...
func (ef *EchoFilter) OnResponse(_ context.Context, result protocol.Result, _ protocol.Invoker,
    _ protocol.Invocation) protocol.Result {
​
    return result
}
  • OnResponse方法直接返回result

GetFilter

dubbo-go-v1.4.2/filter/filter_impl/echo_filter.go

代码语言:javascript复制
// GetFilter ...
func GetFilter() filter.Filter {
    return &EchoFilter{}
}
  • GetFilter方法创建EchoFilter

小结

EchoFilter的Invoke方法判断invocation.MethodName是否是echo,且有一个参数,是的话则返回protocol.RPCResult

doc

  • echo_filter

0 人点赞