Commit 01f6ba39 authored by Aaron Raddon's avatar Aaron Raddon

change throttler to use time.Duration instead of secs/int

parent de978e93
......@@ -7,6 +7,7 @@ import (
"runtime"
"strings"
"sync"
"time"
)
const (
......@@ -237,7 +238,7 @@ func LogThrottleKey(logLvl, limit int, key, format string, v ...interface{}) {
throttleMu.Lock()
th, ok := logThrottles[key]
if !ok {
th = NewThrottler(limit, 3600)
th = NewThrottler(limit, 3600*time.Second)
logThrottles[key] = th
}
if th.Throttle() {
......@@ -259,7 +260,7 @@ func LogThrottle(logLvl, limit int, format string, v ...interface{}) {
throttleMu.Lock()
th, ok := logThrottles[format]
if !ok {
th = NewThrottler(limit, 3600)
th = NewThrottler(limit, 3600*time.Second)
logThrottles[format] = th
}
if th.Throttle() {
......
......@@ -20,12 +20,12 @@ type Throttler struct {
// new Throttler that will tell you to limit or not based
// on given max events per second input @limit
func NewThrottler(maxPerSecond, per int) *Throttler {
func NewThrottler(max int, per time.Duration) *Throttler {
return &Throttler{
maxPer: float64(maxPerSecond),
allowance: float64(maxPerSecond),
maxPer: float64(max),
allowance: float64(max),
last: time.Now(),
per: float64(per),
per: per.Seconds(),
}
}
......
......@@ -8,13 +8,13 @@ import (
)
func TestThrottleer(t *testing.T) {
th := NewThrottler(10, 10)
th := NewThrottler(10, 10*time.Second)
for i := 0; i < 10; i++ {
assert.Tf(t, th.Throttle() == false, "Should not throttle %v", i)
time.Sleep(time.Millisecond * 10)
}
throttled := 0
th = NewThrottler(10, 1)
th = NewThrottler(10, 1*time.Second)
// We are going to loop 20 times, first 10 should make it, next 10 throttled
for i := 0; i < 20; i++ {
LogThrottleKey(WARN, 10, "throttle", "hello %v", i)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment