Throttling can be useful if you don’t want to hit a resource with high loads during peak hours. In this article, we’ll show you a simple module that you can use to delay a process/function based on the time of day.
If you have any questions please contact us at email@example.com.
Note: This code can be adapted to throttle any resource. However, if you want to throttle output to a database, we strongly recommend using a slightly altered version of this module that we’ve designed specifically for this task. This module (and its accompanying copy-and-paste code) can be found here: Throttlling database inserts and updates.
How It Works
The process behind the
throttle module is quite simple:
- It establishes the current weekday and time.
- It 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.
You can get the throttle.lua module from our code repository.
The module assumes that peak hours will be on weekdays between 10:30am to 3:30pm. You can modify the code to match your own particular situation. You can also change the throttling criteria to include multiple peak times, different times on different days, or even create a throttle based on load/time.
util.sleep() function does not take up any extra CPU cycles! It simply pauses the thread that the call is applied to (and nothing else).
Once you have the module set up, applying a throttle is easy. 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