vercel / micro

Asynchronous HTTP microservices
MIT License
10.59k stars 458 forks source link

Async Issue with Multiple Requests #402

Closed nahtnam closed 5 years ago

nahtnam commented 5 years ago

Hello,

So I am noticing something weird and I want to double check to make sure I'm doing things right. What I am noticing is that micro is NOT async. I am unable to have micro process two requests at once.

I have a reproducible example here: https://codesandbox.io/s/microissue-75ww7

So to reproduce, open https://75ww7.sse.codesandbox.io/ in two tabs at around the same time. If micro was async, what would happen is both requests would get put on the event loop and would snooze for 5 seconds. So both requests should resolve after around 5 seconds. What actually happens is that the first request snoozes for 5 seconds and resolves, but the second request snoozes for the 5 seconds for the first request, and then another 5 seconds for itself. Am I doing something wrong here?

This isn't noticeable on a serverless platform but it is very apparent on a non-serverless platform. Does anyone know why? And how do we fix this?

Thanks, Manthan M.

OlliV commented 5 years ago

I cannot reproduce with this code:

module.exports = async (req, res) => {
    await new Promise((r) => setTimeout(r, 15000));
    return 'OK';
};
nahtnam commented 5 years ago

@OlliV How did you run the code?

I can reproduce it with this code:

const micro = require("micro");

const server = micro(async (req, res) => {
  await new Promise(r => setTimeout(r, 5000));
  return "hello world";
});

server.listen(8080);

See here: https://codesandbox.io/s/microissue-75ww7

Steps:

  1. Open https://75ww7.sse.codesandbox.io/ in two tabs at the same time

The second tab will return after 10 seconds instead of 5.

I can also reproduce this on my local machine.

OlliV commented 5 years ago

micro-sleep

nahtnam commented 5 years ago

Interesting... I tried with two different browsers and it seems to work fine. I think it may be an issue with Chrome where it does not load one page until the other one is finished... Sorry for the troubles