Commit 4d3eeed9 authored by Aaron Raddon's avatar Aaron Raddon

less type conversion on throttling

parent 7d3d6746
......@@ -6,8 +6,9 @@ import (
type Throttler struct {
// Limit to this events/second
maxPerSec int
// Limit to this events/per
maxPer float64
per float64
// Last Event
last time.Time
......@@ -15,15 +16,13 @@ type Throttler struct {
// How many events are allowed left to happen?
// Starts at limit, decrements down
allowance float64
per float64
}
// 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 {
return &Throttler{
maxPerSec: maxPerSecond,
maxPer: float64(maxPerSecond),
allowance: float64(maxPerSecond),
last: time.Now(),
per: float64(per),
......@@ -33,20 +32,19 @@ func NewThrottler(maxPerSecond, per int) *Throttler {
// Should we limit this because we are above rate?
func (r *Throttler) Throttle() bool {
if r.maxPerSec == 0 {
if r.maxPer == 0 {
return false
}
// http://stackoverflow.com/questions/667508/whats-a-good-rate-limiting-algorithm
rate := float64(r.maxPerSec)
now := time.Now()
elapsed := float64(now.Sub(r.last).Nanoseconds()) / 1e9 // nano Seconds
elapsed := float64(now.Sub(r.last).Nanoseconds()) / 1e9 // seconds
r.last = now
r.allowance += elapsed * (rate / r.per)
r.allowance += elapsed * (r.maxPer / r.per)
//Infof("maxRate: %v cur: %v elapsed:%-6.6f incr: %v", r.maxPerSec, int(r.allowance), elapsed, elapsed*float64(r.maxPerSec))
if r.allowance > rate {
r.allowance = float64(r.maxPerSec)
//Infof("maxRate: %v cur: %v elapsed:%-6.6f incr: %v", r.maxPer, int(r.allowance), elapsed, elapsed*float64(r.maxPer))
if r.allowance > r.maxPer {
r.allowance = r.maxPer
}
if r.allowance <= 1.0 {
......@@ -56,56 +54,3 @@ func (r *Throttler) Throttle() bool {
r.allowance -= 1.0
return false // dont throttle
}
/*
type Ratelimiter struct {
rate int // conn/sec
last time.Time // last time we were polled/asked
allowance float64
}
// Create new rate limiter that limits at rate/sec
func NewRateLimiter(rate int) (*Ratelimiter, error) {
r := Ratelimiter{rate:rate, last:time.Now()}
r.allowance = float64(r.rate)
return &r, nil
}
// Return true if the current call exceeds the set rate, false
// otherwise
func (r* Ratelimiter) Limit() bool {
// handle cases where rate in config file is unset - defaulting
// to "0" (unlimited)
if r.rate == 0 {
return false
}
rate := float64(r.rate)
now := time.Now()
elapsed := now.Sub(r.last)
r.last = now
r.allowance += float64(elapsed) * rate
// Clamp number of tokens in the bucket. Don't let it get
// unboundedly large
if r.allowance > rate {
r.allowance = rate
}
var ret bool
if r.allowance < 1.0 {
ret = true
} else {
r.allowance -= 1.0
ret = false
}
return ret
}
*/
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