local gettime = require "socket".gettime;
+local setmetatable = setmetatable;
+local floor = math.floor;
-module "throttle"
+local _ENV = nil;
local throttle = {};
local throttle_mt = { __index = throttle };
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
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;
+};