net.dns: Ensure all pending requests get notified of a timeout when looking up a...
[prosody.git] / util / throttle.lua
index 82b0a67ba57903a8d184279b1afb2f10852d86c7..55e1d07b3f5a836b450fc65cf23a1c31b7363e49 100644 (file)
@@ -1,6 +1,7 @@
 
 local gettime = require "socket".gettime;
 local setmetatable = setmetatable;
+local floor = math.floor;
 
 module "throttle"
 
@@ -11,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
@@ -34,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