Mercurial > prosody-hg
comparison util/throttle.lua @ 14078:d649d011df45
Use util.time.now() consistently instead of socket.gettime()
| author | Link Mauve <linkmauve@linkmauve.fr> |
|---|---|
| date | Fri, 30 Jan 2026 14:08:53 +0100 |
| parents | d10957394a3c |
| children |
comparison
equal
deleted
inserted
replaced
| 14077:c83aee2a0b11 | 14078:d649d011df45 |
|---|---|
| 1 | 1 |
| 2 local gettime = require "prosody.util.time".now | 2 local time_now = require "prosody.util.time".now; |
| 3 local setmetatable = setmetatable; | 3 local setmetatable = setmetatable; |
| 4 | 4 |
| 5 local _ENV = nil; | 5 local _ENV = nil; |
| 6 -- luacheck: std none | 6 -- luacheck: std none |
| 7 | 7 |
| 8 local throttle = {}; | 8 local throttle = {}; |
| 9 local throttle_mt = { __index = throttle }; | 9 local throttle_mt = { __index = throttle }; |
| 10 | 10 |
| 11 function throttle:update() | 11 function throttle:update() |
| 12 local newt = gettime(); | 12 local newt = time_now(); |
| 13 local elapsed = newt - self.t; | 13 local elapsed = newt - self.t; |
| 14 self.t = newt; | 14 self.t = newt; |
| 15 local balance = (self.rate * elapsed) + self.balance; | 15 local balance = (self.rate * elapsed) + self.balance; |
| 16 if balance > self.max then | 16 if balance > self.max then |
| 17 self.balance = self.max; | 17 self.balance = self.max; |
| 38 return false, balance, (cost-balance); | 38 return false, balance, (cost-balance); |
| 39 end | 39 end |
| 40 end | 40 end |
| 41 | 41 |
| 42 local function create(max, period) | 42 local function create(max, period) |
| 43 return setmetatable({ rate = max / period, max = max, t = gettime(), balance = max }, throttle_mt); | 43 return setmetatable({ rate = max / period, max = max, t = time_now(), balance = max }, throttle_mt); |
| 44 end | 44 end |
| 45 | 45 |
| 46 return { | 46 return { |
| 47 create = create; | 47 create = create; |
| 48 }; | 48 }; |
