转载

聊聊dubbo-go的EchoFilter

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

EchoFilter

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

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

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

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

// GetFilter ...
func GetFilter() filter.Filter {
    return &EchoFilter{}
}
  • GetFilter方法创建EchoFilter

小结

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

doc

  • echo_filter
原文  https://segmentfault.com/a/1190000023248393
正文到此结束
Loading...