X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Fthrottle.lua;h=55e1d07b3f5a836b450fc65cf23a1c31b7363e49;hb=84c934632367eac822fb6af2663032b67fd0a596;hp=8b62e7974f3eb74ddd391ef1309b5a9de4efdbd5;hpb=910be2624084ea2025837e30394a0df84bb65444;p=prosody.git diff --git a/util/throttle.lua b/util/throttle.lua index 8b62e797..55e1d07b 100644 --- a/util/throttle.lua +++ b/util/throttle.lua @@ -1,5 +1,7 @@ local gettime = require "socket".gettime; +local setmetatable = setmetatable; +local floor = math.floor; module "throttle" @@ -10,7 +12,7 @@ function throttle:update() local newt = gettime(); local elapsed = newt - self.t; self.t = newt; - local balance = self.rate * elapsed + self.balance; + local balance = floor(self.rate * elapsed) + self.balance; if balance > self.max then self.balance = self.max; else @@ -33,7 +35,7 @@ function throttle:poll(cost, split) if split then self.balance = 0; end - return false, balance, (cost-self.balance); + return false, balance, (cost-balance); end end