RATELIMIT Library
The RATELIMIT library provides the ability to perform rate-limiting on processes within the Translator. This is useful when integrating with API systems that have strict limits on HTTP requests. It provides the ability to:
-
Specify custom rate limits
-
Specify different rate limits for a peak period and a non-peak period
-
Automatically throttle messages once it detects the message throughput will exceed the specified limits
The library works by caching message/event timestamps in a SQLite database and tracking the number of messages/events over the given rate period. If needed, you can access this database via the getLogs() function to gain insight into the current state of the event tracking.
To implement the rate-limiting process using the library, follow the below steps:
-
Initialize the library with your desired rate limits and peak period details using RATELIMITinit
-
Implement a check() in the function that performs the API call or process that needs to be rate-limited
-
Perform an addLog() after calling the API or completing the process that needs to be tracked
Add the library to the Translator project using +LIBRARY and try the below code snippet:
-- Set up the rate limiter
local R = RATELIMITinit{
-- Peak period runs Mon-Fri
peak_days_start = 2, peak_days_end = 6,
-- Peak hours run from 6am-6pm (server time)
peak_time_start = 6, peak_time_end = 18,
-- During peak hours, rates are limited at 15 msg/min or 15 msg/60 s
-- During regular hours, rates are limited at 120 msg/min or 120 msg/60 s
peak_max_msg = 15, reg_max_msg = 120,
msg_period = 60
}
-- Check if rates have exceeded the specified parameters and delay accordingly
R:check()
-- After performing the API call, log the call
R:addLog()