Throttling during peak hours

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.

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: Controlling database inserts and updates.

How It Works

The process behind the throttle module is quite simple:

  1. It establishes the current weekday and time.
  2. It checks to see if this within your specified “peak time”.
  3. 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.

Note: The 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

See Also

Please contact support at support@interfaceware.com if you need more help.