Skip to content

Commit

Permalink
move filter keys to constant package
Browse files Browse the repository at this point in the history
  • Loading branch information
justxuewei committed Jul 8, 2021
1 parent 8877e87 commit 9621ebf
Show file tree
Hide file tree
Showing 25 changed files with 67 additions and 81 deletions.
6 changes: 3 additions & 3 deletions common/constant/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ const (
const (
DEFAULT_KEY = "default"
PREFIX_DEFAULT_KEY = "default."
DEFAULT_SERVICE_FILTERS = "echo,token,accesslog,tps,generic_service,execute,pshutdown"
DEFAULT_REFERENCE_FILTERS = "cshutdown"
GENERIC_REFERENCE_FILTERS = "generic"
DEFAULT_SERVICE_FILTERS = EchoFilterKey+","+TokenFilterKey+","+AccessLogFilterKey+","+TpsLimitFilterKey+","+GenericServiceFilterKey+","+ExecuteLimitFilterKey+","+GracefulShutdownProviderFilterKey
DEFAULT_REFERENCE_FILTERS = GracefulShutdownConsumerFilterKey
GENERIC_REFERENCE_FILTERS = GenericFilterKey
GENERIC = "$invoke"
ECHO = "$echo"
)
Expand Down
31 changes: 24 additions & 7 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ const (
REFERENCE_FILTER_KEY = "reference.filter"
)

// Filter Keys
const (
AccessLogFilterKey = "accesslog"
ActiveFilterKey = "active"
AuthConsumerFilterKey = "sign"
AuthProviderFilterKey = "auth"
EchoFilterKey = "echo"
ExecuteLimitFilterKey = "execute"
GenericFilterKey = "generic"
GenericServiceFilterKey = "generic_service"
GracefulShutdownProviderFilterKey = "pshutdown"
GracefulShutdownConsumerFilterKey = "cshutdown"
HystrixConsumerFilterKey = "hystrix_consumer"
HystrixProviderFilterKey = "hystrix_provider"
MetricsFilterKey = "metrics"
SeataFilterKey = "seata"
SentinelProviderFilterKey = "sentinel-provider"
SentinelConsumerFilterKey = "sentinel-consumer"
TokenFilterKey = "token"
TpsLimitFilterKey = "tps"
TracingFilterKey = "tracing"
)

const (
TIMESTAMP_KEY = "timestamp"
REMOTE_TIMESTAMP_KEY = "remote.timestamp"
Expand All @@ -74,7 +97,6 @@ const (
FORKS_KEY = "forks"
DEFAULT_FORKS = 2
DEFAULT_TIMEOUT = 1000
ACCESS_LOG_KEY = "accesslog"
TPS_LIMITER_KEY = "tps.limiter"
TPS_REJECTED_EXECUTION_HANDLER_KEY = "tps.limit.rejected.handler"
TPS_LIMIT_RATE_KEY = "tps.limit.rate"
Expand All @@ -85,8 +107,6 @@ const (
EXECUTE_LIMIT_KEY = "execute.limit"
DEFAULT_EXECUTE_LIMIT = "-1"
EXECUTE_REJECTED_EXECUTION_HANDLER_KEY = "execute.limit.rejected.handler"
PROVIDER_SHUTDOWN_FILTER = "pshutdown"
CONSUMER_SHUTDOWN_FILTER = "cshutdown"
SERIALIZATION_KEY = "serialization"
PID_KEY = "pid"
SYNC_REPORT_KEY = "sync.report"
Expand Down Expand Up @@ -251,11 +271,8 @@ const (
AttachmentKey = DubboCtxKey("attachment")
)

// Auth filter
const (
// name of consumer sign filter
CONSUMER_SIGN_FILTER = "sign"
// name of consumer sign filter
PROVIDER_AUTH_FILTER = "auth"
// name of service filter
SERVICE_AUTH_KEY = "auth"
// key of authenticator
Expand Down
4 changes: 2 additions & 2 deletions config/config_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ func TestConfigLoader(t *testing.T) {
}

func TestLoad(t *testing.T) {
extension.SetFilter(constant.CONSUMER_SHUTDOWN_FILTER, func() filter.Filter {
extension.SetFilter(constant.GracefulShutdownConsumerFilterKey, func() filter.Filter {
return &mockGracefulShutdownFilter{}
})
extension.SetFilter(constant.PROVIDER_SHUTDOWN_FILTER, func() filter.Filter {
extension.SetFilter(constant.GracefulShutdownProviderFilterKey, func() filter.Filter {
return &mockGracefulShutdownFilter{}
})

Expand Down
4 changes: 2 additions & 2 deletions config/graceful_shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ func GracefulShutdownInit() {
signal.Notify(signals, ShutdownSignals...)

// retrieve ShutdownConfig for gracefulShutdownFilter
if filter, ok := extension.GetFilter(constant.CONSUMER_SHUTDOWN_FILTER).(Setter); ok && GetConsumerConfig().ShutdownConfig != nil {
if filter, ok := extension.GetFilter(constant.GracefulShutdownConsumerFilterKey).(Setter); ok && GetConsumerConfig().ShutdownConfig != nil {
filter.Set(GracefulShutdownFilterShutdownConfig, GetConsumerConfig().ShutdownConfig)
}
if filter, ok := extension.GetFilter(constant.PROVIDER_SHUTDOWN_FILTER).(Setter); ok && GetProviderConfig().ShutdownConfig != nil {
if filter, ok := extension.GetFilter(constant.GracefulShutdownProviderFilterKey).(Setter); ok && GetProviderConfig().ShutdownConfig != nil {
filter.Set(GracefulShutdownFilterShutdownConfig, GetProviderConfig().ShutdownConfig)
}

Expand Down
4 changes: 2 additions & 2 deletions config/graceful_shutdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import (
)

func TestGracefulShutdownInit(t *testing.T) {
extension.SetFilter(constant.CONSUMER_SHUTDOWN_FILTER, func() filter.Filter {
extension.SetFilter(constant.GracefulShutdownConsumerFilterKey, func() filter.Filter {
return &mockGracefulShutdownFilter{}
})
extension.SetFilter(constant.PROVIDER_SHUTDOWN_FILTER, func() filter.Filter {
extension.SetFilter(constant.GracefulShutdownProviderFilterKey, func() filter.Filter {
return &mockGracefulShutdownFilter{}
})
GracefulShutdownInit()
Expand Down
2 changes: 1 addition & 1 deletion config/reference_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func (p *mockRegistryProtocol) GetRegistries() []registry.Registry {

func mockFilter() {
consumerFiler := &mockShutdownFilter{}
extension.SetFilter(constant.CONSUMER_SHUTDOWN_FILTER, func() filter.Filter {
extension.SetFilter(constant.GracefulShutdownConsumerFilterKey, func() filter.Filter {
return consumerFiler
})
}
Expand Down
2 changes: 1 addition & 1 deletion config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (c *ServiceConfig) getUrlMap() url.Values {
urlMap.Set(constant.SERVICE_FILTER_KEY, mergeValue(providerConfig.Filter, c.Filter, constant.DEFAULT_SERVICE_FILTERS))

// filter special config
urlMap.Set(constant.ACCESS_LOG_KEY, c.AccessLog)
urlMap.Set(constant.AccessLogFilterKey, c.AccessLog)
// tps limiter
urlMap.Set(constant.TPS_LIMIT_STRATEGY_KEY, c.TpsLimitStrategy)
urlMap.Set(constant.TPS_LIMIT_INTERVAL_KEY, c.TpsLimitInterval)
Expand Down
10 changes: 4 additions & 6 deletions filter/accesslog/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ const (
)

func init() {
extension.SetFilter(constant.ACCESS_LOG_KEY, func() filter.Filter {
return NewFilter()
})
extension.SetFilter(constant.AccessLogFilterKey, newFilter)
}

// Filter for Access Log
Expand All @@ -79,9 +77,9 @@ type Filter struct {
}

// Invoke will check whether user wants to use this filter.
// If we find the value of key constant.ACCESS_LOG_KEY, we will log the invocation info
// If we find the value of key constant.AccessLogFilterKey, we will log the invocation info
func (f *Filter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
accessLog := invoker.GetURL().GetParam(constant.ACCESS_LOG_KEY, "")
accessLog := invoker.GetURL().GetParam(constant.AccessLogFilterKey, "")

// the user do not
if len(accessLog) > 0 {
Expand Down Expand Up @@ -222,7 +220,7 @@ func isDefault(accessLog string) bool {
return strings.EqualFold("true", accessLog) || strings.EqualFold("default", accessLog)
}

func NewFilter() *Filter {
func newFilter() filter.Filter {
accessLogFilter := &Filter{logChan: make(chan Data, LogMaxBuffer)}
go func() {
for accessLogData := range accessLogFilter.logChan {
Expand Down
4 changes: 2 additions & 2 deletions filter/active/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/filter"
Expand All @@ -32,11 +33,10 @@ import (

const (
dubboInvokeStartTime = "dubboInvokeStartTime"
Active = "active"
)

func init() {
extension.SetFilter(Active, func() filter.Filter {
extension.SetFilter(constant.ActiveFilterKey, func() filter.Filter {
return &Filter{}
})
}
Expand Down
4 changes: 2 additions & 2 deletions filter/auth/consumer_sign_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import (
)

func init() {
extension.SetFilter(constant.CONSUMER_SIGN_FILTER, func() filter.Filter {
extension.SetFilter(constant.AuthConsumerFilterKey, func() filter.Filter {
return &ConsumerSignFilter{}
})
extension.SetFilter(constant.PROVIDER_AUTH_FILTER, func() filter.Filter {
extension.SetFilter(constant.AuthProviderFilterKey, func() filter.Filter {
return &ProviderAuthFilter{}
})
}
Expand Down
2 changes: 1 addition & 1 deletion filter/auth/provider_auth_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

func init() {
extension.SetFilter(constant.PROVIDER_AUTH_FILTER, newProviderAuthFilter)
extension.SetFilter(constant.AuthProviderFilterKey, newProviderAuthFilter)
}

// ProviderAuthFilter verifies the correctness of the signature on provider side
Expand Down
6 changes: 1 addition & 5 deletions filter/echo/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ import (
"dubbo.apache.org/dubbo-go/v3/protocol"
)

const (
Echo = "echo"
)

func init() {
extension.SetFilter(Echo, func() filter.Filter {
extension.SetFilter(constant.EchoFilterKey, func() filter.Filter {
return &Filter{}
})
}
Expand Down
12 changes: 3 additions & 9 deletions filter/execlmt/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@ import (
"dubbo.apache.org/dubbo-go/v3/protocol"
)

const (
ExecuteLimit = "execute"
)

func init() {
extension.SetFilter(ExecuteLimit, func() filter.Filter {
return NewFilter()
})
extension.SetFilter(constant.ExecuteLimitFilterKey, newFilter)
}

// Filter will limit the number of in-progress request and it's thread-safe.
Expand Down Expand Up @@ -141,8 +135,8 @@ var (
executeLimitFilter *Filter
)

// NewFilter returns the singleton Filter instance
func NewFilter() *Filter {
// newFilter returns the singleton Filter instance
func newFilter() filter.Filter {
executeLimitOnce.Do(func() {
executeLimitFilter = &Filter{
executeState: concurrent.NewMap(),
Expand Down
6 changes: 3 additions & 3 deletions filter/execlmt/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestFilterInvokeIgnored(t *testing.T) {
common.WithParams(url.Values{}),
common.WithParamsValue(constant.INTERFACE_KEY, methodName))

limitFilter := NewFilter()
limitFilter := newFilter()

result := limitFilter.Invoke(context.Background(), protocol.NewBaseInvoker(invokeUrl), invoc)
assert.NotNil(t, result)
Expand All @@ -59,7 +59,7 @@ func TestFilterInvokeConfigureError(t *testing.T) {
common.WithParamsValue(constant.EXECUTE_LIMIT_KEY, "13a"),
)

limitFilter := NewFilter()
limitFilter := newFilter()

result := limitFilter.Invoke(context.Background(), protocol.NewBaseInvoker(invokeUrl), invoc)
assert.NotNil(t, result)
Expand All @@ -76,7 +76,7 @@ func TestFilterInvoke(t *testing.T) {
common.WithParamsValue(constant.EXECUTE_LIMIT_KEY, "20"),
)

limitFilter := NewFilter()
limitFilter := newFilter()

result := limitFilter.Invoke(context.Background(), protocol.NewBaseInvoker(invokeUrl), invoc)
assert.NotNil(t, result)
Expand Down
6 changes: 1 addition & 5 deletions filter/generic/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ import (
invocation2 "dubbo.apache.org/dubbo-go/v3/protocol/invocation"
)

const (
Generic = "generic"
)

func init() {
extension.SetFilter(Generic, func() filter.Filter {
extension.SetFilter(constant.GenericFilterKey, func() filter.Filter {
return &Filter{}
})
}
Expand Down
3 changes: 1 addition & 2 deletions filter/generic/service_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ import (
)

const (
GenericService = "generic_service"
// nolint
GENERIC_SERIALIZATION_DEFAULT = "true"
)

func init() {
extension.SetFilter(GenericService, func() filter.Filter {
extension.SetFilter(constant.GenericServiceFilterKey, func() filter.Filter {
return &ServiceFilter{}
})
}
Expand Down
4 changes: 2 additions & 2 deletions filter/gshutdown/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import (

func init() {
// `init()` is performed before config.Load(), so shutdownConfig will be retrieved after config was loaded.
extension.SetFilter(constant.CONSUMER_SHUTDOWN_FILTER, func() filter.Filter {
extension.SetFilter(constant.GracefulShutdownConsumerFilterKey, func() filter.Filter {
return &Filter{}
})
extension.SetFilter(constant.PROVIDER_SHUTDOWN_FILTER, func() filter.Filter {
extension.SetFilter(constant.GracefulShutdownProviderFilterKey, func() filter.Filter {
return &Filter{}
})
}
Expand Down
2 changes: 1 addition & 1 deletion filter/gshutdown/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGenericFilterInvoke(t *testing.T) {
invoc := invocation.NewRPCInvocation("GetUser", []interface{}{"OK"}, make(map[string]interface{}))
invokeUrl := common.NewURLWithOptions(common.WithParams(url.Values{}))

shutdownFilter := extension.GetFilter(constant.PROVIDER_SHUTDOWN_FILTER).(*Filter)
shutdownFilter := extension.GetFilter(constant.GracefulShutdownProviderFilterKey).(*Filter)

providerConfig := config.GetProviderConfig()

Expand Down
7 changes: 3 additions & 4 deletions filter/hystrix/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
)

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/config"
Expand All @@ -41,8 +42,6 @@ import (
const (
// nolint
HYSTRIX = "hystrix"
HystrixConsumer = "hystrix_consumer"
HystrixProvider = "hystrix_provider"
)

var (
Expand All @@ -54,8 +53,8 @@ var (
)

func init() {
extension.SetFilter(HystrixConsumer, newFilterConsumer)
extension.SetFilter(HystrixProvider, newFilterProvider)
extension.SetFilter(constant.HystrixConsumerFilterKey, newFilterConsumer)
extension.SetFilter(constant.HystrixProviderFilterKey, newFilterProvider)
}

// FilterError implements error interface
Expand Down
5 changes: 2 additions & 3 deletions filter/metrics/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@ import (
)

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/metrics"
"dubbo.apache.org/dubbo-go/v3/protocol"
)

const Metrics = "metrics"

// must initialized before using the filter and after loading configuration
var metricFilterInstance *Filter

func init() {
extension.SetFilter(Metrics, newFilter)
extension.SetFilter(constant.MetricsFilterKey, newFilter)
}

// Filter will calculate the invocation's duration and the report to the reporters
Expand Down
3 changes: 1 addition & 2 deletions filter/seata/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ import (

const (
SEATA_XID = constant.DubboCtxKey("SEATA_XID")
Seata = constant.DubboCtxKey("seata")
)

func init() {
extension.SetFilter(string(Seata), func() filter.Filter {
extension.SetFilter(constant.SeataFilterKey, func() filter.Filter {
return &Filter{}
})
}
Expand Down
Loading

0 comments on commit 9621ebf

Please sign in to comment.