Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong concurrency model #40

Closed
manyuanrong opened this issue Mar 11, 2020 · 3 comments · Fixed by #41
Closed

Wrong concurrency model #40

manyuanrong opened this issue Mar 11, 2020 · 3 comments · Fixed by #41

Comments

@manyuanrong
Copy link
Contributor

This is a piece of code from Oak:

https://github.com/oakserver/oak/blob/master/application.ts#L25-L35

This incorrectly uses await for each request. As a result, requests cannot be executed in parallel, but are executed serially. If two requests are sent in succession, each of which takes 10s, eventually all requests will be completed in 20s instead of all being completed at the same time in 10s

import { Application } from "https://deno.land/x/oak/mod.ts";
const app = new Application();
app.use(async ctx => {
  await new Promise(resolve => setTimeout(resolve, 10000));
  ctx.response.body = "Hello World!";
});
await app.listen({ port: 8000 });
@manyuanrong
Copy link
Contributor Author

My example seems wrong. I also got the same result using setTimeout in Node.js

@manyuanrong
Copy link
Contributor Author

I encountered the problem of deadlock, and I guess it was caused by concurrency, but it wasn't. But my code can respond normally in std / http / server. Deadlocked in oak

Deadlocked

import { Application } from "https://deno.land/x/oak/mod.ts";
const app = new Application();
app.use(async ctx => {
  console.log(ctx.request.url);
  if (ctx.request.url === "/ssr") {
    await fetch("http://localhost:8000/hello")
      .then(res => res.text())
      .then(text => {
        ctx.response.body = text;
      });
  } else {
    ctx.response.body = "Hello World!";
  }
});
await app.listen({ port: 8000 });

It is normal

import { serve } from "https://deno.land/std/http/server.ts";
const body = new TextEncoder().encode("Hello World\n");
for await (const req of serve(":8000")) {
  console.log(req.url);
  if (req.url === "/ssr") {
    fetch("http://localhost:8000/hello")
      .then(res => res.text())
      .then(text => {
        req.respond({ body: text });
      });
  } else {
    req.respond({ body });
  }
}

@Qard
Copy link

Qard commented Mar 12, 2020

Yep, that should really spin off another handleRequest async function which it uses catch(...) on instead of awaiting it. That’d allow it to run in the background.

kitsonk pushed a commit that referenced this issue May 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants