From 34902904fdc989735fe4ecbfd82067c286cd1efe Mon Sep 17 00:00:00 2001 From: Ming Deng <mindeng@ebay.com> Date: Thu, 17 Oct 2019 13:43:25 +0800 Subject: [PATCH] Fix review: move the tps_limit to filter/impl/tps/ --- common/extension/tps_limit.go | 20 ++++++++-------- config/testdata/provider_config.yml | 9 +++++++ .../tps/intf}/rejected_execution_handler.go | 15 +++++++++++- .../{ => impl/tps/intf}/tps_limit_strategy.go | 13 +++++++++- filter/{ => impl/tps/intf}/tps_limiter.go | 2 +- .../tps}/rejected_execution_handler_mock.go | 2 +- ...ejected_execution_handler_only_log_impl.go | 22 ++++++++++------- ...ed_execution_handler_only_log_impl_test.go | 2 +- .../tps_limit_fix_window_strategy_impl.go | 6 ++--- ...tps_limit_fix_window_strategy_impl_test.go | 2 +- .../tps_limit_sliding_window_strategy_impl.go | 6 ++--- ...limit_sliding_window_strategy_impl_test.go | 24 +++++++++---------- .../{ => impl/tps}/tps_limit_strategy_mock.go | 2 +- ...it_thread_safe_fix_window_strategy_impl.go | 6 ++--- ...read_safe_fix_window_strategy_impl_test.go | 22 ++++++++--------- .../tps_limiter_method_service_impl.go | 10 ++++---- .../tps_limiter_method_service_impl_test.go | 16 ++++++------- filter/{ => impl/tps}/tps_limiter_mock.go | 2 +- filter/impl/tps_limit_filter_test.go | 15 ++++++------ 19 files changed, 118 insertions(+), 78 deletions(-) rename filter/{ => impl/tps/intf}/rejected_execution_handler.go (70%) rename filter/{ => impl/tps/intf}/tps_limit_strategy.go (69%) rename filter/{ => impl/tps/intf}/tps_limiter.go (98%) rename filter/{ => impl/tps}/rejected_execution_handler_mock.go (99%) rename filter/impl/{ => tps}/rejected_execution_handler_only_log_impl.go (75%) rename filter/impl/{ => tps}/rejected_execution_handler_only_log_impl_test.go (98%) rename filter/impl/{ => tps}/tps_limit_fix_window_strategy_impl.go (96%) rename filter/impl/{ => tps}/tps_limit_fix_window_strategy_impl_test.go (99%) rename filter/impl/{ => tps}/tps_limit_sliding_window_strategy_impl.go (96%) rename filter/impl/{ => tps}/tps_limit_sliding_window_strategy_impl_test.go (67%) rename filter/{ => impl/tps}/tps_limit_strategy_mock.go (99%) rename filter/impl/{ => tps}/tps_limit_thread_safe_fix_window_strategy_impl.go (95%) rename filter/impl/{ => tps}/tps_limit_thread_safe_fix_window_strategy_impl_test.go (69%) rename filter/impl/{ => tps}/tps_limiter_method_service_impl.go (96%) rename filter/impl/{ => tps}/tps_limiter_method_service_impl_test.go (93%) rename filter/{ => impl/tps}/tps_limiter_mock.go (99%) diff --git a/common/extension/tps_limit.go b/common/extension/tps_limit.go index 626361e49..e789d5b10 100644 --- a/common/extension/tps_limit.go +++ b/common/extension/tps_limit.go @@ -18,20 +18,20 @@ package extension import ( - "github.com/apache/dubbo-go/filter" + "github.com/apache/dubbo-go/filter/impl/tps/intf" ) var ( - tpsLimitStrategy = make(map[string]func(rate int, interval int) filter.TpsLimitStrategy) - tpsLimiter = make(map[string]func() filter.TpsLimiter) - tpsRejectedExecutionHandler = make(map[string]func() filter.RejectedExecutionHandler) + tpsLimitStrategy = make(map[string]func(rate int, interval int) intf.TpsLimitStrategy) + tpsLimiter = make(map[string]func() intf.TpsLimiter) + tpsRejectedExecutionHandler = make(map[string]func() intf.RejectedExecutionHandler) ) -func SetTpsLimiter(name string, creator func() filter.TpsLimiter) { +func SetTpsLimiter(name string, creator func() intf.TpsLimiter) { tpsLimiter[name] = creator } -func GetTpsLimiter(name string) filter.TpsLimiter { +func GetTpsLimiter(name string) intf.TpsLimiter { creator, ok := tpsLimiter[name] if !ok { panic("TpsLimiter for " + name + " is not existing, make sure you have import the package " + @@ -40,11 +40,11 @@ func GetTpsLimiter(name string) filter.TpsLimiter { return creator() } -func SetTpsLimitStrategy(name string, creator func(rate int, interval int) filter.TpsLimitStrategy) { +func SetTpsLimitStrategy(name string, creator func(rate int, interval int) intf.TpsLimitStrategy) { tpsLimitStrategy[name] = creator } -func GetTpsLimitStrategyCreator(name string) func(rate int, interval int) filter.TpsLimitStrategy { +func GetTpsLimitStrategyCreator(name string) func(rate int, interval int) intf.TpsLimitStrategy { creator, ok := tpsLimitStrategy[name] if !ok { panic("TpsLimitStrategy for " + name + " is not existing, make sure you have import the package " + @@ -53,11 +53,11 @@ func GetTpsLimitStrategyCreator(name string) func(rate int, interval int) filter return creator } -func SetTpsRejectedExecutionHandler(name string, creator func() filter.RejectedExecutionHandler) { +func SetTpsRejectedExecutionHandler(name string, creator func() intf.RejectedExecutionHandler) { tpsRejectedExecutionHandler[name] = creator } -func GetTpsRejectedExecutionHandler(name string) filter.RejectedExecutionHandler { +func GetTpsRejectedExecutionHandler(name string) intf.RejectedExecutionHandler { creator, ok := tpsRejectedExecutionHandler[name] if !ok { panic("TpsRejectedExecutionHandler for " + name + " is not existing, make sure you have import the package " + diff --git a/config/testdata/provider_config.yml b/config/testdata/provider_config.yml index 5cbefe081..71e45b9c0 100644 --- a/config/testdata/provider_config.yml +++ b/config/testdata/provider_config.yml @@ -29,6 +29,15 @@ services: "UserProvider": registry: "hangzhouzk,shanghaizk" filter: "" + # the name of limiter + tps.limiter: "default" + # the time unit of interval is ms + tps.limit.interval: 60000 + tps.limit.rate: 200 + # the name of strategy + tps.limit.strategy: "slidingWindow" + # the name of RejectedExecutionHandler + tps.limit.rejected.handler: "default" protocol : "dubbo" # equivalent to interface of dubbo.xml interface : "com.ikurento.user.UserProvider" diff --git a/filter/rejected_execution_handler.go b/filter/impl/tps/intf/rejected_execution_handler.go similarity index 70% rename from filter/rejected_execution_handler.go rename to filter/impl/tps/intf/rejected_execution_handler.go index 1773edcbd..1e0c0938f 100644 --- a/filter/rejected_execution_handler.go +++ b/filter/impl/tps/intf/rejected_execution_handler.go @@ -14,13 +14,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package filter +package intf import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/protocol" ) +/** + * This implementation only logs the invocation info. + * it always return en error inside the result. + * "UserProvider": + * registry: "hangzhouzk" + * protocol : "dubbo" + * interface : "com.ikurento.user.UserProvider" + * ... # other configuration + * tps.limiter: "method-service" # the name of limiter + * tps.limit.rejected.handler: "name of handler" + * methods: + * - name: "GetUser" + */ type RejectedExecutionHandler interface { RejectedExecution(url common.URL, invocation protocol.Invocation) protocol.Result } diff --git a/filter/tps_limit_strategy.go b/filter/impl/tps/intf/tps_limit_strategy.go similarity index 69% rename from filter/tps_limit_strategy.go rename to filter/impl/tps/intf/tps_limit_strategy.go index 602d20f79..ed52daebf 100644 --- a/filter/tps_limit_strategy.go +++ b/filter/impl/tps/intf/tps_limit_strategy.go @@ -15,10 +15,21 @@ * limitations under the License. */ -package filter +package intf /* * please register your implementation by invoking SetTpsLimitStrategy + * "UserProvider": + * registry: "hangzhouzk" + * protocol : "dubbo" + * interface : "com.ikurento.user.UserProvider" + * ... # other configuration + * tps.limiter: "method-service" # the name of limiter + * tps.limit.strategy: "name of implementation" # service-level + * methods: + * - name: "GetUser" + * tps.interval: 3000 + * tps.limit.strategy: "name of implementation" # method-level */ type TpsLimitStrategy interface { IsAllowable() bool diff --git a/filter/tps_limiter.go b/filter/impl/tps/intf/tps_limiter.go similarity index 98% rename from filter/tps_limiter.go rename to filter/impl/tps/intf/tps_limiter.go index 1d2b2341a..d3935c11d 100644 --- a/filter/tps_limiter.go +++ b/filter/impl/tps/intf/tps_limiter.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package filter +package intf import ( "github.com/apache/dubbo-go/common" diff --git a/filter/rejected_execution_handler_mock.go b/filter/impl/tps/rejected_execution_handler_mock.go similarity index 99% rename from filter/rejected_execution_handler_mock.go rename to filter/impl/tps/rejected_execution_handler_mock.go index 20c3e575b..55b01f022 100644 --- a/filter/rejected_execution_handler_mock.go +++ b/filter/impl/tps/rejected_execution_handler_mock.go @@ -18,7 +18,7 @@ // Source: rejected_execution_handler.go // Package filter is a generated GoMock package. -package filter +package tps import ( common "github.com/apache/dubbo-go/common" diff --git a/filter/impl/rejected_execution_handler_only_log_impl.go b/filter/impl/tps/rejected_execution_handler_only_log_impl.go similarity index 75% rename from filter/impl/rejected_execution_handler_only_log_impl.go rename to filter/impl/tps/rejected_execution_handler_only_log_impl.go index bc67fa4ec..596160f07 100644 --- a/filter/impl/rejected_execution_handler_only_log_impl.go +++ b/filter/impl/tps/rejected_execution_handler_only_log_impl.go @@ -15,21 +15,18 @@ * limitations under the License. */ -package impl +package tps import ( "sync" ) -import ( - "github.com/prometheus/common/log" -) - 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/common/logger" + "github.com/apache/dubbo-go/filter/impl/tps/intf" "github.com/apache/dubbo-go/protocol" ) @@ -46,16 +43,25 @@ var onlyLogHandlerOnce sync.Once /** * This implementation only logs the invocation info. * it always return en error inside the result. + * "UserProvider": + * registry: "hangzhouzk" + * protocol : "dubbo" + * interface : "com.ikurento.user.UserProvider" + * ... # other configuration + * tps.limiter: "method-service" # the name of limiter + * tps.limit.rejected.handler: "default" or "log" + * methods: + * - name: "GetUser" */ type OnlyLogRejectedExecutionHandler struct { } func (handler *OnlyLogRejectedExecutionHandler) RejectedExecution(url common.URL, invocation protocol.Invocation) protocol.Result { - log.Errorf("The invocation was rejected due to over rate limitation. url: %s", url.String()) + logger.Errorf("The invocation was rejected due to over rate limitation. url: %s", url.String()) return &protocol.RPCResult{} } -func GetOnlyLogRejectedExecutionHandler() filter.RejectedExecutionHandler { +func GetOnlyLogRejectedExecutionHandler() intf.RejectedExecutionHandler { onlyLogHandlerOnce.Do(func() { onlyLogHandlerInstance = &OnlyLogRejectedExecutionHandler{} }) diff --git a/filter/impl/rejected_execution_handler_only_log_impl_test.go b/filter/impl/tps/rejected_execution_handler_only_log_impl_test.go similarity index 98% rename from filter/impl/rejected_execution_handler_only_log_impl_test.go rename to filter/impl/tps/rejected_execution_handler_only_log_impl_test.go index da54d8a10..beff7e3f2 100644 --- a/filter/impl/rejected_execution_handler_only_log_impl_test.go +++ b/filter/impl/tps/rejected_execution_handler_only_log_impl_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package tps import ( "net/url" diff --git a/filter/impl/tps_limit_fix_window_strategy_impl.go b/filter/impl/tps/tps_limit_fix_window_strategy_impl.go similarity index 96% rename from filter/impl/tps_limit_fix_window_strategy_impl.go rename to filter/impl/tps/tps_limit_fix_window_strategy_impl.go index 04e79b9b4..7e4032c68 100644 --- a/filter/impl/tps_limit_fix_window_strategy_impl.go +++ b/filter/impl/tps/tps_limit_fix_window_strategy_impl.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package tps import ( "sync/atomic" @@ -24,7 +24,7 @@ import ( import ( "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/filter/impl/tps/intf" ) const ( @@ -75,7 +75,7 @@ func (impl *FixedWindowTpsLimitStrategyImpl) IsAllowable() bool { return atomic.AddInt32(&impl.count, 1) <= impl.rate } -func NewFixedWindowTpsLimitStrategyImpl(rate int, interval int) filter.TpsLimitStrategy { +func NewFixedWindowTpsLimitStrategyImpl(rate int, interval int) intf.TpsLimitStrategy { return &FixedWindowTpsLimitStrategyImpl{ rate: int32(rate), interval: int64(interval * 1000), // convert to ns diff --git a/filter/impl/tps_limit_fix_window_strategy_impl_test.go b/filter/impl/tps/tps_limit_fix_window_strategy_impl_test.go similarity index 99% rename from filter/impl/tps_limit_fix_window_strategy_impl_test.go rename to filter/impl/tps/tps_limit_fix_window_strategy_impl_test.go index 55d0b14b7..8848a05dd 100644 --- a/filter/impl/tps_limit_fix_window_strategy_impl_test.go +++ b/filter/impl/tps/tps_limit_fix_window_strategy_impl_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package tps import ( "testing" diff --git a/filter/impl/tps_limit_sliding_window_strategy_impl.go b/filter/impl/tps/tps_limit_sliding_window_strategy_impl.go similarity index 96% rename from filter/impl/tps_limit_sliding_window_strategy_impl.go rename to filter/impl/tps/tps_limit_sliding_window_strategy_impl.go index 726ac197a..3a8db5ab3 100644 --- a/filter/impl/tps_limit_sliding_window_strategy_impl.go +++ b/filter/impl/tps/tps_limit_sliding_window_strategy_impl.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" + "github.com/apache/dubbo-go/filter/impl/tps/intf" ) func init() { @@ -80,7 +80,7 @@ func (impl *SlidingWindowTpsLimitStrategyImpl) IsAllowable() bool { return false } -func NewSlidingWindowTpsLimitStrategyImpl(rate int, interval int) filter.TpsLimitStrategy { +func NewSlidingWindowTpsLimitStrategyImpl(rate int, interval int) intf.TpsLimitStrategy { return &SlidingWindowTpsLimitStrategyImpl{ rate: rate, interval: int64(interval * 1000), diff --git a/filter/impl/tps_limit_sliding_window_strategy_impl_test.go b/filter/impl/tps/tps_limit_sliding_window_strategy_impl_test.go similarity index 67% rename from filter/impl/tps_limit_sliding_window_strategy_impl_test.go rename to filter/impl/tps/tps_limit_sliding_window_strategy_impl_test.go index 26a9400cd..1bca13d40 100644 --- a/filter/impl/tps_limit_sliding_window_strategy_impl_test.go +++ b/filter/impl/tps/tps_limit_sliding_window_strategy_impl_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package tps import ( "testing" @@ -23,23 +23,23 @@ import ( ) import ( - "go.etcd.io/etcd/pkg/testutil" + "github.com/stretchr/testify/assert" ) func TestSlidingWindowTpsLimitStrategyImpl_IsAllowable(t *testing.T) { strategy := NewSlidingWindowTpsLimitStrategyImpl(2, 60000) - testutil.AssertTrue(t, strategy.IsAllowable()) - testutil.AssertTrue(t, strategy.IsAllowable()) - testutil.AssertFalse(t, strategy.IsAllowable()) + assert.True(t, strategy.IsAllowable()) + assert.True(t, strategy.IsAllowable()) + assert.False(t, strategy.IsAllowable()) time.Sleep(time.Duration(2100 * 1000)) - testutil.AssertFalse(t, strategy.IsAllowable()) + assert.False(t, strategy.IsAllowable()) strategy = NewSlidingWindowTpsLimitStrategyImpl(2, 2000) - testutil.AssertTrue(t, strategy.IsAllowable()) - testutil.AssertTrue(t, strategy.IsAllowable()) - testutil.AssertFalse(t, strategy.IsAllowable()) + assert.True(t, strategy.IsAllowable()) + assert.True(t, strategy.IsAllowable()) + assert.False(t, strategy.IsAllowable()) time.Sleep(time.Duration(2100 * 1000)) - testutil.AssertTrue(t, strategy.IsAllowable()) - testutil.AssertTrue(t, strategy.IsAllowable()) - testutil.AssertFalse(t, strategy.IsAllowable()) + assert.True(t, strategy.IsAllowable()) + assert.True(t, strategy.IsAllowable()) + assert.False(t, strategy.IsAllowable()) } diff --git a/filter/tps_limit_strategy_mock.go b/filter/impl/tps/tps_limit_strategy_mock.go similarity index 99% rename from filter/tps_limit_strategy_mock.go rename to filter/impl/tps/tps_limit_strategy_mock.go index 4adf5848d..72c658fb9 100644 --- a/filter/tps_limit_strategy_mock.go +++ b/filter/impl/tps/tps_limit_strategy_mock.go @@ -18,7 +18,7 @@ // Source: tps_limit_strategy.go // Package filter is a generated GoMock package. -package filter +package tps import ( gomock "github.com/golang/mock/gomock" diff --git a/filter/impl/tps_limit_thread_safe_fix_window_strategy_impl.go b/filter/impl/tps/tps_limit_thread_safe_fix_window_strategy_impl.go similarity index 95% rename from filter/impl/tps_limit_thread_safe_fix_window_strategy_impl.go rename to filter/impl/tps/tps_limit_thread_safe_fix_window_strategy_impl.go index e7d592c0b..3541252db 100644 --- a/filter/impl/tps_limit_thread_safe_fix_window_strategy_impl.go +++ b/filter/impl/tps/tps_limit_thread_safe_fix_window_strategy_impl.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" + "github.com/apache/dubbo-go/filter/impl/tps/intf" ) func init() { @@ -56,7 +56,7 @@ func (impl *ThreadSafeFixedWindowTpsLimitStrategyImpl) IsAllowable() bool { return impl.fixedWindow.IsAllowable() } -func NewThreadSafeFixedWindowTpsLimitStrategyImpl(rate int, interval int) filter.TpsLimitStrategy { +func NewThreadSafeFixedWindowTpsLimitStrategyImpl(rate int, interval int) intf.TpsLimitStrategy { fixedWindowStrategy := NewFixedWindowTpsLimitStrategyImpl(rate, interval).(*FixedWindowTpsLimitStrategyImpl) return &ThreadSafeFixedWindowTpsLimitStrategyImpl{ fixedWindow: fixedWindowStrategy, diff --git a/filter/impl/tps_limit_thread_safe_fix_window_strategy_impl_test.go b/filter/impl/tps/tps_limit_thread_safe_fix_window_strategy_impl_test.go similarity index 69% rename from filter/impl/tps_limit_thread_safe_fix_window_strategy_impl_test.go rename to filter/impl/tps/tps_limit_thread_safe_fix_window_strategy_impl_test.go index e41dd62f2..72cd4800d 100644 --- a/filter/impl/tps_limit_thread_safe_fix_window_strategy_impl_test.go +++ b/filter/impl/tps/tps_limit_thread_safe_fix_window_strategy_impl_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package tps import ( "testing" @@ -23,21 +23,21 @@ import ( ) import ( - "github.com/coreos/etcd/pkg/testutil" + "github.com/stretchr/testify/assert" ) func TestThreadSafeFixedWindowTpsLimitStrategyImpl_IsAllowable(t *testing.T) { strategy := NewThreadSafeFixedWindowTpsLimitStrategyImpl(2, 60000) - testutil.AssertTrue(t, strategy.IsAllowable()) - testutil.AssertTrue(t, strategy.IsAllowable()) - testutil.AssertFalse(t, strategy.IsAllowable()) + assert.True(t, strategy.IsAllowable()) + assert.True(t, strategy.IsAllowable()) + assert.False(t, strategy.IsAllowable()) strategy = NewThreadSafeFixedWindowTpsLimitStrategyImpl(2, 2000) - testutil.AssertTrue(t, strategy.IsAllowable()) - testutil.AssertTrue(t, strategy.IsAllowable()) - testutil.AssertFalse(t, strategy.IsAllowable()) + assert.True(t, strategy.IsAllowable()) + assert.True(t, strategy.IsAllowable()) + assert.False(t, strategy.IsAllowable()) time.Sleep(time.Duration(2100 * 1000)) - testutil.AssertTrue(t, strategy.IsAllowable()) - testutil.AssertTrue(t, strategy.IsAllowable()) - testutil.AssertFalse(t, strategy.IsAllowable()) + assert.True(t, strategy.IsAllowable()) + assert.True(t, strategy.IsAllowable()) + assert.False(t, strategy.IsAllowable()) } diff --git a/filter/impl/tps_limiter_method_service_impl.go b/filter/impl/tps/tps_limiter_method_service_impl.go similarity index 96% rename from filter/impl/tps_limiter_method_service_impl.go rename to filter/impl/tps/tps_limiter_method_service_impl.go index 23cd64f5c..01d9f6564 100644 --- a/filter/impl/tps_limiter_method_service_impl.go +++ b/filter/impl/tps/tps_limiter_method_service_impl.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" + "github.com/apache/dubbo-go/filter/impl/tps/intf" "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.(filter.TpsLimitStrategy).IsAllowable() + return limitState.(intf.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(int(limitRate), int(limitInterval))) - return limitState.(filter.TpsLimitStrategy).IsAllowable() + return limitState.(intf.TpsLimitStrategy).IsAllowable() } func getLimitConfig(methodLevelConfig string, @@ -178,7 +178,7 @@ func getLimitConfig(methodLevelConfig string, var methodServiceTpsLimiterInstance *MethodServiceTpsLimiterImpl var methodServiceTpsLimiterOnce sync.Once -func GetMethodServiceTpsLimiter() filter.TpsLimiter { +func GetMethodServiceTpsLimiter() intf.TpsLimiter { methodServiceTpsLimiterOnce.Do(func() { methodServiceTpsLimiterInstance = &MethodServiceTpsLimiterImpl{ tpsState: concurrent.NewMap(), diff --git a/filter/impl/tps_limiter_method_service_impl_test.go b/filter/impl/tps/tps_limiter_method_service_impl_test.go similarity index 93% rename from filter/impl/tps_limiter_method_service_impl_test.go rename to filter/impl/tps/tps_limiter_method_service_impl_test.go index c0beee9cf..8a6fa8398 100644 --- a/filter/impl/tps_limiter_method_service_impl_test.go +++ b/filter/impl/tps/tps_limiter_method_service_impl_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package impl +package tps import ( "net/url" @@ -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" + "github.com/apache/dubbo-go/filter/impl/tps/intf" "github.com/apache/dubbo-go/protocol/invocation" ) @@ -46,9 +46,9 @@ func TestMethodServiceTpsLimiterImpl_IsAllowable_Only_Service_Level(t *testing.T common.WithParamsValue(constant.INTERFACE_KEY, methodName), common.WithParamsValue(constant.TPS_LIMIT_RATE_KEY, "20")) - mockStrategyImpl := filter.NewMockTpsLimitStrategy(ctrl) + mockStrategyImpl := NewMockTpsLimitStrategy(ctrl) mockStrategyImpl.EXPECT().IsAllowable().Return(true).Times(1) - extension.SetTpsLimitStrategy(constant.DEFAULT_KEY, func(rate int, interval int) filter.TpsLimitStrategy { + extension.SetTpsLimitStrategy(constant.DEFAULT_KEY, func(rate int, interval int) intf.TpsLimitStrategy { assert.Equal(t, 20, rate) assert.Equal(t, 60000, interval) return mockStrategyImpl @@ -93,9 +93,9 @@ func TestMethodServiceTpsLimiterImpl_IsAllowable_Method_Level_Override(t *testin common.WithParamsValue(methodConfigPrefix+constant.TPS_LIMIT_STRATEGY_KEY, "default"), ) - mockStrategyImpl := filter.NewMockTpsLimitStrategy(ctrl) + mockStrategyImpl := NewMockTpsLimitStrategy(ctrl) mockStrategyImpl.EXPECT().IsAllowable().Return(true).Times(1) - extension.SetTpsLimitStrategy(constant.DEFAULT_KEY, func(rate int, interval int) filter.TpsLimitStrategy { + extension.SetTpsLimitStrategy(constant.DEFAULT_KEY, func(rate int, interval int) intf.TpsLimitStrategy { assert.Equal(t, 40, rate) assert.Equal(t, 7000, interval) return mockStrategyImpl @@ -121,9 +121,9 @@ func TestMethodServiceTpsLimiterImpl_IsAllowable_Both_Method_And_Service(t *test common.WithParamsValue(methodConfigPrefix+constant.TPS_LIMIT_RATE_KEY, "40"), ) - mockStrategyImpl := filter.NewMockTpsLimitStrategy(ctrl) + mockStrategyImpl := NewMockTpsLimitStrategy(ctrl) mockStrategyImpl.EXPECT().IsAllowable().Return(true).Times(1) - extension.SetTpsLimitStrategy(constant.DEFAULT_KEY, func(rate int, interval int) filter.TpsLimitStrategy { + extension.SetTpsLimitStrategy(constant.DEFAULT_KEY, func(rate int, interval int) intf.TpsLimitStrategy { assert.Equal(t, 40, rate) assert.Equal(t, 3000, interval) return mockStrategyImpl diff --git a/filter/tps_limiter_mock.go b/filter/impl/tps/tps_limiter_mock.go similarity index 99% rename from filter/tps_limiter_mock.go rename to filter/impl/tps/tps_limiter_mock.go index 5fbf71761..a247bf14d 100644 --- a/filter/tps_limiter_mock.go +++ b/filter/impl/tps/tps_limiter_mock.go @@ -18,7 +18,7 @@ // Source: tps_limiter.go // Package filter is a generated GoMock package. -package filter +package tps import ( common "github.com/apache/dubbo-go/common" diff --git a/filter/impl/tps_limit_filter_test.go b/filter/impl/tps_limit_filter_test.go index 45ce2a5e6..18a72ba9e 100644 --- a/filter/impl/tps_limit_filter_test.go +++ b/filter/impl/tps_limit_filter_test.go @@ -31,7 +31,8 @@ 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/filter/impl/tps" + "github.com/apache/dubbo-go/filter/impl/tps/intf" "github.com/apache/dubbo-go/protocol" "github.com/apache/dubbo-go/protocol/invocation" ) @@ -53,9 +54,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 := filter.NewMockTpsLimiter(ctrl) + mockLimiter := tps.NewMockTpsLimiter(ctrl) mockLimiter.EXPECT().IsAllowable(gomock.Any(), gomock.Any()).Return(true).Times(1) - extension.SetTpsLimiter(constant.DEFAULT_KEY, func() filter.TpsLimiter { + extension.SetTpsLimiter(constant.DEFAULT_KEY, func() intf.TpsLimiter { return mockLimiter }) @@ -74,17 +75,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 := filter.NewMockTpsLimiter(ctrl) + mockLimiter := tps.NewMockTpsLimiter(ctrl) mockLimiter.EXPECT().IsAllowable(gomock.Any(), gomock.Any()).Return(false).Times(1) - extension.SetTpsLimiter(constant.DEFAULT_KEY, func() filter.TpsLimiter { + extension.SetTpsLimiter(constant.DEFAULT_KEY, func() intf.TpsLimiter { return mockLimiter }) mockResult := &protocol.RPCResult{} - mockRejectedHandler := filter.NewMockRejectedExecutionHandler(ctrl) + mockRejectedHandler := tps.NewMockRejectedExecutionHandler(ctrl) mockRejectedHandler.EXPECT().RejectedExecution(gomock.Any(), gomock.Any()).Return(mockResult).Times(1) - extension.SetTpsRejectedExecutionHandler(constant.DEFAULT_KEY, func() filter.RejectedExecutionHandler { + extension.SetTpsRejectedExecutionHandler(constant.DEFAULT_KEY, func() intf.RejectedExecutionHandler { return mockRejectedHandler }) -- GitLab