Rate limiter that doesn't delay first message

I'm looking for a different rate limiter and wonder if anyone has already built it.

The default rate limiter seems to delay the first message by the limit rate. I want the first message after deployment to be sent, and then rate limit subsequent messages.

What I want is one that works like this when a message is received:

  • If no message has been sent, send the message and log the time as last_time;
  • If a message has previously been sent, and (current_time - last_time) < delay, drop the message;
  • If a message has previously been sent, and (current_time - last_time) >= delay, send the message and log the time as last_time

The key difference here is that the first message isn't delayed at all.

Has anyone done this? I can think of ways to do it, but if someone else has a tried-and-tested node for this I'd rather use that.

Thanks!

What sort of deploy are you doing? If you have it set to deploy only modified nodes, and the delay node has not been changed, then it will carry on as before, so may delay the next message.

Does it delay the first message if you use Restart Flows from the drop-down on the deploy button?

Does it delay if you restart node red?

For me it works as expected, not holding up the first message. Import this flow, deploy, and click the inject button. It should pass it straight through.

[{"id":"5cbfd15707b8c5ca","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":350,"y":2680,"wires":[["2d4804521563daec"]]},{"id":"2d4804521563daec","type":"delay","z":"bdd7be38.d3b55","name":"rate limit 1 per 30 seconds","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"30","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":570,"y":2680,"wires":[["7bc3029eb563337f"]]},{"id":"7bc3029eb563337f","type":"debug","z":"bdd7be38.d3b55","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":780,"y":2680,"wires":[]}]
1 Like