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

[Bug 1.25.1] (Deno.serve) - Only handles one request at a time #15758

Closed
sagea opened this issue Sep 4, 2022 · 2 comments · Fixed by #15852
Closed

[Bug 1.25.1] (Deno.serve) - Only handles one request at a time #15758

sagea opened this issue Sep 4, 2022 · 2 comments · Fixed by #15852
Assignees
Labels
bug Something isn't working correctly

Comments

@sagea
Copy link
Contributor

sagea commented Sep 4, 2022

Description

The new Deno.serve method only appears to be allowing one request at a time.

I have two examples below:

  1. Using Deno.serve (Runs all requests synchronously)
  2. Using std HTTP serve (Runs all requests in parallel)

Example (Using Deno.serve)

deno-serve-example
Command: RUST_BACKTRACE=full deno run -A --unstable serve-test.ts

Source:

let active = 0;
const sleep = (time: number) => new Promise(r => setTimeout(r, time));
const start = (pathname: string) => {
  active++;
  console.log(`⏰ Request received  ${pathname}  | Total Active: ${active}`)
};
const end = (pathname: string) => {
  active--;
  console.log(`✅ Request completed ${pathname}  | Total Active: ${active}`);
}

const handler: Deno.ServeHandler = async (event) => {
  const pathname = new URL(event.url).pathname;
  start(pathname);
  await sleep(2000);
  try {
    return new Response('done');
  } finally {
    end(pathname);
  }
};

Deno.serve(
  {
    handler,
    port: 9999,
    onListen({ hostname, port }) {
      console.log(`Server Started... http://${hostname}:${port}/`);
    }
  } 
)

Example (Using std/http/server)

std-http-serve-example

Command: RUST_BACKTRACE=full deno run -A serve-test.ts

Source:

import { serve } from "https://deno.land/std@0.154.0/http/server.ts";

let active = 0;
const sleep = (time: number) => new Promise(r => setTimeout(r, time));
const start = (pathname: string) => {
  active++;
  console.log(`⏰ Request received  ${pathname}  | Total Active: ${active}`)
};
const end = (pathname: string) => {
  active--;
  console.log(`✅ Request completed ${pathname}  | Total Active: ${active}`);
}

const handler: Deno.ServeHandler = async (event) => {
  const pathname = new URL(event.url).pathname;
  start(pathname);
  await sleep(2000);
  try {
    return new Response('done');
  } finally {
    end(pathname);
  }
};

serve(
  handler,
  {
    port: 9999,
    onListen({ hostname, port }) {
      console.log(`Server Started... http://${hostname}:${port}/`);
    }
  } 
)
@littledivy littledivy added bug Something isn't working correctly flash labels Sep 4, 2022
@littledivy
Copy link
Member

Thanks for the detailed reproduction. I can confirm that #15852 fixes the issue:

Screen.Recording.2022-09-11.at.9.40.11.AM.mov

@littledivy littledivy self-assigned this Sep 11, 2022
@sagea
Copy link
Contributor Author

sagea commented Sep 12, 2022

@littledivy Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants