1
votes

I have an app which takes a long time to procces post requests. if a client sends more than 6 requests in a short space of time (say 5 seconds), I get some unexpected behaivior.

for example: serverside

app.options('/',cors({origin: "http://localhost:5050"}));
app.post('/',cors({origin: "http://localhost:5050"}),async(req,res) => {
  console.log(Date.now());
  await new Promise(resolve => setTimeout(resolve, 5000));
  console.log("----complete----")
  res.send("Success");
})

client side:

$.ajax ({
   url: "http://127.0.0.1:5050/",
   type: "POST",
   data: JSON.stringify({token:"62e2c285a01937cdb462985d"}),
   contentType: "application/json; charset=utf-8"
}).done(function(result,status,xhr){
    console.log("done");}

if the client makes more than 7 requests in the 5 second period you get the following output (server side) output (in milliseconds since epoch):

1659038307446
1659038307703
1659038307985
1659038308260
1659038308528
1659038308818
----complete----
1659038312459
----complete----
1659038312712
----complete----
1659038312994
----complete----
1659038313266
----complete----
----complete----
----complete----
----complete----
----complete----
----complete----

as you can see the logged times the first 6 times are around 0.3 seconds apart (this is how fast I clicked the button on my browser to initiate requests), but the 6th and 7th logged times are spaced about 5 seconds apart. I would have expected all of the logged times to be spaced 0.3 seconds apart. Clearly node is waiting to finish the 1st request before starting to proccess the 7th.

Why is this happening, why the limit of 6, and is there anyway to override this behaviour?

I believe you should read up on the event loop developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop - HugoDos