X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Fthrottle.lua;h=3d3f5d2d8b029f9134de0276620411b06390abac;hb=3cf900623131e50db77784774edfdf5ba0255692;hp=2e901158096e0cc4302b2d630e8df7968186310c;hpb=cb2c1215a9a5df6c7e458dd788193aa6b41169c7;p=prosody.git diff --git a/util/throttle.lua b/util/throttle.lua index 2e901158..3d3f5d2d 100644 --- a/util/throttle.lua +++ b/util/throttle.lua @@ -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; +};