From b4d116a8bf00d5b1f24cfade48923f6160c534bb Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 29 Dec 2019 21:06:11 +0800 Subject: [PATCH 01/14] generic service filter --- common/constant/default.go | 2 +- filter/impl/generic_service_filter.go | 98 +++++++++++++++++++ filter/impl/generic_service_filter_test.go | 107 +++++++++++++++++++++ 3 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 filter/impl/generic_service_filter.go create mode 100644 filter/impl/generic_service_filter_test.go diff --git a/common/constant/default.go b/common/constant/default.go index 6e0f848878..3d9177b21d 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/filter/impl/generic_service_filter.go b/filter/impl/generic_service_filter.go new file mode 100644 index 0000000000..9848fd8e58 --- /dev/null +++ b/filter/impl/generic_service_filter.go @@ -0,0 +1,98 @@ +package impl + +import ( + hessian "github.com/apache/dubbo-go-hessian2" + "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" + "github.com/mitchellh/mapstructure" + "reflect" + "strings" +) + +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 ( + 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: %v", method) + return &protocol.RPCResult{} + } + argsType = method.ArgsType() + genericKey = invocation.AttachmentsByKey(constant.GENERIC_KEY, GENERIC_SERIALIZATION_DEFAULT) + if genericKey == GENERIC_SERIALIZATION_DEFAULT { + oldParams = invocation.Arguments()[2].([]hessian.Object) + } else { + logger.Errorf("[Generic Service Filter] Don't support this generic: %v", genericKey) + 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 + for i := range argsType { + var newParam interface{} + if argsType[i].Kind() == reflect.Ptr { + newParam = reflect.New(argsType[i].Elem()).Interface() + err = mapstructure.Decode(oldParams[i], newParam) + } else if argsType[i].Kind() == reflect.Struct || argsType[i].Kind() == reflect.Slice { + newParam = reflect.New(argsType[i]).Interface() + err = mapstructure.Decode(oldParams[i], newParam) + newParam = reflect.ValueOf(newParam).Elem().Interface() + } else { + newParam = oldParams[i] + } + if err != nil { + logger.Errorf("[Generic Service Filter] decode arguments map to struct wrong") + } + newParams = append(newParams, 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 { + s := reflect.ValueOf(result.Result()).Elem().Interface() + r := struct2MapAll(s) + result.SetResult(r) + } + return result +} + +func GetGenericServiceFilter() filter.Filter { + return &GenericServiceFilter{} +} diff --git a/filter/impl/generic_service_filter_test.go b/filter/impl/generic_service_filter_test.go new file mode 100644 index 0000000000..e8b625bf0a --- /dev/null +++ b/filter/impl/generic_service_filter_test.go @@ -0,0 +1,107 @@ +package impl + +import ( + "context" + "errors" + hessian "github.com/apache/dubbo-go-hessian2" + "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" + "github.com/stretchr/testify/assert" + "reflect" + "testing" +) + +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_Response(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) +} From 131ad2de04505f1f9d42f583464833fe1c1f0849 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 29 Dec 2019 21:12:53 +0800 Subject: [PATCH 02/14] go.mod modify --- go.mod | 1 + 1 file changed, 1 insertion(+) 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 From 1ac170aba2fdf32553a7b84255246ab9e2056243 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 30 Dec 2019 00:00:34 +0800 Subject: [PATCH 03/14] reponse fix --- filter/impl/generic_service_filter.go | 8 +++++--- filter/impl/generic_service_filter_test.go | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/filter/impl/generic_service_filter.go b/filter/impl/generic_service_filter.go index 9848fd8e58..e180857ca5 100644 --- a/filter/impl/generic_service_filter.go +++ b/filter/impl/generic_service_filter.go @@ -86,9 +86,11 @@ func (ef *GenericServiceFilter) Invoke(invoker protocol.Invoker, invocation prot 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 { - s := reflect.ValueOf(result.Result()).Elem().Interface() - r := struct2MapAll(s) - result.SetResult(r) + v := reflect.ValueOf(result.Result()) + if v.Kind() == reflect.Ptr { + v = v.Elem() + } + result.SetResult(struct2MapAll(v.Interface())) } return result } diff --git a/filter/impl/generic_service_filter_test.go b/filter/impl/generic_service_filter_test.go index e8b625bf0a..ab319f1aa5 100644 --- a/filter/impl/generic_service_filter_test.go +++ b/filter/impl/generic_service_filter_test.go @@ -81,7 +81,7 @@ func TestGenericServiceFilter_Invoke(t *testing.T) { assert.Nil(t, result.Error()) } -func TestGenericServiceFilter_Response(t *testing.T) { +func TestGenericServiceFilter_ResponseTestStruct(t *testing.T) { ts := &TestStruct{ AaAa: "aaa", BaBa: "bbb", @@ -105,3 +105,21 @@ func TestGenericServiceFilter_Response(t *testing.T) { 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.Map) +} From 5ef93c3d8566aa9a2e9127d676b5a619faae84e0 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 30 Dec 2019 01:30:28 +0800 Subject: [PATCH 04/14] optimize code --- filter/impl/generic_service_filter.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/filter/impl/generic_service_filter.go b/filter/impl/generic_service_filter.go index e180857ca5..71d9d02fd7 100644 --- a/filter/impl/generic_service_filter.go +++ b/filter/impl/generic_service_filter.go @@ -64,16 +64,9 @@ func (ef *GenericServiceFilter) Invoke(invoker protocol.Invoker, invocation prot // oldParams convert to newParams for i := range argsType { var newParam interface{} - if argsType[i].Kind() == reflect.Ptr { - newParam = reflect.New(argsType[i].Elem()).Interface() - err = mapstructure.Decode(oldParams[i], newParam) - } else if argsType[i].Kind() == reflect.Struct || argsType[i].Kind() == reflect.Slice { - newParam = reflect.New(argsType[i]).Interface() - err = mapstructure.Decode(oldParams[i], newParam) - newParam = reflect.ValueOf(newParam).Elem().Interface() - } else { - newParam = oldParams[i] - } + 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") } From fe314db2f7f17e8d018cc231b96f22b42bed4b6a Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 30 Dec 2019 01:37:12 +0800 Subject: [PATCH 05/14] fix --- filter/impl/generic_service_filter.go | 1 + 1 file changed, 1 insertion(+) diff --git a/filter/impl/generic_service_filter.go b/filter/impl/generic_service_filter.go index 71d9d02fd7..c51b9efcaf 100644 --- a/filter/impl/generic_service_filter.go +++ b/filter/impl/generic_service_filter.go @@ -69,6 +69,7 @@ func (ef *GenericServiceFilter) Invoke(invoker protocol.Invoker, invocation prot newParam = reflect.ValueOf(newParam).Elem().Interface() if err != nil { logger.Errorf("[Generic Service Filter] decode arguments map to struct wrong") + return &protocol.RPCResult{} } newParams = append(newParams, newParam) } From 6aca4f56d98ed0e7da812e140b394d56bf9ef58b Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 30 Dec 2019 01:40:02 +0800 Subject: [PATCH 06/14] modify error log info --- filter/impl/generic_service_filter.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/filter/impl/generic_service_filter.go b/filter/impl/generic_service_filter.go index c51b9efcaf..b55b248e51 100644 --- a/filter/impl/generic_service_filter.go +++ b/filter/impl/generic_service_filter.go @@ -10,6 +10,7 @@ import ( "github.com/apache/dubbo-go/protocol" invocation2 "github.com/apache/dubbo-go/protocol/invocation" "github.com/mitchellh/mapstructure" + perrors "github.com/pkg/errors" "reflect" "strings" ) @@ -46,7 +47,7 @@ func (ef *GenericServiceFilter) Invoke(invoker protocol.Invoker, invocation prot // get method method := svc.Method()[methodName] if method == nil { - logger.Errorf("[Generic Service Filter] Don't have this method: %v", method) + logger.Errorf("[Generic Service Filter] Don't have this method: %s", methodName) return &protocol.RPCResult{} } argsType = method.ArgsType() @@ -54,7 +55,7 @@ func (ef *GenericServiceFilter) Invoke(invoker protocol.Invoker, invocation prot if genericKey == GENERIC_SERIALIZATION_DEFAULT { oldParams = invocation.Arguments()[2].([]hessian.Object) } else { - logger.Errorf("[Generic Service Filter] Don't support this generic: %v", genericKey) + logger.Errorf("[Generic Service Filter] Don't support this generic: %s", genericKey) return &protocol.RPCResult{} } if len(oldParams) != len(argsType) { @@ -68,7 +69,7 @@ func (ef *GenericServiceFilter) Invoke(invoker protocol.Invoker, invocation prot 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") + logger.Errorf("[Generic Service Filter] decode arguments map to struct wrong: error{%v}", perrors.WithStack(err)) return &protocol.RPCResult{} } newParams = append(newParams, newParam) From f96a30b0e70c0037e575c8f5819210d83d831a3f Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 30 Dec 2019 01:51:09 +0800 Subject: [PATCH 07/14] modify test --- filter/impl/generic_service_filter_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filter/impl/generic_service_filter_test.go b/filter/impl/generic_service_filter_test.go index ab319f1aa5..e326b898ed 100644 --- a/filter/impl/generic_service_filter_test.go +++ b/filter/impl/generic_service_filter_test.go @@ -121,5 +121,5 @@ func TestGenericServiceFilter_ResponseString(t *testing.T) { 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) + assert.Equal(t, reflect.ValueOf(r.Result()).Kind(), reflect.String) } From 8064687c4076c84d7f8a34fc3957b1e31ccbad9f Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 30 Dec 2019 10:04:11 +0800 Subject: [PATCH 08/14] split import block and add some blank lines --- filter/impl/generic_service_filter.go | 14 ++++++++++---- filter/impl/generic_service_filter_test.go | 9 ++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/filter/impl/generic_service_filter.go b/filter/impl/generic_service_filter.go index b55b248e51..28b1128b58 100644 --- a/filter/impl/generic_service_filter.go +++ b/filter/impl/generic_service_filter.go @@ -2,6 +2,13 @@ package impl import ( hessian "github.com/apache/dubbo-go-hessian2" + "github.com/mitchellh/mapstructure" + perrors "github.com/pkg/errors" + "reflect" + "strings" +) + +import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" @@ -9,10 +16,6 @@ import ( "github.com/apache/dubbo-go/filter" "github.com/apache/dubbo-go/protocol" invocation2 "github.com/apache/dubbo-go/protocol/invocation" - "github.com/mitchellh/mapstructure" - perrors "github.com/pkg/errors" - "reflect" - "strings" ) const ( @@ -29,9 +32,11 @@ 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 ( err error methodName string @@ -40,6 +45,7 @@ func (ef *GenericServiceFilter) Invoke(invoker protocol.Invoker, invocation prot argsType []reflect.Type oldParams []hessian.Object ) + url := invoker.GetUrl() methodName = invocation.Arguments()[0].(string) // get service diff --git a/filter/impl/generic_service_filter_test.go b/filter/impl/generic_service_filter_test.go index e326b898ed..1b3142cfce 100644 --- a/filter/impl/generic_service_filter_test.go +++ b/filter/impl/generic_service_filter_test.go @@ -4,13 +4,16 @@ import ( "context" "errors" hessian "github.com/apache/dubbo-go-hessian2" + "github.com/stretchr/testify/assert" + "reflect" + "testing" +) + +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" - "github.com/stretchr/testify/assert" - "reflect" - "testing" ) type TestStruct struct { From af31400f8dad76f65c14d9a74efddcc1c8366d2c Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 30 Dec 2019 10:07:59 +0800 Subject: [PATCH 09/14] split import packages --- filter/impl/generic_service_filter.go | 7 +++++-- filter/impl/generic_service_filter_test.go | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/filter/impl/generic_service_filter.go b/filter/impl/generic_service_filter.go index 28b1128b58..fb2b901389 100644 --- a/filter/impl/generic_service_filter.go +++ b/filter/impl/generic_service_filter.go @@ -1,11 +1,14 @@ package impl +import ( + "reflect" + "strings" +) + import ( hessian "github.com/apache/dubbo-go-hessian2" "github.com/mitchellh/mapstructure" perrors "github.com/pkg/errors" - "reflect" - "strings" ) import ( diff --git a/filter/impl/generic_service_filter_test.go b/filter/impl/generic_service_filter_test.go index 1b3142cfce..6a73305f8b 100644 --- a/filter/impl/generic_service_filter_test.go +++ b/filter/impl/generic_service_filter_test.go @@ -3,12 +3,15 @@ package impl import ( "context" "errors" - hessian "github.com/apache/dubbo-go-hessian2" - "github.com/stretchr/testify/assert" "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" From a7a7926bea4b767fb2ac546f217aa2cd0ed1e655 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 1 Jan 2020 13:40:50 +0800 Subject: [PATCH 10/14] add license --- filter/impl/generic_service_filter.go | 17 +++++++++++++++++ filter/impl/generic_service_filter_test.go | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/filter/impl/generic_service_filter.go b/filter/impl/generic_service_filter.go index fb2b901389..9edbf284c9 100644 --- a/filter/impl/generic_service_filter.go +++ b/filter/impl/generic_service_filter.go @@ -1,3 +1,20 @@ +/* + * 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 impl import ( diff --git a/filter/impl/generic_service_filter_test.go b/filter/impl/generic_service_filter_test.go index 6a73305f8b..89898694c2 100644 --- a/filter/impl/generic_service_filter_test.go +++ b/filter/impl/generic_service_filter_test.go @@ -1,3 +1,20 @@ +/* + * 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 impl import ( From b786517991e2a120a81b0bfeb6658c2972dbb99d Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 2 Jan 2020 20:13:11 +0800 Subject: [PATCH 11/14] modify generic_service filter name --- common/constant/default.go | 2 +- filter/impl/generic_service_filter.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/common/constant/default.go b/common/constant/default.go index 3d9177b21d..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,generic-service,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/filter/impl/generic_service_filter.go b/filter/impl/generic_service_filter.go index 9edbf284c9..61f4fbef23 100644 --- a/filter/impl/generic_service_filter.go +++ b/filter/impl/generic_service_filter.go @@ -39,7 +39,7 @@ import ( ) const ( - GENERIC_SERVICE = "generic-service" + GENERIC_SERVICE = "generic_service" GENERIC_SERIALIZATION_DEFAULT = "true" ) From 527f21e45eb6b5f068202654e4bd724ef843e01b Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 2 Jan 2020 20:48:35 +0800 Subject: [PATCH 12/14] fix --- filter/impl/generic_service_filter.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/filter/impl/generic_service_filter.go b/filter/impl/generic_service_filter.go index 61f4fbef23..2fde925d66 100644 --- a/filter/impl/generic_service_filter.go +++ b/filter/impl/generic_service_filter.go @@ -58,6 +58,7 @@ func (ef *GenericServiceFilter) Invoke(invoker protocol.Invoker, invocation prot } var ( + ok bool err error methodName string newParams []interface{} @@ -79,26 +80,30 @@ func (ef *GenericServiceFilter) Invoke(invoker protocol.Invoker, invocation prot argsType = method.ArgsType() genericKey = invocation.AttachmentsByKey(constant.GENERIC_KEY, GENERIC_SERIALIZATION_DEFAULT) if genericKey == GENERIC_SERIALIZATION_DEFAULT { - oldParams = invocation.Arguments()[2].([]hessian.Object) + 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 { - var newParam interface{} - newParam = reflect.New(argsType[i]).Interface() + 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 = append(newParams, newParam) + newParams[i] = newParam } newInvocation := invocation2.NewRPCInvocation(methodName, newParams, invocation.Attachments()) newInvocation.SetReply(invocation.Reply()) From 1c5cca4474ce75419c4c93db5ffa9a1bb1fb8377 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 5 Jan 2020 00:31:20 +0800 Subject: [PATCH 13/14] modify filter package --- common/extension/filter.go | 7 +++---- common/extension/tps_limit.go | 14 ++++++------- .../rejected_execution_handler_mock.go | 2 +- .../rejected_execution_handler_only_log.go | 6 +++--- ...ejected_execution_handler_only_log_test.go | 2 +- filter/{impl => filter}/access_log_filter.go | 2 +- .../access_log_filter_test.go | 2 +- filter/{impl => filter}/active_filter.go | 2 +- filter/{impl => filter}/echo_filter.go | 2 +- filter/{impl => filter}/echo_filter_test.go | 2 +- .../{impl => filter}/execute_limit_filter.go | 4 ++-- .../execute_limit_filter_test.go | 2 +- filter/{impl => filter}/generic_filter.go | 2 +- .../{impl => filter}/generic_filter_test.go | 2 +- .../generic_service_filter.go | 2 +- .../generic_service_filter_test.go | 2 +- .../graceful_shutdown_filter.go | 5 ++--- .../graceful_shutdown_filter_test.go | 10 ++++----- filter/{impl => filter}/hystrix_filter.go | 2 +- .../{impl => filter}/hystrix_filter_test.go | 2 +- filter/{impl => filter}/token_filter.go | 2 +- filter/{impl => filter}/token_filter_test.go | 2 +- filter/{impl => filter}/tps_limit_filter.go | 6 +++--- .../{impl => filter}/tps_limit_filter_test.go | 21 +++++++++---------- .../rejected_execution_handler.go | 2 +- .../tps_limit_fix_window_strategy.go | 6 +++--- .../tps_limit_fix_window_strategy_test.go | 2 +- .../tps_limit_sliding_window_strategy.go | 6 +++--- .../tps_limit_sliding_window_strategy_test.go | 2 +- .../impl => tps}/tps_limit_strategy_mock.go | 2 +- ...s_limit_thread_safe_fix_window_strategy.go | 6 +++--- ...it_thread_safe_fix_window_strategy_test.go | 2 +- .../tps_limiter_method_service.go | 10 ++++----- .../tps_limiter_method_service_test.go | 8 +++---- .../tps/impl => tps}/tps_limiter_mock.go | 2 +- filter/{impl/tps => }/tps_limit_strategy.go | 2 +- filter/{impl/tps => }/tps_limiter.go | 2 +- 37 files changed, 77 insertions(+), 80 deletions(-) rename filter/common/{impl => }/rejected_execution_handler_mock.go (99%) rename filter/common/{impl => }/rejected_execution_handler_only_log.go (93%) rename filter/common/{impl => }/rejected_execution_handler_only_log_test.go (98%) rename filter/{impl => filter}/access_log_filter.go (99%) rename filter/{impl => filter}/access_log_filter_test.go (99%) rename filter/{impl => filter}/active_filter.go (99%) rename filter/{impl => filter}/echo_filter.go (99%) rename filter/{impl => filter}/echo_filter_test.go (98%) rename filter/{impl => filter}/execute_limit_filter.go (98%) rename filter/{impl => filter}/execute_limit_filter_test.go (99%) rename filter/{impl => filter}/generic_filter.go (99%) rename filter/{impl => filter}/generic_filter_test.go (99%) rename filter/{impl => filter}/generic_service_filter.go (99%) rename filter/{impl => filter}/generic_service_filter_test.go (99%) rename filter/{impl => filter}/graceful_shutdown_filter.go (95%) rename filter/{impl => filter}/graceful_shutdown_filter_test.go (89%) rename filter/{impl => filter}/hystrix_filter.go (99%) rename filter/{impl => filter}/hystrix_filter_test.go (99%) rename filter/{impl => filter}/token_filter.go (99%) rename filter/{impl => filter}/token_filter_test.go (99%) rename filter/{impl => filter}/tps_limit_filter.go (95%) rename filter/{impl => filter}/tps_limit_filter_test.go (84%) rename filter/{common => }/rejected_execution_handler.go (98%) rename filter/{impl/tps/impl => tps}/tps_limit_fix_window_strategy.go (96%) rename filter/{impl/tps/impl => tps}/tps_limit_fix_window_strategy_test.go (99%) rename filter/{impl/tps/impl => tps}/tps_limit_sliding_window_strategy.go (96%) rename filter/{impl/tps/impl => tps}/tps_limit_sliding_window_strategy_test.go (99%) rename filter/{impl/tps/impl => tps}/tps_limit_strategy_mock.go (99%) rename filter/{impl/tps/impl => tps}/tps_limit_thread_safe_fix_window_strategy.go (95%) rename filter/{impl/tps/impl => tps}/tps_limit_thread_safe_fix_window_strategy_test.go (99%) rename filter/{impl/tps/impl => tps}/tps_limiter_method_service.go (96%) rename filter/{impl/tps/impl => tps}/tps_limiter_method_service_test.go (97%) rename filter/{impl/tps/impl => tps}/tps_limiter_mock.go (99%) rename filter/{impl/tps => }/tps_limit_strategy.go (98%) rename filter/{impl/tps => }/tps_limiter.go (98%) 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 35aadb11a4..f15449520d 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/impl/generic_service_filter.go b/filter/filter/generic_service_filter.go similarity index 99% rename from filter/impl/generic_service_filter.go rename to filter/filter/generic_service_filter.go index 2fde925d66..514a51f0b0 100644 --- a/filter/impl/generic_service_filter.go +++ b/filter/filter/generic_service_filter.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "reflect" diff --git a/filter/impl/generic_service_filter_test.go b/filter/filter/generic_service_filter_test.go similarity index 99% rename from filter/impl/generic_service_filter_test.go rename to filter/filter/generic_service_filter_test.go index 89898694c2..599a6a66d0 100644 --- a/filter/impl/generic_service_filter_test.go +++ b/filter/filter/generic_service_filter_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package filter import ( "context" 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..38430a5d1e 100644 --- a/filter/impl/graceful_shutdown_filter_test.go +++ b/filter/filter/graceful_shutdown_filter_test.go @@ -15,9 +15,11 @@ * limitations under the License. */ -package impl +package filter import ( + "github.com/apache/dubbo-go/filter" + common2 "github.com/apache/dubbo-go/filter/common" "net/url" "testing" ) @@ -31,8 +33,6 @@ 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/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..d4353bb7d1 100644 --- a/filter/impl/tps_limit_filter_test.go +++ b/filter/filter/tps_limit_filter_test.go @@ -15,9 +15,12 @@ * limitations under the License. */ -package impl +package filter import ( + "github.com/apache/dubbo-go/filter" + common2 "github.com/apache/dubbo-go/filter/common" + "github.com/apache/dubbo-go/filter/tps" "net/url" "testing" ) @@ -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..b69c567ffe 100644 --- a/filter/impl/tps/impl/tps_limit_fix_window_strategy.go +++ b/filter/tps/tps_limit_fix_window_strategy.go @@ -15,9 +15,10 @@ * limitations under the License. */ -package impl +package tps import ( + "github.com/apache/dubbo-go/filter" "sync/atomic" "time" ) @@ -25,7 +26,6 @@ import ( import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/filter/impl/tps" ) 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..ec2afe6021 100644 --- a/filter/impl/tps/impl/tps_limit_sliding_window_strategy.go +++ b/filter/tps/tps_limit_sliding_window_strategy.go @@ -15,17 +15,17 @@ * limitations under the License. */ -package impl +package tps import ( "container/list" + "github.com/apache/dubbo-go/filter" "sync" "time" ) import ( "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/filter/impl/tps" ) 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..279c3b0034 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,15 +15,15 @@ * limitations under the License. */ -package impl +package tps import ( + "github.com/apache/dubbo-go/filter" "sync" ) import ( "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/filter/impl/tps" ) 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..11137b784d 100644 --- a/filter/impl/tps/impl/tps_limiter_method_service.go +++ b/filter/tps/tps_limiter_method_service.go @@ -14,10 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package impl +package tps import ( "fmt" + "github.com/apache/dubbo-go/filter" "strconv" "sync" ) @@ -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" ) @@ -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..80e5797d50 100644 --- a/filter/impl/tps/impl/tps_limiter_method_service_test.go +++ b/filter/tps/tps_limiter_method_service_test.go @@ -15,9 +15,10 @@ * limitations under the License. */ -package impl +package tps import ( + "github.com/apache/dubbo-go/filter" "net/url" "testing" ) @@ -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" From bd7989ad3846dab35daac7094e558dd446657237 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 5 Jan 2020 23:08:26 +0800 Subject: [PATCH 14/14] fix --- filter/filter/graceful_shutdown_filter_test.go | 4 ++-- filter/filter/tps_limit_filter_test.go | 6 +++--- filter/tps/tps_limit_fix_window_strategy.go | 2 +- filter/tps/tps_limit_sliding_window_strategy.go | 2 +- filter/tps/tps_limit_thread_safe_fix_window_strategy.go | 2 +- filter/tps/tps_limiter_method_service.go | 2 +- filter/tps/tps_limiter_method_service_test.go | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/filter/filter/graceful_shutdown_filter_test.go b/filter/filter/graceful_shutdown_filter_test.go index 38430a5d1e..af57cd4ec8 100644 --- a/filter/filter/graceful_shutdown_filter_test.go +++ b/filter/filter/graceful_shutdown_filter_test.go @@ -18,8 +18,6 @@ package filter import ( - "github.com/apache/dubbo-go/filter" - common2 "github.com/apache/dubbo-go/filter/common" "net/url" "testing" ) @@ -33,6 +31,8 @@ import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/config" + "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" ) diff --git a/filter/filter/tps_limit_filter_test.go b/filter/filter/tps_limit_filter_test.go index d4353bb7d1..6acaab7036 100644 --- a/filter/filter/tps_limit_filter_test.go +++ b/filter/filter/tps_limit_filter_test.go @@ -18,14 +18,14 @@ package filter import ( - "github.com/apache/dubbo-go/filter" - common2 "github.com/apache/dubbo-go/filter/common" - "github.com/apache/dubbo-go/filter/tps" "net/url" "testing" ) 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" ) diff --git a/filter/tps/tps_limit_fix_window_strategy.go b/filter/tps/tps_limit_fix_window_strategy.go index b69c567ffe..6ea5dc1033 100644 --- a/filter/tps/tps_limit_fix_window_strategy.go +++ b/filter/tps/tps_limit_fix_window_strategy.go @@ -18,7 +18,6 @@ package tps import ( - "github.com/apache/dubbo-go/filter" "sync/atomic" "time" ) @@ -26,6 +25,7 @@ import ( import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/filter" ) const ( diff --git a/filter/tps/tps_limit_sliding_window_strategy.go b/filter/tps/tps_limit_sliding_window_strategy.go index ec2afe6021..40ea2d14be 100644 --- a/filter/tps/tps_limit_sliding_window_strategy.go +++ b/filter/tps/tps_limit_sliding_window_strategy.go @@ -19,13 +19,13 @@ package tps import ( "container/list" - "github.com/apache/dubbo-go/filter" "sync" "time" ) import ( "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/filter" ) func init() { diff --git a/filter/tps/tps_limit_thread_safe_fix_window_strategy.go b/filter/tps/tps_limit_thread_safe_fix_window_strategy.go index 279c3b0034..faec9b6ec1 100644 --- a/filter/tps/tps_limit_thread_safe_fix_window_strategy.go +++ b/filter/tps/tps_limit_thread_safe_fix_window_strategy.go @@ -18,12 +18,12 @@ package tps import ( - "github.com/apache/dubbo-go/filter" "sync" ) import ( "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/filter" ) func init() { diff --git a/filter/tps/tps_limiter_method_service.go b/filter/tps/tps_limiter_method_service.go index 11137b784d..ac4498a33d 100644 --- a/filter/tps/tps_limiter_method_service.go +++ b/filter/tps/tps_limiter_method_service.go @@ -18,7 +18,6 @@ package tps import ( "fmt" - "github.com/apache/dubbo-go/filter" "strconv" "sync" ) @@ -31,6 +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" "github.com/apache/dubbo-go/protocol" ) diff --git a/filter/tps/tps_limiter_method_service_test.go b/filter/tps/tps_limiter_method_service_test.go index 80e5797d50..441224a3e3 100644 --- a/filter/tps/tps_limiter_method_service_test.go +++ b/filter/tps/tps_limiter_method_service_test.go @@ -18,11 +18,11 @@ package tps import ( - "github.com/apache/dubbo-go/filter" "net/url" "testing" ) import ( + "github.com/apache/dubbo-go/filter" "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" )