com-lihaoyi / cask

Cask: a Scala HTTP micro-framework
https://com-lihaoyi.github.io/cask/
Other
524 stars 53 forks source link

Use Java Virtual Threads (Loom) (500USD Bounty) #90

Open sorumehta opened 10 months ago

sorumehta commented 10 months ago

Spawn a virtual thread for each incoming request, which would enable asynchronous behaviour and increase performance of the library. The new Java virtual threads are lightweight and can replace the traditional Java threads. Given that Cask does not have any strong async/concurrency model of its own, leveraging the new JVM feature would be really nice.

Haoyi: To incentivize contribution, I'm putting a 500USD bounty on resolving this ticket. This is payable via bank transfer, and at my discretion in case of ambiguity. The acceptance criteria is a PR implementing support for virtual threads, tests exercising them, and performance benchmarks (ad-hoc is OK) demonstrating a performance improvement (e.g. in high concurrency use cases) vs normal threads in Java 21+

sorumehta commented 10 months ago

I can create a PR for this if this seems like a good idea to @lihaoyi

lihaoyi commented 10 months ago

Go for it, though note we'll need some compatibility story for Java <21

lloydmeta commented 6 months ago

Is this something that https://github.com/com-lihaoyi/cask/pull/74 unblocks?

andreak commented 2 months ago

I would recommend against this until JAVA-23 is released, du to the "pinning issue", effectively making Virtual Threads useless if your code/libs uses synchronized a lot. https://www.infoworld.com/article/3713220/java-virtual-threads-hit-with-pinning-issue.html

lihaoyi commented 2 months ago

Doing it opt-in should be fine. We can run benchmarks etc. without synchronized to see the benefit. That way we can be ready on day one when Java 23 lands with a fix to the thread pinning issue.

He-Pin commented 2 months ago

I have done this at $work for all my services, with Java 21, it works fine, no need to delay to Java 23.

sideeffffect commented 2 weeks ago

If there are issues because of undertow, you may consider switching to Nima, which is the new web server from Oracle, built for working with virtual threads.

https://helidon.io/nima

https://github.com/helidon-io/helidon/tree/main/webserver