聊聊dubbo-go的EchoFilter

/ distributedgolang / 没有评论 / 80浏览

本文主要研究一下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{}

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)
}

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
}

GetFilter

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

// GetFilter ...
func GetFilter() filter.Filter {
	return &EchoFilter{}
}

小结

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

doc