MUC: Send participant list and subject on explicit joins (thanks daurnimator)
[prosody.git] / util / throttle.lua
index 82b0a67ba57903a8d184279b1afb2f10852d86c7..3d3f5d2d8b029f9134de0276620411b06390abac 100644 (file)
@@ -1,8 +1,9 @@
 
 local gettime = require "socket".gettime;
 local setmetatable = setmetatable;
+local floor = math.floor;
 
-module "throttle"
+local _ENV = nil;
 
 local throttle = {};
 local throttle_mt = { __index = 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,12 +35,14 @@ 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
 
-function create(max, period)
+local function create(max, period)
        return setmetatable({ rate = max / period, max = max, t = 0, balance = max }, throttle_mt);
 end
 
-return _M;
+return {
+       create = create;
+};