# Logic in nodered function to lock or release

**URL:** https://discourse.nodered.org/t/logic-in-nodered-function-to-lock-or-release/84431
**Category:** General
**Tags:** function-node
**Created:** [9 January 2024 18:06 UTC](https://discourse.nodered.org/t/logic-in-nodered-function-to-lock-or-release/84431 "2024-01-09T18:06:51Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![zygfrydos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zygfrydos/32/87176_2.png) [@zygfrydos](https://discourse.nodered.org/u/zygfrydos)
#### Post date: [9 January 2024 18:06 UTC](https://discourse.nodered.org/t/logic-in-nodered-function-to-lock-or-release/84431/1 "2024-01-09T18:06:51Z")

</div>

In my code I have 10 place when I make http request to 10 diffrent resource on the same server. But http server of that resource allow only 1/request per client per 3 seconds. How to make this on node-red?. I prefer logic in javascript function block.

My first solution was to create in function while loop and in that loop check global variable (e.g myLock). If myLock is false then set it to true, make request, wait 3 second, set myLock to false and return value.  
This solution is not elegant but I'm thing that will work. But I dont now is context variable is safe in this situation ? What you think about this?

Best regards

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [9 January 2024 18:28 UTC](https://discourse.nodered.org/t/logic-in-nodered-function-to-lock-or-release/84431/2 "2024-01-09T18:28:29Z")

</div>

Welcome to the forum @zygfrydos

Feed the requests through a Delay node set to Rate Limit mode set to 3 messages per second or less.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [9 January 2024 18:42 UTC](https://discourse.nodered.org/t/logic-in-nodered-function-to-lock-or-release/84431/3 "2024-01-09T18:42:02Z")

</div>

> [@zygfrydos](#):
>
> My first solution was to create in function while loop and in that loop check global variable (e.g myLock)

That would block the nodejs event loop. (nodeJS and therefore Node-RED is single threaded, event loop driven) In short, looping i a function is bad (and anti-pattern)

> [@zygfrydos](#):
>
> In my code I have 10 place when I make http request to 10 diffrent resource on the same server. But http server of that resource allow only 1/request per client per 3 seconds. How to make this on node-red?. I prefer logic in javascript function block.

function block is not ideal for this.

A better solution:

Create a subroutine consisting of `link-in` -\> `delay` node (rate limit mode) -\> `http request` -\> `link-out` (set to return mode)

then, whenever you need to call to HTTP, use a `link-call` node. It will add your request to the queue and return to you when it is done.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/3/f300166b1c09ece0361657f93c8f8aee03522845.png)

---

<div class="post-metadata">

### Author: ![zygfrydos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zygfrydos/32/87176_2.png) [@zygfrydos](https://discourse.nodered.org/u/zygfrydos)
#### Post date: [9 January 2024 18:57 UTC](https://discourse.nodered.org/t/logic-in-nodered-function-to-lock-or-release/84431/4 "2024-01-09T18:57:17Z")

</div>

Thank you Steve-Mcl.  
This is good solution for now and solving my problem.  
However, if I wanted in future write this in my own node in javascript. How to safe semaphore/variable and execute some code if this variable change?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [9 January 2024 19:00 UTC](https://discourse.nodered.org/t/logic-in-nodered-function-to-lock-or-release/84431/5 "2024-01-09T19:00:46Z")

</div>

> [@zygfrydos](#):
>
> However, if I wanted in future write this in my own node in javascript

Do you mean as a node package (like node-red-contrib-my-super-duper-node) - OR - as a function node in the editor?

---

<div class="post-metadata">

### Author: ![zygfrydos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zygfrydos/32/87176_2.png) [@zygfrydos](https://discourse.nodered.org/u/zygfrydos)
#### Post date: [9 January 2024 19:04 UTC](https://discourse.nodered.org/t/logic-in-nodered-function-to-lock-or-release/84431/6 "2024-01-09T19:04:04Z")

</div>

I mean package. I now that function node is in sandbox and it's slow but safe 🙂

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [23 January 2024 19:04 UTC](https://discourse.nodered.org/t/logic-in-nodered-function-to-lock-or-release/84431/7 "2024-01-23T19:04:52Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
