diff --git a/limiter.go b/limiter.go index e842748..b8aa108 100644 --- a/limiter.go +++ b/limiter.go @@ -3,10 +3,10 @@ package main import ( "fmt" "io" + "math/rand" "strconv" "strings" "time" - "math/rand" ) type Limiter struct { @@ -40,21 +40,21 @@ func NewLimiter(plugin interface{}, options string) io.ReadWriter { } func (l *Limiter) isLimited() bool { - if (l.isPercent) { + if l.isPercent { return l.limit <= rand.Intn(100) } if (time.Now().UnixNano() - l.currentTime) > time.Second.Nanoseconds() { l.currentTime = time.Now().UnixNano() l.currentRPS = 0 - } + } if l.currentRPS >= l.limit { return true } l.currentRPS++ - + return false } diff --git a/limiter_test.go b/limiter_test.go index 8946ea5..ee56cc9 100644 --- a/limiter_test.go +++ b/limiter_test.go @@ -54,7 +54,6 @@ func TestInputLimiter(t *testing.T) { close(quit) } - // Should limit all requests func TestPercentLimiter1(t *testing.T) { wg := new(sync.WaitGroup) @@ -102,4 +101,4 @@ func TestPercentLimiter2(t *testing.T) { wg.Wait() close(quit) -} \ No newline at end of file +}