From bf11658616bd8cbc3ca7330983762b0833d2cf63 Mon Sep 17 00:00:00 2001 From: vicanso Date: Mon, 8 Jul 2019 21:17:36 +0800 Subject: [PATCH] refactor: support more limiter --- README.md | 4 ++-- router_concurrent_limiter.go | 24 +++++++++++++++--------- router_concurrent_limiter_test.go | 5 +++-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8ecaf6a..b27266c 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Router concurrent limiter for cod, it support custom max concurrency for each router. -- `NewLimiter` create a limiter for router concurrent limit. +- `NewLocalLimiter` create a limiter for router concurrent limit. ```go package main @@ -23,7 +23,7 @@ func main() { d := cod.New() d.Use(routerLimiter.New(routerLimiter.Config{ - Limiter: routerLimiter.NewLimiter(map[string]uint32{ + Limiter: routerLimiter.NewLocalLimiter(map[string]uint32{ "/users/me": 2, }), })) diff --git a/router_concurrent_limiter.go b/router_concurrent_limiter.go index a576b4b..b80e3f5 100644 --- a/router_concurrent_limiter.go +++ b/router_concurrent_limiter.go @@ -38,20 +38,26 @@ type ( // Config router concurrent limiter config Config struct { Skipper cod.Skipper - Limiter *Limiter + Limiter Limiter } concurrency struct { max uint32 current uint32 } - // Limiter limiter - Limiter struct { + // Limiter limiter interface + Limiter interface { + IncConcurrency(route string) (current uint32, max uint32) + DecConcurrency(route string) + GetConcurrency(route string) (current uint32) + } + // LocalLimiter local limiter + LocalLimiter struct { m map[string]*concurrency } ) -// NewLimiter create a new limiter -func NewLimiter(data map[string]uint32) *Limiter { +// NewLocalLimiter create a new limiter +func NewLocalLimiter(data map[string]uint32) *LocalLimiter { m := make(map[string]*concurrency) for route, max := range data { m[route] = &concurrency{ @@ -59,13 +65,13 @@ func NewLimiter(data map[string]uint32) *Limiter { current: 0, } } - return &Limiter{ + return &LocalLimiter{ m: m, } } // IncConcurrency concurrency inc -func (l *Limiter) IncConcurrency(route string) (current, max uint32) { +func (l *LocalLimiter) IncConcurrency(route string) (current, max uint32) { concur, ok := l.m[route] if !ok { return 0, 0 @@ -75,7 +81,7 @@ func (l *Limiter) IncConcurrency(route string) (current, max uint32) { } // DecConcurrency concurrency dec -func (l *Limiter) DecConcurrency(route string) { +func (l *LocalLimiter) DecConcurrency(route string) { concur, ok := l.m[route] if !ok { return @@ -84,7 +90,7 @@ func (l *Limiter) DecConcurrency(route string) { } // GetConcurrency get concurrency -func (l *Limiter) GetConcurrency(route string) uint32 { +func (l *LocalLimiter) GetConcurrency(route string) uint32 { concur, ok := l.m[route] if !ok { return 0 diff --git a/router_concurrent_limiter_test.go b/router_concurrent_limiter_test.go index fde859b..c0be1d2 100644 --- a/router_concurrent_limiter_test.go +++ b/router_concurrent_limiter_test.go @@ -27,8 +27,9 @@ import ( ) func TestLimiter(t *testing.T) { + assert := assert.New(t) - limiter := NewLimiter(map[string]uint32{ + limiter := NewLocalLimiter(map[string]uint32{ "/users/login": 10, "/books/:id": 100, }) @@ -60,7 +61,7 @@ func TestNoLimiterPanic(t *testing.T) { } func TestRouterConcurrentLimiter(t *testing.T) { - limiter := NewLimiter(map[string]uint32{ + limiter := NewLocalLimiter(map[string]uint32{ "/users/login": 1, "/books/:id": 100, })