This channel shows how we can slow down an interface using a simple helper module that throttles a function by adding a delay according to a specified schedule.
This can be helpful if you have a production scenario where one interface is sending too many transactions such that it is taking resources away from other feeds.
An example of when this might happen could be if a customer is replaying 6 months worth of patient demographic data – you want to process the data but not so fast that it slows performance down for all the other customers.
Using the Code [top]
- Import the Throttle Transaction Volumechannel from the Builtin: Iguana Tools repository
- Experiment with the code to find out how it works
- Then add the module to your Translator project
- Copy the require statement from the channel and add it at the top of your script
Note: This module uses require to return a table
- Adapt the code to your own requirements
- Use the
throttle.run()function to throttle a function
throttle.modifySchedule()to change the throttling schedule times and delays
Note: The two examples in the code can be used as templates to create your own schedule
- Interactive scripting help is included for this module
This is the github code for the main module:
How it works [top]
The module only has two functions: The
throttle.run() function, and the
throttle.modifySchedule() function. The
throttle.run() function is used to throttle a function, by adding a delay when it is called. The
throttle.modifySchedule() is use to change the scheduled throttling times and the delays.
The throttling schedule and delays are stored in a Lua table, this is the default schedule from the module:
The sample code itself is fairly simple, the first two calls demonstrate the
throttle.run() function, first with a single parameter then with multiple parameters. The next two calls show how to change the throttling schedule using
throttle.modifySchedule(), by passing in a new “schedule table” (that replaces the current schedule). The first example shows adds throttling on Saturday as well, the second shows how to add multiple throttling periods on each day and how to use different delays.
At first glance
throttle.modifySchedule() might look a little complex, but we are simply passing in a new schedule using the standard Lua table format. Also the examples are intended as templates that you can modify to create your own schedule.