聊聊dubbo-go的TpsLimitFilter

/ distributedgolang / 没有评论 / 40浏览

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

TpsLimitFilter

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

const (
	// TpsLimitFilterKey key
	TpsLimitFilterKey = "tps"
)

func init() {
	extension.SetFilter(TpsLimitFilterKey, GetTpsLimitFilter)
}

type TpsLimitFilter struct {
}

GetTpsLimitFilter

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

// GetTpsLimitFilter ...
func GetTpsLimitFilter() filter.Filter {
	return &TpsLimitFilter{}
}

Invoke

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

// Invoke ...
func (t TpsLimitFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
	url := invoker.GetUrl()
	tpsLimiter := url.GetParam(constant.TPS_LIMITER_KEY, "")
	rejectedExeHandler := url.GetParam(constant.TPS_REJECTED_EXECUTION_HANDLER_KEY, constant.DEFAULT_KEY)
	if len(tpsLimiter) > 0 {
		allow := extension.GetTpsLimiter(tpsLimiter).IsAllowable(invoker.GetUrl(), invocation)
		if allow {
			return invoker.Invoke(ctx, invocation)
		}
		logger.Errorf("The invocation was rejected due to over the tps limitation, url: %s ", url.String())
		return extension.GetRejectedExecutionHandler(rejectedExeHandler).RejectedExecution(url, invocation)
	}
	return invoker.Invoke(ctx, invocation)
}

OnResponse

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

// OnResponse ...
func (t TpsLimitFilter) OnResponse(_ context.Context, result protocol.Result, _ protocol.Invoker,
	_ protocol.Invocation) protocol.Result {
	return result
}

小结

TpsLimitFilter的Invoke方法读取constant.TPS_LIMITER_KEY及constant.TPS_REJECTED_EXECUTION_HANDLER_KEY;对于有配置tpsLimiter的执行extension.GetTpsLimiter(tpsLimiter).IsAllowable(invoker.GetUrl(), invocation),对于allow的执行invoker.Invoke(ctx, invocation),非allow的执行extension.GetRejectedExecutionHandler(rejectedExeHandler).RejectedExecution(url, invocation)

doc