How to handle multiple http post request at once

Hi all,

I have a flow which has a http end point (Post), that process something in the middle and send response thru http response. The problem is that the client system make many calls in one shot (node.js, using Promisse.all), and my flow receive every calls, respond one of them the others crash with the message that the headers has been already used.
Question: How control this queue and process each call without error on http response?

I have no idea… but what happens if you save all the request on a buffer and release one request at time with some delay between each release in order to be able to handle the response prior to process next request?

As example if you use the rate limit x messages per x seconds will do the job.`maybe…

[{"id":"6bd57f39.c0d1c","type":"debug","z":"b7eb5457.b00758","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":524,"y":1343,"wires":[]},{"id":"a9f98f83.55d8","type":"inject","z":"b7eb5457.b00758","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":128,"y":1328,"wires":[["2527d6b0.9117ca"]]},{"id":"2527d6b0.9117ca","type":"delay","z":"b7eb5457.b00758","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":327,"y":1335,"wires":[["6bd57f39.c0d1c"]]}]

That sounds like a msg.res object is being reused.

Can you share your flow so we can see what you’re doing with the msgs between the http in and response nodes?

Yes, sure…

[{"id":"b3369f6.48b956","type":"http in","z":"89fa1dea.67dd2","name":"","url":"/classifica","method":"post","upload":false,"swaggerDoc":"","x":100,"y":60,"wires":[["bb4bf78d.633a18"]],"icon":"node-red/subflow.png"},{"id":"bb4bf78d.633a18","type":"change","z":"89fa1dea.67dd2","name":"","rules":[{"t":"set","p":"pageIndex","pt":"flow","to":"req.body.pageIndex","tot":"msg"},{"t":"set","p":"pageName","pt":"flow","to":"req.body.pageName","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"req.body.ocrData","tot":"msg"},{"t":"set","p":"resAux","pt":"flow","to":"res","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":285,"y":60,"wires":[["92a409da.19bed8","4dc50b49.d997a4"]]},{"id":"92a409da.19bed8","type":"debug","z":"89fa1dea.67dd2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":830,"y":60,"wires":[]},{"id":"46c28d10.c2daf4","type":"http response","z":"89fa1dea.67dd2","name":"","statusCode":"","headers":{},"x":845,"y":361,"wires":[],"icon":"node-red/subflow.png"},{"id":"983540d4.2676","type":"natural-language-understanding","z":"89fa1dea.67dd2","name":"","categories":false,"concepts":false,"maxconcepts":"8","doc-emotion":false,"doc-emotion-target":"","doc-sentiment":false,"doc-sentiment-target":"","entity":true,"entity-emotion":false,"entity-sentiment":false,"maxentities":"50","keyword":false,"keyword-emotion":false,"keyword-sentiment":false,"maxkeywords":"50","metadata":false,"relation":true,"semantic":false,"semantic-entities":false,"semantic-keywords":false,"maxsemantics":"50","default-endpoint":true,"service-endpoint":"https://gateway.watsonplatform.net/natural-language-understanding/api","x":600,"y":222,"wires":[["93679d2e.6353f","23ca5b8a.154c24"]]},{"id":"c89b6732.bd3f68","type":"function","z":"89fa1dea.67dd2","name":"Peticoes","func":"msg = \n{nlu_options:{entity_model:\"10:091feb70-50d5-40a1-8513-3d5b4d687c6b\",\n              relations_model:\"10:091feb70-50d5-40a1-8513-3d5b4d687c6b\"\n},\n payload:msg.payload,\n res:msg.res,\n req:msg.req\n};\nreturn msg;","outputs":1,"noerr":0,"x":269,"y":147,"wires":[["983540d4.2676"]]},{"id":"93679d2e.6353f","type":"debug","z":"89fa1dea.67dd2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":830,"y":142,"wires":[]},{"id":"4dc50b49.d997a4","type":"switch","z":"89fa1dea.67dd2","name":"Modelos","property":"pageIndex","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"},{"t":"eq","v":"3","vt":"str"},{"t":"eq","v":"4","vt":"str"},{"t":"eq","v":"5","vt":"str"},{"t":"eq","v":"7","vt":"str"},{"t":"eq","v":"6","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":8,"x":105,"y":193,"wires":[["c89b6732.bd3f68"],["e5249163.e3101"],["e5249163.e3101"],["4309848c.6ff76c"],["6218147c.3b7eac"],["64a2dc3b.916984"],["c9f3fe53.70eb1"],["61702483.62761c"]]},{"id":"d0790050.5eb09","type":"comment","z":"89fa1dea.67dd2","name":"Modelos","info":"{pageIndex: 1, pageName: 'Petição'},\n{pageIndex: 2, pageName: 'Guia'},\n{pageIndex: 3, pageName: 'Guia'},\n{pageIndex: 4, pageName: 'Comprovante'},\n{pageIndex: 5, pageName: 'Determinação Judicial'},\n{pageIndex: 6, pageName: 'Print TJ'},\n{pageIndex: 7, pageName: 'Correio'}","x":109,"y":280,"wires":[]},{"id":"e5249163.e3101","type":"function","z":"89fa1dea.67dd2","name":"Guia","func":"msg = \n{nlu_options:{entity_model:\"10:130a669d-13b5-47fa-af7b-c694e4b695a6\",\n              relations_model:\"10:130a669d-13b5-47fa-af7b-c694e4b695a6\"\n},\n payload:msg.payload,\n res:msg.res,\n req:msg.req\n};\nreturn msg;","outputs":1,"noerr":0,"x":259,"y":183,"wires":[["983540d4.2676"]]},{"id":"4309848c.6ff76c","type":"function","z":"89fa1dea.67dd2","name":"Comprovante","func":"msg = \n{nlu_options:{entity_model:\"10:d6e6adad-c89b-4f27-856b-52ef9d97eca8\",\n              relations_model:\"10:d6e6adad-c89b-4f27-856b-52ef9d97eca8\"\n},\n payload:msg.payload,\n res:msg.res,\n req:msg.req\n};\nreturn msg;","outputs":1,"noerr":0,"x":289,"y":219,"wires":[["983540d4.2676"]]},{"id":"c9f3fe53.70eb1","type":"function","z":"89fa1dea.67dd2","name":"Correio","func":"msg = \n{nlu_options:{entity_model:\"10:d6e8bc64-ccbb-436f-a488-dfc5ea5b1a9d\",\n              relations_model:\"10:d6e8bc64-ccbb-436f-a488-dfc5ea5b1a9d\"\n},\n payload:msg.payload,\n res:msg.res,\n req:msg.req\n};\nreturn msg;","outputs":1,"noerr":0,"x":269,"y":327,"wires":[["983540d4.2676"]]},{"id":"64a2dc3b.916984","type":"function","z":"89fa1dea.67dd2","name":"Print_TJ","func":"msg = \n{nlu_options:{entity_model:\"10:560e1396-ea4d-48e4-84ae-d801786ebd14\",\n              relations_model:\"10:560e1396-ea4d-48e4-84ae-d801786ebd14\"\n},\n payload:msg.payload,\n res:msg.res,\n req:msg.req\n};\nreturn msg;","outputs":1,"noerr":0,"x":270,"y":291,"wires":[["983540d4.2676"]]},{"id":"6218147c.3b7eac","type":"function","z":"89fa1dea.67dd2","name":"Determinação Judicial","func":"msg = \n{nlu_options:{entity_model:\"10:091feb70-50d5-40a1-8513-3d5b4d687c6b\",\n              relations_model:\"10:091feb70-50d5-40a1-8513-3d5b4d687c6b\"\n},\n payload:msg.payload,\n res:msg.res,\n req:msg.req\n};\nreturn msg;","outputs":1,"noerr":0,"x":309,"y":254,"wires":[["bb53018d.70c0c"]]},{"id":"61702483.62761c","type":"function","z":"89fa1dea.67dd2","name":"Modelo Inexistente","func":"msg.payload =\n{\"rc\":300,\"msg\":\"Modelo desconhecido\"};\nreturn msg;","outputs":1,"noerr":0,"x":299,"y":363,"wires":[["1e755ac.5d36ea5"]]},{"id":"bb53018d.70c0c","type":"function","z":"89fa1dea.67dd2","name":"","func":"msg.payload = \n{\"rc\":350,\"msg\":\"Modelo em construção\"};\nreturn msg;","outputs":1,"noerr":0,"x":495,"y":322,"wires":[["1e755ac.5d36ea5"]]},{"id":"23ca5b8a.154c24","type":"change","z":"89fa1dea.67dd2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"features","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":637,"y":280,"wires":[["1e755ac.5d36ea5"]]},{"id":"7d595d5.1f022a4","type":"catch","z":"89fa1dea.67dd2","name":"","scope":["983540d4.2676"],"x":259,"y":442,"wires":[["9be9b90b.d98348","7a750f77.fcadb"]]},{"id":"7a750f77.fcadb","type":"debug","z":"89fa1dea.67dd2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":710,"y":522,"wires":[]},{"id":"9be9b90b.d98348","type":"function","z":"89fa1dea.67dd2","name":"","func":"msg.payload = \n{\"rc\":360,\"msg\":\"Erro inesperado no NLU\"};\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":442,"wires":[["1e755ac.5d36ea5"]]},{"id":"1e755ac.5d36ea5","type":"change","z":"89fa1dea.67dd2","name":"","rules":[{"t":"set","p":"res","pt":"msg","to":"resAux","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":646,"y":361,"wires":[["46c28d10.c2daf4","9d9ba5fb.2e1918"]]},{"id":"d4d88658.0dbcf8","type":"cloudant out","z":"89fa1dea.67dd2","name":"","cloudant":"","database":"dokia","service":"node-red-dokia-cloudantNoSQLDB","payonly":false,"operation":"insert","x":844.5,"y":456,"wires":[]},{"id":"e2e40381.8767d","type":"function","z":"89fa1dea.67dd2","name":"","func":"msg = \n{pageIndex:context.flow.pageIndex,\n pageName:context.flow.pageName,\n ocrData:context.flow.ocrData,\n NLU:msg.features};\nreturn msg;","outputs":1,"noerr":0,"x":703.5,"y":456,"wires":[["d4d88658.0dbcf8"]]},{"id":"9d9ba5fb.2e1918","type":"delay","z":"89fa1dea.67dd2","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":700.5,"y":411,"wires":[["e2e40381.8767d"]]}]

@sergiogama the first Change node in your flow is copying msg.res to flow.resAux - which you then copy back at the end of your flow.

That is the cause of the problem. As soon as the second request arrives, whilst the first is still being processed, flow.resAux will be overwritten with the response object for the second request. When the first request reaches the final Change node, it will be given the second request’s response object - and respond to the wrong request.

The same will be true of flow.pageName and flow.pageIndex which you also set each time a message arrives.

You need to leave msg.res attached to the message as it passes through the whole flow.

Thanks!! I will try!!

Thanks! It’s woking fine!!

Is there any limitation of request on node-red server ?

In what way do you mean?

ss ss2

For example I have multinodes like above and much more which handle multiple mqtt process and http requests. And sensors which are in my house send data to node-red asynchronously. Can Node-red handle it at the same time or I should try different approaches ? I am new on red sorry for my ignorance and thanks for answering

It should handle it unless you run or processor power or (if you are doing a lot of web stuff) network bandwith. So it depends on what hardware you are using. However, a Pi, for example, will likely handle hundreds of requests a second before it runs out of steam.

1 Like

thanks for answering :slight_smile: and the last question, all nodes use msg.payload, is this msg object same for each thread or different ?

Messages are passed down the wires between nodes so they will all be different.
Read through the docs, particularly the user guide, to see how the system works
https://nodered.org/docs

1 Like