net.dns: Ensure all pending requests get notified of a timeout when looking up a...
[prosody.git] / util / throttle.lua
index 8b62e7974f3eb74ddd391ef1309b5a9de4efdbd5..55e1d07b3f5a836b450fc65cf23a1c31b7363e49 100644 (file)
@@ -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