mod_http_files: Switch to use util.cache for cache
[prosody.git] / util / throttle.lua
index 2e901158096e0cc4302b2d630e8df7968186310c..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
@@ -38,8 +39,10 @@ function throttle:poll(cost, split)
        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;
+};