the number of messages per second, minute, etc.
Because util.throttle is flexible in what it can limit, you have to decide on a 'cost' for every action. If you are limiting based on traffic, for example, the cost of a packet of data would be the size of that packet in bytes. If you are limiting some action, you can simply use a cost of 1.
The library is an implementation of the token bucket algorithm.
This small example shows a fictional 'check_message' that ensures received messages are allowed by the throttles.
In a real life system, sometimes you will want to reject the event (e.g. responding to a message with an error), and sometimes you will want to defer/delay the event while temporarily not accepting any further input (e.g. to smooth out traffic flow, with a traffic-based throttle). The extra return values from throttle:poll() can be used to determine how long to wait until the throttle is ready to allow events again.
Creates a new throttle object. For 'max', specify the number of events or maximum total cost to allow over the given time 'period' (in seconds). Both parameters must be positive numbers, they need not be whole numbers.
Tip: Often you may want a "softer" limit, where temporarily going over the rate limit is allowed for short periods. This can be achieved by multiplying both 'max' and 'period' by the 'burst' factor. E.g. multiplying both by '2' will keep the overall average the same, but temporarily allow up to 2x the normal rate.
If it would not take the throttle over the limit, deducts 'cost' and returns true, followed by the remaining maxiumum cost in the current time period.
If the cost would take the throttle over the limit, does nothing and returns false, followed by the remaining maxiumum cost in the current time period.
This version of the 'poll' method is intended for events that can be split, for example if part of a packet can fit within the limit.
If the cost would take the throttle over the limit, and 'split' is true, deducts as much of 'cost' as would be allowed without going over the limit. Returns three results: true, the remaining balance (typically 0), and the outstanding cost (i.e. the cost that couldn't be satisfied, and should not be let through).
Tests to see whether a given cost could be allowed, without actually counting it against the rate limit. Returns true if it would be allowed, false otherwise.
Forces the throttle to update. This should not normally be called.