throttle.lua
Verified
Added by iNTERFACEWARE
Throttle a process during peak hours, by slowing down the code.
Source Code
local throttle = {} -- Establish day and time values. local function IsWeekday(Day) -- Sunday == 1, Saturday == 7 return Day ~= 1 or Day ~= 7 end local function IsPeakTime(Hours, Minutes) -- assuming peak time is 10:30 am (10:30) to 3:30 pm (15:30) return (Hours > 10 and Hours < 15) or (Hours == 10 and Minutes >= 30) or (Hours == 15 and Minutes <= 30) end -- Establish busiest time of day . -- The definition of "peak period" will be dependent on the situation. local function IsPeakPeriod() local Date = os.date("*t") local IsWeekdayValue = IsWeekday(Date.wday) -- wday range is (1-7) local IsPeakTimeValue = IsPeakTime(Date.hour, Date.min) -- hour range is (0-23) -- min range is (0-59) if IsWeekdayValue and IsPeakTimeValue then return true else return false end end -- Apply throttle to user-supplied function function throttle.run(function) -- Use sleep to throttle user-supplied function for -- 100 milliseconds, but only during peak periods. if IsPeakPeriod() then util.sleep(100) end -- Execute user-supplied function method() end return throttle
Description
Throttle a process during peak hours, by slowing down the code.
Usage Details
The throttle
module simply establishes the current weekday and time, then checks to see if this within your specified “peak time”. If it is peak time, then a specified delay is applied to throttle a specified process.
How to use throttle.lua:
- Add it to your shared modules in any Translator project
- Add l
ocal throttle = require 'throttle'
at the top of your script - Use
throttle.run()
to throttle a process
Below is a “blank” template demonstrating how to call throttle.run()
for the function/process being executed:
local throttle = require 'throttle' function main(Data) -- Throttle a specific process (replace "function" with that process) throttle.run(function) -- -- end) end