diff --git a/packages/node_modules/@node-red/nodes/core/function/89-delay.js b/packages/node_modules/@node-red/nodes/core/function/89-delay.js index 071379ff4..f80e58365 100644 --- a/packages/node_modules/@node-red/nodes/core/function/89-delay.js +++ b/packages/node_modules/@node-red/nodes/core/function/89-delay.js @@ -20,6 +20,20 @@ module.exports = function(RED) { var MILLIS_TO_NANOS = 1000000; var SECONDS_TO_NANOS = 1000000000; + var _maxKeptMsgsCount; + + function maxKeptMsgsCount(node) { + if (_maxKeptMsgsCount === undefined) { + var name = "nodeMessageBufferMaxLength"; + if (RED.settings.hasOwnProperty(name)) { + _maxKeptMsgsCount = RED.settings[name]; + } + else { + _maxKeptMsgsCount = 0; + } + } + return _maxKeptMsgsCount; + } function DelayNode(n) { RED.nodes.createNode(this,n); @@ -172,8 +186,14 @@ module.exports = function(RED) { node.intervalID = setInterval(sendMsgFromBuffer, node.rate); } if (!msg.hasOwnProperty("flush")) { - node.buffer.push(msg); - node.reportDepth(); + var max_msgs = maxKeptMsgsCount(node); + if ((max_msgs > 0) && (node.buffer.length >= max_msgs)) { + node.buffer = []; + node.error(RED._("delay.errors.too-many"), msg); + } else { + node.buffer.push(msg); + node.reportDepth(); + } } } else { @@ -186,20 +206,36 @@ module.exports = function(RED) { } } else { - if (msg.hasOwnProperty("rate")) { - node.rate = msg.rate; - } - var timeSinceLast; - if (node.lastSent) { - timeSinceLast = process.hrtime(node.lastSent); - } - if (!node.lastSent) { // ensuring that we always send the first message - node.lastSent = process.hrtime(); - node.send(msg); - } - else if ( ( (timeSinceLast[0] * SECONDS_TO_NANOS) + timeSinceLast[1] ) > (node.rate * MILLIS_TO_NANOS) ) { - node.lastSent = process.hrtime(); - node.send(msg); + if (maxKeptMsgsCount(node) > 0) { + if (node.intervalID === -1) { + node.send(msg); + node.intervalID = setInterval(sendMsgFromBuffer, node.rate); + } else { + if (msg.hasOwnProperty("rate") && node.rate !== msg.rate) { + node.rate = msg.rate; + clearInterval(node.intervalID); + node.intervalID = setInterval(sendMsgFromBuffer, node.rate); + } + if (node.buffer.length < _maxKeptMsgsCount) { + node.buffer.push(msg); + } + } + } else { + if (msg.hasOwnProperty("rate")) { + node.rate = msg.rate; + } + var timeSinceLast; + if (node.lastSent) { + timeSinceLast = process.hrtime(node.lastSent); + } + if (!node.lastSent) { // ensuring that we always send the first message + node.lastSent = process.hrtime(); + node.send(msg); + } + else if ( ( (timeSinceLast[0] * SECONDS_TO_NANOS) + timeSinceLast[1] ) > (node.rate * MILLIS_TO_NANOS) ) { + node.lastSent = process.hrtime(); + node.send(msg); + } } } if (msg.hasOwnProperty("reset")) { diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json index b69272078..58c334f08 100755 --- a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json @@ -280,6 +280,9 @@ "singular": "Day" } } + }, + "errors": { + "too-many" : "too many pending messages in delay node" } }, "trigger": { diff --git a/packages/node_modules/@node-red/nodes/locales/ja/messages.json b/packages/node_modules/@node-red/nodes/locales/ja/messages.json index 93f23f36f..1621e1da5 100755 --- a/packages/node_modules/@node-red/nodes/locales/ja/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/ja/messages.json @@ -280,6 +280,9 @@ "singular": "日" } } + }, + "errors": { + "too-many": "delayノード内で保持しているメッセージが多すぎます" } }, "trigger": {