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

feat(ext/http): abort event when request is cancelled #26781

Merged
merged 5 commits into from
Nov 8, 2024

Conversation

littledivy
Copy link
Member

Deno.serve(async (req) => {
  const { promise, resolve } = Promise.withResolvers<void>();

  req.signal.addEventListener("abort", () => {
    resolve();
  });

  await promise;

  return new Response("Ok");
});

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool, have you measured the performance impact?

@littledivy
Copy link
Member Author

Yep. No extra overhead unless req.signal is accessed. Even then its negligible:

Deno.serve(req => {
  req.signal.addEventListener("abort", () => {});
  return new Response("Hello");
})
divy@divy-macbook deno % wrk http://localhost:8000 # main
Running 10s test @ http://localhost:8000
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   102.95us   29.67us   0.94ms   78.63%
    Req/Sec    44.97k     2.99k   49.21k    70.30%
  903669 requests in 10.10s, 123.24MB read
Requests/sec:  89475.95
Transfer/sec:     12.20MB

divy@divy-macbook deno % wrk http://localhost:8000 # this PR
Running 10s test @ http://localhost:8000
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   103.74us   40.32us   2.33ms   88.91%
    Req/Sec    44.94k     3.92k   50.48k    68.32%
  903253 requests in 10.10s, 123.18MB read
Requests/sec:  89433.32
Transfer/sec:     12.20MB

@littledivy littledivy merged commit b482a50 into denoland:main Nov 8, 2024
17 checks passed
@littledivy littledivy deleted the req_cancel_event_abort branch November 8, 2024 13:16
@littledivy littledivy restored the req_cancel_event_abort branch November 8, 2024 13:16
littledivy added a commit that referenced this pull request Nov 10, 2024
```js
Deno.serve(async (req) => {
  const { promise, resolve } = Promise.withResolvers<void>();

  req.signal.addEventListener("abort", () => {
    resolve();
  });

  await promise;

  return new Response("Ok");
});
```
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 this pull request may close these issues.

2 participants