转载

聊聊dubbo-go的TpsLimitFilter

本文主要研究一下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 {
}
  • TpsLimitFilter的init设置了GetTpsLimitFilter

GetTpsLimitFilter

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

// GetTpsLimitFilter ...
func GetTpsLimitFilter() filter.Filter {
    return &TpsLimitFilter{}
}
  • GetTpsLimitFilter实例化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)
}
  • 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)

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
}
  • OnResponse目前不做任何操作

小结

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

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