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

Commit

Permalink
refactor: support more limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Jul 8, 2019
1 parent 0eaa20b commit bf11658
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}),
}))
Expand Down
24 changes: 15 additions & 9 deletions router_concurrent_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,40 @@ 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{
max: max,
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
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions router_concurrent_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down Expand Up @@ -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,
})
Expand Down

0 comments on commit bf11658

Please sign in to comment.