From 76a7cca54e78a4d74869b2b15d0e81c1b21c0a5f Mon Sep 17 00:00:00 2001 From: vicanso Date: Fri, 26 Jul 2019 21:32:20 +0800 Subject: [PATCH] refactor: use method and route for router limit key --- README.md | 2 +- go.mod | 7 ++----- go.sum | 10 ++++------ router_concurrent_limiter.go | 18 +++++++++--------- router_concurrent_limiter_test.go | 14 +++++++++----- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index b27266c..1f30ccb 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ func main() { d.Use(routerLimiter.New(routerLimiter.Config{ Limiter: routerLimiter.NewLocalLimiter(map[string]uint32{ - "/users/me": 2, + "GET /users/me": 2, }), })) d.Use(responder.NewDefault()) diff --git a/go.mod b/go.mod index 39efe88..a999ea2 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,7 @@ module github.com/vicanso/cod-router-concurrent-limiter go 1.12 require ( - github.com/json-iterator/go v1.1.6 - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.1 // indirect github.com/stretchr/testify v1.3.0 - github.com/vicanso/cod v0.1.4 - github.com/vicanso/hes v0.1.4 + github.com/vicanso/cod v0.1.5 + github.com/vicanso/hes v0.2.1 ) diff --git a/go.sum b/go.sum index 091cdd3..e250e52 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,16 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/vicanso/cod v0.1.4 h1:XZND7kO+GUUW6cBuO/4gLun5PlhhP/mTs2jRL4JdjgU= -github.com/vicanso/cod v0.1.4/go.mod h1:T5GOazXuYrwwE1qMA0G3zka7NVwwkoL2fYIWNfeEpJw= -github.com/vicanso/hes v0.1.4 h1:n8kG8krvNJF4Sj1PvZOEUzdUsmDSbCcGr8C1nYnoP+o= +github.com/vicanso/cod v0.1.5 h1:KqdTAo1TzUa+iYbdzg1INEAZCG4pvxHkwmwKac5RLz8= +github.com/vicanso/cod v0.1.5/go.mod h1:T5GOazXuYrwwE1qMA0G3zka7NVwwkoL2fYIWNfeEpJw= github.com/vicanso/hes v0.1.4/go.mod h1:bG0UJ3EihDKObFcLLwJYjxHHr9saFllsFcepyDIvFlo= +github.com/vicanso/hes v0.2.1 h1:jRFEADmiQ30koVY/sKwlkhyXM5B3QbVVizLqrjNJgPw= +github.com/vicanso/hes v0.2.1/go.mod h1:QcxOFmFfBQMhASTaLgnFayXYCgevdSeBVprt+o+3eKo= github.com/vicanso/keygrip v0.1.0 h1:/zYzoVIbREAvaxSM7bo3/oSXuuYztaP71dPBfhRoNkM= github.com/vicanso/keygrip v0.1.0/go.mod h1:cI05iOjY00NJ7oH2Z9Zdm9eJPUkpoex3XnEubK78nho= diff --git a/router_concurrent_limiter.go b/router_concurrent_limiter.go index b80e3f5..ad09baa 100644 --- a/router_concurrent_limiter.go +++ b/router_concurrent_limiter.go @@ -71,8 +71,8 @@ func NewLocalLimiter(data map[string]uint32) *LocalLimiter { } // IncConcurrency concurrency inc -func (l *LocalLimiter) IncConcurrency(route string) (current, max uint32) { - concur, ok := l.m[route] +func (l *LocalLimiter) IncConcurrency(key string) (current, max uint32) { + concur, ok := l.m[key] if !ok { return 0, 0 } @@ -81,8 +81,8 @@ func (l *LocalLimiter) IncConcurrency(route string) (current, max uint32) { } // DecConcurrency concurrency dec -func (l *LocalLimiter) DecConcurrency(route string) { - concur, ok := l.m[route] +func (l *LocalLimiter) DecConcurrency(key string) { + concur, ok := l.m[key] if !ok { return } @@ -90,8 +90,8 @@ func (l *LocalLimiter) DecConcurrency(route string) { } // GetConcurrency get concurrency -func (l *LocalLimiter) GetConcurrency(route string) uint32 { - concur, ok := l.m[route] +func (l *LocalLimiter) GetConcurrency(key string) uint32 { + concur, ok := l.m[key] if !ok { return 0 } @@ -119,9 +119,9 @@ func New(config Config) cod.Handler { if skipper(c) { return c.Next() } - route := c.Route - current, max := limiter.IncConcurrency(route) - defer limiter.DecConcurrency(route) + key := c.Request.Method + " " + c.Route + current, max := limiter.IncConcurrency(key) + defer limiter.DecConcurrency(key) if current > max { err = createError(current, max) return diff --git a/router_concurrent_limiter_test.go b/router_concurrent_limiter_test.go index c0be1d2..936ca0c 100644 --- a/router_concurrent_limiter_test.go +++ b/router_concurrent_limiter_test.go @@ -16,6 +16,7 @@ package routerconcurrentlimiter import ( "fmt" + "net/http/httptest" "os" "sync/atomic" "testing" @@ -62,15 +63,16 @@ func TestNoLimiterPanic(t *testing.T) { func TestRouterConcurrentLimiter(t *testing.T) { limiter := NewLocalLimiter(map[string]uint32{ - "/users/login": 1, - "/books/:id": 100, + "POST /users/login": 1, + "GET /books/:id": 100, }) fn := New(Config{ Limiter: limiter, }) t.Run("skip", func(t *testing.T) { assert := assert.New(t) - c := cod.NewContext(nil, nil) + req := httptest.NewRequest("GET", "/", nil) + c := cod.NewContext(nil, req) c.Committed = true done := false c.Next = func() error { @@ -84,7 +86,8 @@ func TestRouterConcurrentLimiter(t *testing.T) { t.Run("below limit", func(t *testing.T) { assert := assert.New(t) - c := cod.NewContext(nil, nil) + req := httptest.NewRequest("GET", "/books/1", nil) + c := cod.NewContext(nil, req) c.Route = "/books/:id" var count int32 max := 10 @@ -102,7 +105,8 @@ func TestRouterConcurrentLimiter(t *testing.T) { t.Run("higher than limit", func(t *testing.T) { assert := assert.New(t) - c := cod.NewContext(nil, nil) + req := httptest.NewRequest("POST", "/users/login", nil) + c := cod.NewContext(nil, req) c.Route = "/users/login" c.Next = func() error { time.Sleep(10 * time.Millisecond)