Tick assumes to be called every r.interval.
()
| 35 | |
| 36 | // Tick assumes to be called every r.interval. |
| 37 | func (r *EwmaRate) Tick() { |
| 38 | newEvents := r.newEvents.Swap(0) |
| 39 | instantRate := float64(newEvents) / r.interval.Seconds() |
| 40 | |
| 41 | r.mutex.Lock() |
| 42 | defer r.mutex.Unlock() |
| 43 | |
| 44 | if r.init { |
| 45 | r.lastRate += r.alpha * (instantRate - r.lastRate) |
| 46 | } else { |
| 47 | r.init = true |
| 48 | r.lastRate = instantRate |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Inc counts one event. |
| 53 | func (r *EwmaRate) Inc() { |