Skip to content
This repository has been archived by the owner on Apr 14, 2020. It is now read-only.

Commit

Permalink
refactor: use method and route for router limit key
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Jul 26, 2019
1 parent bf11658 commit 76a7cca
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
18 changes: 9 additions & 9 deletions router_concurrent_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -81,17 +81,17 @@ 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
}
atomic.AddUint32(&concur.current, ^uint32(0))
}

// 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
}
Expand Down Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions router_concurrent_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package routerconcurrentlimiter

import (
"fmt"
"net/http/httptest"
"os"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 76a7cca

Please sign in to comment.