A simple node that offers rate limiting based on a sliding window.
It ensures a strict limit on rate of messages, provides the set rate very precise (if queueing is used), and performes well even under very high load.
A classic application could be the input protection of a flow that receives messages from the outside world, e.g. web requests or commands via MQTT. If the message rate is normal, messages are forwarded without any delay; but if there is too high volume, the messages are limited.
Rate limiting based on a sliding window means that for any specified time interval no more than the specified number of messages will pass the rate-limiter node. Rate-limiter node will not delay any messages if the rate limit is not reached.
That means even bursts of messages are allowed to pass within the given limit, while the passage of constant high message rates can be avoided. This is unlike the default delay-node (in rate limit mode), which will put a fixed time between each message even before the set rate limit is reached.
The User can specify a maximum number of messages (n) for a time period (t).
For every incoming message the node checks if the number of messages sent in the last time frame t is below n; if yes, the message will be sent to output 1; if no, the message will be dropped, sent to output 2, or queued. Depending on the settings. If messages are queued, they will be release on output 1 whenever they will not exceed the set rate limit.
Rate-limiter node can operate in 2 rate-limiting modes:
- Queue intermediate messages
- Drop intermediate messages
Modes can be selected via dropdown menu in the node's editor.
In drop mode, rate-limiter will drop all messages above the limit. They will never reach the output 1.
There is a checkbox that allows sending those dropped messages to output 2.
Let's assume this setting:
The animation shows messages that are allowed to pass (green) and dropped messages (red) when the rate limit is reached.
In queueing mode, rate-limiter allows to buffer messages that could not be sent immediately because they would have violated the rate limit. The messages will be sent as soon as they will not violate the rate limit.
Additionally, the user can set a maximum queue size. If the number of queued messages reaches the limit further messages can (depending on the setting) either be dropped or added to the queue while the oldest messages in the queue will be dropped.
For example let's assume this setting:
- The rate is set to 5 msg(s) per 10 seconds. --> Means if there is equal or less than 5 messages received in 10 seconds, they are sent to output 1.
- Queue intermediate messages --> All messages above the limit of 5 messages will be buffered for later sending and...
- Queue Max Size equals 0 which means with no limit --> No message will be dropped.
If the node receives for example 6 messages within 10 seconds, only 5 are sent immediately, and the 6th message will be sent exactly 10 seconds after the first message, because then there were only 4 messages sent in the past 10 seconds, hence the 6th can be sent before the limit is reached again.
The node allows to control some of the settings dynamically. To enable the function a dedicated topic must be given in settings dialog.
If control topic is enabled, following commands are available:
- reset resets the node's sent messages, delets the queue, all dynamically changed settings will be reverted to configured settings
- flush empties the whole queue at once
- flushreset executes a flush followed by a reset in one go
- Attributes rate, time, and queue can be changed by message. Sending a JSON object in
msg.payload
allows the parameters to change dynamically; until redeployment or reset.- rate defines the allowed messages per timeframe and must be an integer > 0
- time defines the timeframe in milliseconds
- queue defines the queue max size; 0 means no limit; if the new size of the queue is smaller than the number of already queued messages, the excess is dropped under normal rules (i.e. oldest or newest messages are dropped or flushed to 2nd exit)
Message {"topic":"control", "payload":"reset"}
will reset the rate-limiter node.
This message will change the allowed rate to 1234 msg/17s with queue max size = 10000.
{
"topic":"control",
"payload":{
"rate":1234,
"time":17000,
"queue":10000
}
}
Nodered.org https://flows.nodered.org/node/@cameo69/node-red-ratelimit
npm https://www.npmjs.com/package/@cameo69/node-red-ratelimit
GitHub https://github.com/cameo69/node-red-contrib-ratelimit