Mercurial > prosody-hg
diff util/throttle.lua @ 8245:9499db96c032
util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Sun, 17 Sep 2017 13:29:14 -0400 |
| parents | dc758422d896 |
| children | 64165865b131 |
line wrap: on
line diff
--- a/util/throttle.lua Sun Sep 17 11:17:45 2017 -0400 +++ b/util/throttle.lua Sun Sep 17 13:29:14 2017 -0400 @@ -12,7 +12,7 @@ local newt = gettime(); local elapsed = newt - self.t; self.t = newt; - local balance = floor(self.rate * elapsed) + self.balance; + local balance = (self.rate * elapsed) + self.balance; if balance > self.max then self.balance = self.max; else @@ -40,7 +40,7 @@ end local function create(max, period) - return setmetatable({ rate = max / period, max = max, t = 0, balance = max }, throttle_mt); + return setmetatable({ rate = max / period, max = max, t = gettime(), balance = max }, throttle_mt); end return {
