diff --git a/common/constant/default.go b/common/constant/default.go index 6e0f848878..992fc32748 100644 --- a/common/constant/default.go +++ b/common/constant/default.go @@ -46,7 +46,7 @@ const ( const ( DEFAULT_KEY = "default" PREFIX_DEFAULT_KEY = "default." - DEFAULT_SERVICE_FILTERS = "echo,token,accesslog,tps,execute,pshutdown" + DEFAULT_SERVICE_FILTERS = "echo,token,accesslog,tps,generic_service,execute,pshutdown" DEFAULT_REFERENCE_FILTERS = "cshutdown" GENERIC_REFERENCE_FILTERS = "generic" GENERIC = "$invoke" diff --git a/common/extension/filter.go b/common/extension/filter.go index 93f7f8cf7c..0b5c4b40aa 100644 --- a/common/extension/filter.go +++ b/common/extension/filter.go @@ -19,12 +19,11 @@ package extension import ( "github.com/apache/dubbo-go/filter" - "github.com/apache/dubbo-go/filter/common" ) var ( filters = make(map[string]func() filter.Filter) - rejectedExecutionHandler = make(map[string]func() common.RejectedExecutionHandler) + rejectedExecutionHandler = make(map[string]func() filter.RejectedExecutionHandler) ) func SetFilter(name string, v func() filter.Filter) { @@ -38,11 +37,11 @@ func GetFilter(name string) filter.Filter { return filters[name]() } -func SetRejectedExecutionHandler(name string, creator func() common.RejectedExecutionHandler) { +func SetRejectedExecutionHandler(name string, creator func() filter.RejectedExecutionHandler) { rejectedExecutionHandler[name] = creator } -func GetRejectedExecutionHandler(name string) common.RejectedExecutionHandler { +func GetRejectedExecutionHandler(name string) filter.RejectedExecutionHandler { creator, ok := rejectedExecutionHandler[name] if !ok { panic("RejectedExecutionHandler for " + name + " is not existing, make sure you have import the package " + diff --git a/common/extension/tps_limit.go b/common/extension/tps_limit.go index 151c33ad5e..8c131fafa3 100644 --- a/common/extension/tps_limit.go +++ b/common/extension/tps_limit.go @@ -18,19 +18,19 @@ package extension import ( - "github.com/apache/dubbo-go/filter/impl/tps" + "github.com/apache/dubbo-go/filter" ) var ( - tpsLimitStrategy = make(map[string]tps.TpsLimitStrategyCreator) - tpsLimiter = make(map[string]func() tps.TpsLimiter) + tpsLimitStrategy = make(map[string]filter.TpsLimitStrategyCreator) + tpsLimiter = make(map[string]func() filter.TpsLimiter) ) -func SetTpsLimiter(name string, creator func() tps.TpsLimiter) { +func SetTpsLimiter(name string, creator func() filter.TpsLimiter) { tpsLimiter[name] = creator } -func GetTpsLimiter(name string) tps.TpsLimiter { +func GetTpsLimiter(name string) filter.TpsLimiter { creator, ok := tpsLimiter[name] if !ok { panic("TpsLimiter for " + name + " is not existing, make sure you have import the package " + @@ -39,11 +39,11 @@ func GetTpsLimiter(name string) tps.TpsLimiter { return creator() } -func SetTpsLimitStrategy(name string, creator tps.TpsLimitStrategyCreator) { +func SetTpsLimitStrategy(name string, creator filter.TpsLimitStrategyCreator) { tpsLimitStrategy[name] = creator } -func GetTpsLimitStrategyCreator(name string) tps.TpsLimitStrategyCreator { +func GetTpsLimitStrategyCreator(name string) filter.TpsLimitStrategyCreator { creator, ok := tpsLimitStrategy[name] if !ok { panic("TpsLimitStrategy for " + name + " is not existing, make sure you have import the package " + diff --git a/filter/common/impl/rejected_execution_handler_mock.go b/filter/common/rejected_execution_handler_mock.go similarity index 99% rename from filter/common/impl/rejected_execution_handler_mock.go rename to filter/common/rejected_execution_handler_mock.go index dace189466..a5631af9f7 100644 --- a/filter/common/impl/rejected_execution_handler_mock.go +++ b/filter/common/rejected_execution_handler_mock.go @@ -18,7 +18,7 @@ // Source: rejected_execution_handler.go // Package filter is a generated GoMock package. -package impl +package common import ( reflect "reflect" diff --git a/filter/common/impl/rejected_execution_handler_only_log.go b/filter/common/rejected_execution_handler_only_log.go similarity index 93% rename from filter/common/impl/rejected_execution_handler_only_log.go rename to filter/common/rejected_execution_handler_only_log.go index 8943433af1..65abe677f1 100644 --- a/filter/common/impl/rejected_execution_handler_only_log.go +++ b/filter/common/rejected_execution_handler_only_log.go @@ -15,9 +15,10 @@ * limitations under the License. */ -package impl +package common import ( + "github.com/apache/dubbo-go/filter" "sync" ) @@ -26,7 +27,6 @@ import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/common/logger" - filterCommon "github.com/apache/dubbo-go/filter/common" "github.com/apache/dubbo-go/protocol" ) @@ -61,7 +61,7 @@ func (handler *OnlyLogRejectedExecutionHandler) RejectedExecution(url common.URL return &protocol.RPCResult{} } -func GetOnlyLogRejectedExecutionHandler() filterCommon.RejectedExecutionHandler { +func GetOnlyLogRejectedExecutionHandler() filter.RejectedExecutionHandler { onlyLogHandlerOnce.Do(func() { onlyLogHandlerInstance = &OnlyLogRejectedExecutionHandler{} }) diff --git a/filter/common/impl/rejected_execution_handler_only_log_test.go b/filter/common/rejected_execution_handler_only_log_test.go similarity index 98% rename from filter/common/impl/rejected_execution_handler_only_log_test.go rename to filter/common/rejected_execution_handler_only_log_test.go index da54d8a106..0efc3d8137 100644 --- a/filter/common/impl/rejected_execution_handler_only_log_test.go +++ b/filter/common/rejected_execution_handler_only_log_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package common import ( "net/url" diff --git a/filter/impl/access_log_filter.go b/filter/filter/access_log_filter.go similarity index 99% rename from filter/impl/access_log_filter.go rename to filter/filter/access_log_filter.go index 89fa34952f..cce2c5050f 100644 --- a/filter/impl/access_log_filter.go +++ b/filter/filter/access_log_filter.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "os" diff --git a/filter/impl/access_log_filter_test.go b/filter/filter/access_log_filter_test.go similarity index 99% rename from filter/impl/access_log_filter_test.go rename to filter/filter/access_log_filter_test.go index 834d531f05..2c17021a9f 100644 --- a/filter/impl/access_log_filter_test.go +++ b/filter/filter/access_log_filter_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "context" diff --git a/filter/impl/active_filter.go b/filter/filter/active_filter.go similarity index 99% rename from filter/impl/active_filter.go rename to filter/filter/active_filter.go index 36a4e1a767..e0f73c2b2f 100644 --- a/filter/impl/active_filter.go +++ b/filter/filter/active_filter.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "github.com/apache/dubbo-go/common/extension" diff --git a/filter/impl/echo_filter.go b/filter/filter/echo_filter.go similarity index 99% rename from filter/impl/echo_filter.go rename to filter/filter/echo_filter.go index 18e42c8cb2..1515c0a99c 100644 --- a/filter/impl/echo_filter.go +++ b/filter/filter/echo_filter.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "github.com/apache/dubbo-go/common/constant" diff --git a/filter/impl/echo_filter_test.go b/filter/filter/echo_filter_test.go similarity index 98% rename from filter/impl/echo_filter_test.go rename to filter/filter/echo_filter_test.go index e2e5929747..d57d54329f 100644 --- a/filter/impl/echo_filter_test.go +++ b/filter/filter/echo_filter_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "testing" diff --git a/filter/impl/execute_limit_filter.go b/filter/filter/execute_limit_filter.go similarity index 98% rename from filter/impl/execute_limit_filter.go rename to filter/filter/execute_limit_filter.go index 156af1b140..4b5ea7491c 100644 --- a/filter/impl/execute_limit_filter.go +++ b/filter/filter/execute_limit_filter.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "strconv" @@ -32,7 +32,7 @@ import ( "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/common/logger" "github.com/apache/dubbo-go/filter" - _ "github.com/apache/dubbo-go/filter/common/impl" + _ "github.com/apache/dubbo-go/filter/common" "github.com/apache/dubbo-go/protocol" ) diff --git a/filter/impl/execute_limit_filter_test.go b/filter/filter/execute_limit_filter_test.go similarity index 99% rename from filter/impl/execute_limit_filter_test.go rename to filter/filter/execute_limit_filter_test.go index 5d729c0e6a..326b13677b 100644 --- a/filter/impl/execute_limit_filter_test.go +++ b/filter/filter/execute_limit_filter_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "net/url" diff --git a/filter/impl/generic_filter.go b/filter/filter/generic_filter.go similarity index 99% rename from filter/impl/generic_filter.go rename to filter/filter/generic_filter.go index 067939a34b..9fb26f15ae 100644 --- a/filter/impl/generic_filter.go +++ b/filter/filter/generic_filter.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "reflect" diff --git a/filter/impl/generic_filter_test.go b/filter/filter/generic_filter_test.go similarity index 99% rename from filter/impl/generic_filter_test.go rename to filter/filter/generic_filter_test.go index 9797c40df1..d5298adbd4 100644 --- a/filter/impl/generic_filter_test.go +++ b/filter/filter/generic_filter_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "reflect" diff --git a/filter/filter/generic_service_filter.go b/filter/filter/generic_service_filter.go new file mode 100644 index 0000000000..514a51f0b0 --- /dev/null +++ b/filter/filter/generic_service_filter.go @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package filter + +import ( + "reflect" + "strings" +) + +import ( + hessian "github.com/apache/dubbo-go-hessian2" + "github.com/mitchellh/mapstructure" + perrors "github.com/pkg/errors" +) + +import ( + "github.com/apache/dubbo-go/common" + "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/common/logger" + "github.com/apache/dubbo-go/filter" + "github.com/apache/dubbo-go/protocol" + invocation2 "github.com/apache/dubbo-go/protocol/invocation" +) + +const ( + GENERIC_SERVICE = "generic_service" + GENERIC_SERIALIZATION_DEFAULT = "true" +) + +func init() { + extension.SetFilter(GENERIC_SERVICE, GetGenericServiceFilter) +} + +type GenericServiceFilter struct{} + +func (ef *GenericServiceFilter) Invoke(invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { + logger.Infof("invoking generic service filter.") + logger.Debugf("generic service filter methodName:%v,args:%v", invocation.MethodName(), len(invocation.Arguments())) + + if invocation.MethodName() != constant.GENERIC || len(invocation.Arguments()) != 3 { + return invoker.Invoke(invocation) + } + + var ( + ok bool + err error + methodName string + newParams []interface{} + genericKey string + argsType []reflect.Type + oldParams []hessian.Object + ) + + url := invoker.GetUrl() + methodName = invocation.Arguments()[0].(string) + // get service + svc := common.ServiceMap.GetService(url.Protocol, strings.TrimPrefix(url.Path, "/")) + // get method + method := svc.Method()[methodName] + if method == nil { + logger.Errorf("[Generic Service Filter] Don't have this method: %s", methodName) + return &protocol.RPCResult{} + } + argsType = method.ArgsType() + genericKey = invocation.AttachmentsByKey(constant.GENERIC_KEY, GENERIC_SERIALIZATION_DEFAULT) + if genericKey == GENERIC_SERIALIZATION_DEFAULT { + oldParams, ok = invocation.Arguments()[2].([]hessian.Object) + } else { + logger.Errorf("[Generic Service Filter] Don't support this generic: %s", genericKey) + return &protocol.RPCResult{} + } + if !ok { + logger.Errorf("[Generic Service Filter] wrong serialization") + return &protocol.RPCResult{} + } + if len(oldParams) != len(argsType) { + logger.Errorf("[Generic Service Filter] method:%s invocation arguments number was wrong", methodName) + return &protocol.RPCResult{} + } + // oldParams convert to newParams + newParams = make([]interface{}, len(oldParams)) + for i := range argsType { + newParam := reflect.New(argsType[i]).Interface() + err = mapstructure.Decode(oldParams[i], newParam) + newParam = reflect.ValueOf(newParam).Elem().Interface() + if err != nil { + logger.Errorf("[Generic Service Filter] decode arguments map to struct wrong: error{%v}", perrors.WithStack(err)) + return &protocol.RPCResult{} + } + newParams[i] = newParam + } + newInvocation := invocation2.NewRPCInvocation(methodName, newParams, invocation.Attachments()) + newInvocation.SetReply(invocation.Reply()) + return invoker.Invoke(newInvocation) +} + +func (ef *GenericServiceFilter) OnResponse(result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { + if invocation.MethodName() == constant.GENERIC && len(invocation.Arguments()) == 3 && result.Result() != nil { + v := reflect.ValueOf(result.Result()) + if v.Kind() == reflect.Ptr { + v = v.Elem() + } + result.SetResult(struct2MapAll(v.Interface())) + } + return result +} + +func GetGenericServiceFilter() filter.Filter { + return &GenericServiceFilter{} +} diff --git a/filter/filter/generic_service_filter_test.go b/filter/filter/generic_service_filter_test.go new file mode 100644 index 0000000000..599a6a66d0 --- /dev/null +++ b/filter/filter/generic_service_filter_test.go @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package filter + +import ( + "context" + "errors" + "reflect" + "testing" +) + +import ( + hessian "github.com/apache/dubbo-go-hessian2" + "github.com/stretchr/testify/assert" +) + +import ( + "github.com/apache/dubbo-go/common" + "github.com/apache/dubbo-go/common/proxy/proxy_factory" + "github.com/apache/dubbo-go/protocol" + "github.com/apache/dubbo-go/protocol/invocation" +) + +type TestStruct struct { + AaAa string + BaBa string `m:"baBa"` + XxYy struct { + xxXx string `m:"xxXx"` + Xx string `m:"xx"` + } `m:"xxYy"` +} + +func (c *TestStruct) JavaClassName() string { + return "com.test.testStruct" +} + +type TestService struct { +} + +func (ts *TestService) MethodOne(ctx context.Context, test1 *TestStruct, test2 []TestStruct, + test3 interface{}, test4 []interface{}, test5 *string) (*TestStruct, error) { + if test1 == nil { + return nil, errors.New("param test1 is nil") + } + if test2 == nil { + return nil, errors.New("param test2 is nil") + } + if test3 == nil { + return nil, errors.New("param test3 is nil") + } + if test4 == nil { + return nil, errors.New("param test4 is nil") + } + if test5 == nil { + return nil, errors.New("param test5 is nil") + } + return &TestStruct{}, nil +} + +func (s *TestService) Reference() string { + return "com.test.Path" +} + +func TestGenericServiceFilter_Invoke(t *testing.T) { + hessian.RegisterPOJO(&TestStruct{}) + methodName := "$invoke" + m := make(map[string]interface{}) + m["AaAa"] = "nihao" + x := make(map[string]interface{}) + x["xxXX"] = "nihaoxxx" + m["XxYy"] = x + aurguments := []interface{}{ + "MethodOne", + nil, + []hessian.Object{ + hessian.Object(m), + hessian.Object(append(make([]map[string]interface{}, 1), m)), + hessian.Object("111"), + hessian.Object(append(make([]map[string]interface{}, 1), m)), + hessian.Object("222")}, + } + s := &TestService{} + _, _ = common.ServiceMap.Register("testprotocol", s) + rpcInvocation := invocation.NewRPCInvocation(methodName, aurguments, nil) + filter := GetGenericServiceFilter() + url, _ := common.NewURL(context.Background(), "testprotocol://127.0.0.1:20000/com.test.Path") + result := filter.Invoke(&proxy_factory.ProxyInvoker{BaseInvoker: *protocol.NewBaseInvoker(url)}, rpcInvocation) + assert.NotNil(t, result) + assert.Nil(t, result.Error()) +} + +func TestGenericServiceFilter_ResponseTestStruct(t *testing.T) { + ts := &TestStruct{ + AaAa: "aaa", + BaBa: "bbb", + XxYy: struct { + xxXx string `m:"xxXx"` + Xx string `m:"xx"` + }{}, + } + result := &protocol.RPCResult{ + Rest: ts, + } + aurguments := []interface{}{ + "MethodOne", + nil, + []hessian.Object{nil}, + } + filter := GetGenericServiceFilter() + methodName := "$invoke" + rpcInvocation := invocation.NewRPCInvocation(methodName, aurguments, nil) + r := filter.OnResponse(result, nil, rpcInvocation) + assert.NotNil(t, r.Result()) + assert.Equal(t, reflect.ValueOf(r.Result()).Kind(), reflect.Map) +} + +func TestGenericServiceFilter_ResponseString(t *testing.T) { + str := "111" + result := &protocol.RPCResult{ + Rest: str, + } + aurguments := []interface{}{ + "MethodOne", + nil, + []hessian.Object{nil}, + } + filter := GetGenericServiceFilter() + methodName := "$invoke" + rpcInvocation := invocation.NewRPCInvocation(methodName, aurguments, nil) + r := filter.OnResponse(result, nil, rpcInvocation) + assert.NotNil(t, r.Result()) + assert.Equal(t, reflect.ValueOf(r.Result()).Kind(), reflect.String) +} diff --git a/filter/impl/graceful_shutdown_filter.go b/filter/filter/graceful_shutdown_filter.go similarity index 95% rename from filter/impl/graceful_shutdown_filter.go rename to filter/filter/graceful_shutdown_filter.go index b912ea88e4..c682c7ef79 100644 --- a/filter/impl/graceful_shutdown_filter.go +++ b/filter/filter/graceful_shutdown_filter.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "sync/atomic" @@ -27,7 +27,6 @@ import ( "github.com/apache/dubbo-go/common/logger" "github.com/apache/dubbo-go/config" "github.com/apache/dubbo-go/filter" - "github.com/apache/dubbo-go/filter/common" "github.com/apache/dubbo-go/protocol" ) @@ -78,7 +77,7 @@ func (gf *gracefulShutdownFilter) rejectNewRequest() bool { return gf.shutdownConfig.RejectRequest } -func (gf *gracefulShutdownFilter) getRejectHandler() common.RejectedExecutionHandler { +func (gf *gracefulShutdownFilter) getRejectHandler() filter.RejectedExecutionHandler { handler := constant.DEFAULT_KEY if gf.shutdownConfig != nil && len(gf.shutdownConfig.RejectRequestHandler) > 0 { handler = gf.shutdownConfig.RejectRequestHandler diff --git a/filter/impl/graceful_shutdown_filter_test.go b/filter/filter/graceful_shutdown_filter_test.go similarity index 89% rename from filter/impl/graceful_shutdown_filter_test.go rename to filter/filter/graceful_shutdown_filter_test.go index 21da167ea0..af57cd4ec8 100644 --- a/filter/impl/graceful_shutdown_filter_test.go +++ b/filter/filter/graceful_shutdown_filter_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "net/url" @@ -31,8 +31,8 @@ import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/config" - filterCommon "github.com/apache/dubbo-go/filter/common" - "github.com/apache/dubbo-go/filter/common/impl" + "github.com/apache/dubbo-go/filter" + common2 "github.com/apache/dubbo-go/filter/common" "github.com/apache/dubbo-go/protocol" "github.com/apache/dubbo-go/protocol/invocation" ) @@ -66,8 +66,8 @@ func TestGenericFilter_Invoke(t *testing.T) { assert.True(t, shutdownFilter.rejectNewRequest()) result = shutdownFilter.OnResponse(nil, protocol.NewBaseInvoker(*invokeUrl), invoc) - rejectHandler := &impl.OnlyLogRejectedExecutionHandler{} - extension.SetRejectedExecutionHandler("mock", func() filterCommon.RejectedExecutionHandler { + rejectHandler := &common2.OnlyLogRejectedExecutionHandler{} + extension.SetRejectedExecutionHandler("mock", func() filter.RejectedExecutionHandler { return rejectHandler }) assert.True(t, providerConfig.ShutdownConfig.RequestsFinished) diff --git a/filter/impl/hystrix_filter.go b/filter/filter/hystrix_filter.go similarity index 99% rename from filter/impl/hystrix_filter.go rename to filter/filter/hystrix_filter.go index 3fd9f87168..a7c57b4dd6 100644 --- a/filter/impl/hystrix_filter.go +++ b/filter/filter/hystrix_filter.go @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package impl +package filter import ( "fmt" diff --git a/filter/impl/hystrix_filter_test.go b/filter/filter/hystrix_filter_test.go similarity index 99% rename from filter/impl/hystrix_filter_test.go rename to filter/filter/hystrix_filter_test.go index d3a5183ede..3743294030 100644 --- a/filter/impl/hystrix_filter_test.go +++ b/filter/filter/hystrix_filter_test.go @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package impl +package filter import ( "regexp" diff --git a/filter/impl/token_filter.go b/filter/filter/token_filter.go similarity index 99% rename from filter/impl/token_filter.go rename to filter/filter/token_filter.go index d10dff5b76..07b80f3402 100644 --- a/filter/impl/token_filter.go +++ b/filter/filter/token_filter.go @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package impl +package filter import ( "strings" diff --git a/filter/impl/token_filter_test.go b/filter/filter/token_filter_test.go similarity index 99% rename from filter/impl/token_filter_test.go rename to filter/filter/token_filter_test.go index 1473f27403..4434865de7 100644 --- a/filter/impl/token_filter_test.go +++ b/filter/filter/token_filter_test.go @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package impl +package filter import ( "net/url" diff --git a/filter/impl/tps_limit_filter.go b/filter/filter/tps_limit_filter.go similarity index 95% rename from filter/impl/tps_limit_filter.go rename to filter/filter/tps_limit_filter.go index 3cb7381c86..ccccec00d4 100644 --- a/filter/impl/tps_limit_filter.go +++ b/filter/filter/tps_limit_filter.go @@ -15,15 +15,15 @@ * limitations under the License. */ -package impl +package filter import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/common/logger" "github.com/apache/dubbo-go/filter" - _ "github.com/apache/dubbo-go/filter/common/impl" - _ "github.com/apache/dubbo-go/filter/impl/tps/impl" + _ "github.com/apache/dubbo-go/filter/common" + _ "github.com/apache/dubbo-go/filter/tps" "github.com/apache/dubbo-go/protocol" ) diff --git a/filter/impl/tps_limit_filter_test.go b/filter/filter/tps_limit_filter_test.go similarity index 84% rename from filter/impl/tps_limit_filter_test.go rename to filter/filter/tps_limit_filter_test.go index debdbd00de..6acaab7036 100644 --- a/filter/impl/tps_limit_filter_test.go +++ b/filter/filter/tps_limit_filter_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "net/url" @@ -23,6 +23,9 @@ import ( ) import ( + "github.com/apache/dubbo-go/filter" + common2 "github.com/apache/dubbo-go/filter/common" + "github.com/apache/dubbo-go/filter/tps" "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" ) @@ -31,10 +34,6 @@ import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" - filterCommon "github.com/apache/dubbo-go/filter/common" - filterCommonImpl "github.com/apache/dubbo-go/filter/common/impl" - "github.com/apache/dubbo-go/filter/impl/tps" - "github.com/apache/dubbo-go/filter/impl/tps/impl" "github.com/apache/dubbo-go/protocol" "github.com/apache/dubbo-go/protocol/invocation" ) @@ -56,9 +55,9 @@ func TestTpsLimitFilter_Invoke_With_No_TpsLimiter(t *testing.T) { func TestGenericFilter_Invoke_With_Default_TpsLimiter(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockLimiter := impl.NewMockTpsLimiter(ctrl) + mockLimiter := tps.NewMockTpsLimiter(ctrl) mockLimiter.EXPECT().IsAllowable(gomock.Any(), gomock.Any()).Return(true).Times(1) - extension.SetTpsLimiter(constant.DEFAULT_KEY, func() tps.TpsLimiter { + extension.SetTpsLimiter(constant.DEFAULT_KEY, func() filter.TpsLimiter { return mockLimiter }) @@ -77,17 +76,17 @@ func TestGenericFilter_Invoke_With_Default_TpsLimiter(t *testing.T) { func TestGenericFilter_Invoke_With_Default_TpsLimiter_Not_Allow(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockLimiter := impl.NewMockTpsLimiter(ctrl) + mockLimiter := tps.NewMockTpsLimiter(ctrl) mockLimiter.EXPECT().IsAllowable(gomock.Any(), gomock.Any()).Return(false).Times(1) - extension.SetTpsLimiter(constant.DEFAULT_KEY, func() tps.TpsLimiter { + extension.SetTpsLimiter(constant.DEFAULT_KEY, func() filter.TpsLimiter { return mockLimiter }) mockResult := &protocol.RPCResult{} - mockRejectedHandler := filterCommonImpl.NewMockRejectedExecutionHandler(ctrl) + mockRejectedHandler := common2.NewMockRejectedExecutionHandler(ctrl) mockRejectedHandler.EXPECT().RejectedExecution(gomock.Any(), gomock.Any()).Return(mockResult).Times(1) - extension.SetRejectedExecutionHandler(constant.DEFAULT_KEY, func() filterCommon.RejectedExecutionHandler { + extension.SetRejectedExecutionHandler(constant.DEFAULT_KEY, func() filter.RejectedExecutionHandler { return mockRejectedHandler }) diff --git a/filter/common/rejected_execution_handler.go b/filter/rejected_execution_handler.go similarity index 98% rename from filter/common/rejected_execution_handler.go rename to filter/rejected_execution_handler.go index b993b8444c..ce95b54b14 100644 --- a/filter/common/rejected_execution_handler.go +++ b/filter/rejected_execution_handler.go @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package common +package filter import ( "github.com/apache/dubbo-go/common" diff --git a/filter/impl/tps/impl/tps_limit_fix_window_strategy.go b/filter/tps/tps_limit_fix_window_strategy.go similarity index 96% rename from filter/impl/tps/impl/tps_limit_fix_window_strategy.go rename to filter/tps/tps_limit_fix_window_strategy.go index 285ecfa658..6ea5dc1033 100644 --- a/filter/impl/tps/impl/tps_limit_fix_window_strategy.go +++ b/filter/tps/tps_limit_fix_window_strategy.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package tps import ( "sync/atomic" @@ -25,7 +25,7 @@ import ( import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/filter/impl/tps" + "github.com/apache/dubbo-go/filter" ) const ( @@ -79,7 +79,7 @@ func (impl *FixedWindowTpsLimitStrategyImpl) IsAllowable() bool { type fixedWindowStrategyCreator struct{} -func (creator *fixedWindowStrategyCreator) Create(rate int, interval int) tps.TpsLimitStrategy { +func (creator *fixedWindowStrategyCreator) Create(rate int, interval int) filter.TpsLimitStrategy { return &FixedWindowTpsLimitStrategyImpl{ rate: int32(rate), interval: int64(interval) * int64(time.Millisecond), // convert to ns diff --git a/filter/impl/tps/impl/tps_limit_fix_window_strategy_test.go b/filter/tps/tps_limit_fix_window_strategy_test.go similarity index 99% rename from filter/impl/tps/impl/tps_limit_fix_window_strategy_test.go rename to filter/tps/tps_limit_fix_window_strategy_test.go index 7ef539ed3b..5eaf2f707d 100644 --- a/filter/impl/tps/impl/tps_limit_fix_window_strategy_test.go +++ b/filter/tps/tps_limit_fix_window_strategy_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package tps import ( "testing" diff --git a/filter/impl/tps/impl/tps_limit_sliding_window_strategy.go b/filter/tps/tps_limit_sliding_window_strategy.go similarity index 96% rename from filter/impl/tps/impl/tps_limit_sliding_window_strategy.go rename to filter/tps/tps_limit_sliding_window_strategy.go index d1a5db6e25..40ea2d14be 100644 --- a/filter/impl/tps/impl/tps_limit_sliding_window_strategy.go +++ b/filter/tps/tps_limit_sliding_window_strategy.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package tps import ( "container/list" @@ -25,7 +25,7 @@ import ( import ( "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/filter/impl/tps" + "github.com/apache/dubbo-go/filter" ) func init() { @@ -82,7 +82,7 @@ func (impl *SlidingWindowTpsLimitStrategyImpl) IsAllowable() bool { type slidingWindowStrategyCreator struct{} -func (creator *slidingWindowStrategyCreator) Create(rate int, interval int) tps.TpsLimitStrategy { +func (creator *slidingWindowStrategyCreator) Create(rate int, interval int) filter.TpsLimitStrategy { return &SlidingWindowTpsLimitStrategyImpl{ rate: rate, interval: int64(interval) * int64(time.Millisecond), diff --git a/filter/impl/tps/impl/tps_limit_sliding_window_strategy_test.go b/filter/tps/tps_limit_sliding_window_strategy_test.go similarity index 99% rename from filter/impl/tps/impl/tps_limit_sliding_window_strategy_test.go rename to filter/tps/tps_limit_sliding_window_strategy_test.go index 075f1d9d2b..57342d1c44 100644 --- a/filter/impl/tps/impl/tps_limit_sliding_window_strategy_test.go +++ b/filter/tps/tps_limit_sliding_window_strategy_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package tps import ( "testing" diff --git a/filter/impl/tps/impl/tps_limit_strategy_mock.go b/filter/tps/tps_limit_strategy_mock.go similarity index 99% rename from filter/impl/tps/impl/tps_limit_strategy_mock.go rename to filter/tps/tps_limit_strategy_mock.go index a653fb287a..72c658fb9a 100644 --- a/filter/impl/tps/impl/tps_limit_strategy_mock.go +++ b/filter/tps/tps_limit_strategy_mock.go @@ -18,7 +18,7 @@ // Source: tps_limit_strategy.go // Package filter is a generated GoMock package. -package impl +package tps import ( gomock "github.com/golang/mock/gomock" diff --git a/filter/impl/tps/impl/tps_limit_thread_safe_fix_window_strategy.go b/filter/tps/tps_limit_thread_safe_fix_window_strategy.go similarity index 95% rename from filter/impl/tps/impl/tps_limit_thread_safe_fix_window_strategy.go rename to filter/tps/tps_limit_thread_safe_fix_window_strategy.go index 9a1b21a334..faec9b6ec1 100644 --- a/filter/impl/tps/impl/tps_limit_thread_safe_fix_window_strategy.go +++ b/filter/tps/tps_limit_thread_safe_fix_window_strategy.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package tps import ( "sync" @@ -23,7 +23,7 @@ import ( import ( "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/filter/impl/tps" + "github.com/apache/dubbo-go/filter" ) func init() { @@ -62,7 +62,7 @@ type threadSafeFixedWindowStrategyCreator struct { fixedWindowStrategyCreator *fixedWindowStrategyCreator } -func (creator *threadSafeFixedWindowStrategyCreator) Create(rate int, interval int) tps.TpsLimitStrategy { +func (creator *threadSafeFixedWindowStrategyCreator) Create(rate int, interval int) filter.TpsLimitStrategy { fixedWindowStrategy := creator.fixedWindowStrategyCreator.Create(rate, interval).(*FixedWindowTpsLimitStrategyImpl) return &ThreadSafeFixedWindowTpsLimitStrategyImpl{ fixedWindow: fixedWindowStrategy, diff --git a/filter/impl/tps/impl/tps_limit_thread_safe_fix_window_strategy_test.go b/filter/tps/tps_limit_thread_safe_fix_window_strategy_test.go similarity index 99% rename from filter/impl/tps/impl/tps_limit_thread_safe_fix_window_strategy_test.go rename to filter/tps/tps_limit_thread_safe_fix_window_strategy_test.go index 1294939624..90cd15201c 100644 --- a/filter/impl/tps/impl/tps_limit_thread_safe_fix_window_strategy_test.go +++ b/filter/tps/tps_limit_thread_safe_fix_window_strategy_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package tps import ( "testing" diff --git a/filter/impl/tps/impl/tps_limiter_method_service.go b/filter/tps/tps_limiter_method_service.go similarity index 96% rename from filter/impl/tps/impl/tps_limiter_method_service.go rename to filter/tps/tps_limiter_method_service.go index 426ae59948..ac4498a33d 100644 --- a/filter/impl/tps/impl/tps_limiter_method_service.go +++ b/filter/tps/tps_limiter_method_service.go @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package impl +package tps import ( "fmt" @@ -30,7 +30,7 @@ import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/filter/impl/tps" + "github.com/apache/dubbo-go/filter" "github.com/apache/dubbo-go/protocol" ) @@ -127,7 +127,7 @@ func (limiter MethodServiceTpsLimiterImpl) IsAllowable(url common.URL, invocatio limitState, found := limiter.tpsState.Load(limitTarget) if found { - return limitState.(tps.TpsLimitStrategy).IsAllowable() + return limitState.(filter.TpsLimitStrategy).IsAllowable() } limitRate := getLimitConfig(methodLimitRateConfig, url, invocation, @@ -149,7 +149,7 @@ func (limiter MethodServiceTpsLimiterImpl) IsAllowable(url common.URL, invocatio url.GetParam(constant.TPS_LIMIT_STRATEGY_KEY, constant.DEFAULT_KEY)) limitStateCreator := extension.GetTpsLimitStrategyCreator(limitStrategyConfig) limitState, _ = limiter.tpsState.LoadOrStore(limitTarget, limitStateCreator.Create(int(limitRate), int(limitInterval))) - return limitState.(tps.TpsLimitStrategy).IsAllowable() + return limitState.(filter.TpsLimitStrategy).IsAllowable() } func getLimitConfig(methodLevelConfig string, @@ -178,7 +178,7 @@ func getLimitConfig(methodLevelConfig string, var methodServiceTpsLimiterInstance *MethodServiceTpsLimiterImpl var methodServiceTpsLimiterOnce sync.Once -func GetMethodServiceTpsLimiter() tps.TpsLimiter { +func GetMethodServiceTpsLimiter() filter.TpsLimiter { methodServiceTpsLimiterOnce.Do(func() { methodServiceTpsLimiterInstance = &MethodServiceTpsLimiterImpl{ tpsState: concurrent.NewMap(), diff --git a/filter/impl/tps/impl/tps_limiter_method_service_test.go b/filter/tps/tps_limiter_method_service_test.go similarity index 97% rename from filter/impl/tps/impl/tps_limiter_method_service_test.go rename to filter/tps/tps_limiter_method_service_test.go index e747d4682d..441224a3e3 100644 --- a/filter/impl/tps/impl/tps_limiter_method_service_test.go +++ b/filter/tps/tps_limiter_method_service_test.go @@ -15,13 +15,14 @@ * limitations under the License. */ -package impl +package tps import ( "net/url" "testing" ) import ( + "github.com/apache/dubbo-go/filter" "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" ) @@ -30,7 +31,6 @@ import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/filter/impl/tps" "github.com/apache/dubbo-go/protocol/invocation" ) @@ -144,10 +144,10 @@ type mockStrategyCreator struct { rate int interval int t *testing.T - strategy tps.TpsLimitStrategy + strategy filter.TpsLimitStrategy } -func (creator *mockStrategyCreator) Create(rate int, interval int) tps.TpsLimitStrategy { +func (creator *mockStrategyCreator) Create(rate int, interval int) filter.TpsLimitStrategy { assert.Equal(creator.t, creator.rate, rate) assert.Equal(creator.t, creator.interval, interval) return creator.strategy diff --git a/filter/impl/tps/impl/tps_limiter_mock.go b/filter/tps/tps_limiter_mock.go similarity index 99% rename from filter/impl/tps/impl/tps_limiter_mock.go rename to filter/tps/tps_limiter_mock.go index acd3a15d18..463b0988ac 100644 --- a/filter/impl/tps/impl/tps_limiter_mock.go +++ b/filter/tps/tps_limiter_mock.go @@ -18,7 +18,7 @@ // Source: tps_limiter.go // Package filter is a generated GoMock package. -package impl +package tps import ( reflect "reflect" diff --git a/filter/impl/tps/tps_limit_strategy.go b/filter/tps_limit_strategy.go similarity index 98% rename from filter/impl/tps/tps_limit_strategy.go rename to filter/tps_limit_strategy.go index c55f008a09..1051c3d96d 100644 --- a/filter/impl/tps/tps_limit_strategy.go +++ b/filter/tps_limit_strategy.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package tps +package filter /* * please register your implementation by invoking SetTpsLimitStrategy diff --git a/filter/impl/tps/tps_limiter.go b/filter/tps_limiter.go similarity index 98% rename from filter/impl/tps/tps_limiter.go rename to filter/tps_limiter.go index 0622a957a8..1d2b2341ac 100644 --- a/filter/impl/tps/tps_limiter.go +++ b/filter/tps_limiter.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package tps +package filter import ( "github.com/apache/dubbo-go/common" diff --git a/go.mod b/go.mod index c2a61f2db1..17ac2bc067 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f // indirect github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 // indirect github.com/magiconair/properties v1.8.1 + github.com/mitchellh/mapstructure v1.1.2 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd github.com/nacos-group/nacos-sdk-go v0.0.0-20190723125407-0242d42e3dbb github.com/pkg/errors v0.8.1