1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add push to front of rate limit queue.

(moved random delay to top to group with other delay types.
Tests and docs to follow
This commit is contained in:
Dave Conway-Jones 2021-07-15 22:15:46 +01:00
parent 66a704af55
commit d820f55358
No known key found for this signature in database
GPG Key ID: 88BA2B8A411BE9FF

View File

@ -154,6 +154,7 @@ module.exports = function(RED) {
}, 15 * 1000); }, 15 * 1000);
node.on("close", function() { clearInterval(loggerId); }); node.on("close", function() { clearInterval(loggerId); });
// The delay type modes
if (node.pauseType === "delay") { if (node.pauseType === "delay") {
node.on("input", function(msg, send, done) { node.on("input", function(msg, send, done) {
var id = ourTimeout(function() { var id = ourTimeout(function() {
@ -199,6 +200,29 @@ module.exports = function(RED) {
}); });
node.on("close", function() { clearDelayList(); }); node.on("close", function() { clearDelayList(); });
} }
else if (node.pauseType === "random") {
node.on("input", function(msg, send, done) {
var wait = node.randomFirst + (node.diff * Math.random());
var id = ourTimeout(function() {
node.idList.splice(node.idList.indexOf(id),1);
send(msg);
if (node.timeout >= 1000) {
node.status({fill:"blue",shape:"dot",text:node.idList.length});
}
done();
}, wait, () => done());
if (Object.keys(msg).length === 2 && msg.hasOwnProperty("flush")) { id.clear(); }
else { node.idList.push(id); }
if (msg.hasOwnProperty("reset")) { clearDelayList(true); }
if (msg.hasOwnProperty("flush")) { flushDelayList(msg.flush); done(); }
if (node.timeout >= 1000) {
node.status({fill:"blue",shape:"dot",text:node.idList.length});
}
});
node.on("close", function() { clearDelayList(); });
}
// The rate limit/queue type modes
else if (node.pauseType === "rate") { else if (node.pauseType === "rate") {
node.on("input", function(msg, send, done) { node.on("input", function(msg, send, done) {
if (msg.hasOwnProperty("reset")) { if (msg.hasOwnProperty("reset")) {
@ -217,6 +241,7 @@ module.exports = function(RED) {
if (!node.drop) { if (!node.drop) {
var m = RED.util.cloneMessage(msg); var m = RED.util.cloneMessage(msg);
delete m.flush; delete m.flush;
delete m.lifo;
if (Object.keys(m).length > 1) { if (Object.keys(m).length > 1) {
if (node.intervalID !== -1) { if (node.intervalID !== -1) {
if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate)) && node.rate !== msg.rate) { if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate)) && node.rate !== msg.rate) {
@ -228,6 +253,9 @@ module.exports = function(RED) {
if ((max_msgs > 0) && (node.buffer.length >= max_msgs)) { if ((max_msgs > 0) && (node.buffer.length >= max_msgs)) {
node.buffer = []; node.buffer = [];
node.error(RED._("delay.errors.too-many"), msg); node.error(RED._("delay.errors.too-many"), msg);
} else if (msg.lifo) {
node.buffer.unshift({msg: m, send: send, done: done});
node.reportDepth();
} else { } else {
node.buffer.push({msg: m, send: send, done: done}); node.buffer.push({msg: m, send: send, done: done});
node.reportDepth(); node.reportDepth();
@ -385,27 +413,6 @@ module.exports = function(RED) {
node.status({}); node.status({});
}); });
} }
else if (node.pauseType === "random") {
node.on("input", function(msg, send, done) {
var wait = node.randomFirst + (node.diff * Math.random());
var id = ourTimeout(function() {
node.idList.splice(node.idList.indexOf(id),1);
send(msg);
if (node.timeout >= 1000) {
node.status({fill:"blue",shape:"dot",text:node.idList.length});
}
done();
}, wait, () => done());
if (Object.keys(msg).length === 2 && msg.hasOwnProperty("flush")) { id.clear(); }
else { node.idList.push(id); }
if (msg.hasOwnProperty("reset")) { clearDelayList(true); }
if (msg.hasOwnProperty("flush")) { flushDelayList(msg.flush); done(); }
if (node.timeout >= 1000) {
node.status({fill:"blue",shape:"dot",text:node.idList.length});
}
});
node.on("close", function() { clearDelayList(); });
}
} }
RED.nodes.registerType("delay",DelayNode); RED.nodes.registerType("delay",DelayNode);
} }