Coroutines to create an ultra lightweight web server?

Coroutines, and how they are lighter than threads using internal state-storing magic, sound very cool – and perfect for an ultra lightweight high performance web server.

Has anyone seen an attempt to do this? It would have to be pretty ground-up: using coroutines to service requests inside the minimal com.sun.net.httpserver wouldn’t (AFAIK) be enough, because the outside web serving wrapper would still use threads behind the Handler.

Why not Vert.x + coroutines?

Seems like it has been considered: Redirecting to Google Groups

Yes,
we are working for a pleasant integration.

vert.x is a bit more than a web server, so to implement a only a web server it could be too much.

What about creating a Kotlin layer on top of Grizzly? Grizzyl is a slim, mature high-performance asynchronous server. It could be a solid base for a “Kotlin” server.

However implementing something like Grizzly from scratch in Kotlin with coroutines could be an interesting excercise, although it would be reinventing the wheel …

I’m asking my question poorly.

IF coroutines are all that and a bag of chips, then can anyone create an example that shows off in a real-world scenario just how much better they are? The docs page gives an entirely reasonable “sum up numbers” to show how they beat threads w/r/t memory overhead and some other aspects, but it feels like they are presented as so much better that a more complex example would really show off their potential.

eg. “Look how this 40 line game server can scale to thousands of parallel connections” sort of thing.

Like this! (just found it)
ktor-samples/chat

Look how this 73-line example (28 lines of which are license boilerplate and imports) can accepts thousands of connections in a single thread with no dependency on any framework, but just kotlinx.coroutines library:
https://github.com/Kotlin/kotlinx.coroutines/blob/master/integration/kotlinx-coroutines-nio/src/test/kotlin/examples/echo-example.kt

2 Likes

Yes, that! Bravo!

I am using Jooby to get it easily with a full featured web server :

https://gist.github.com/fabienmoritz/b8f8239a781e89278097cca13af70341#file-main-kt