Coroutines to create an ultra lightweight web server?


#1

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.


#2

Why not Vert.x + coroutines?


#3

Seems like it has been considered: https://groups.google.com/forum/#!topic/vertx/Bgt77vcyynU


#4

Yes,
we are working for a pleasant integration.

https://github.com/vert-x3/vertx-lang-kotlin/pull/14


#5

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 …


#6

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.


#7

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


#8

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:


#9

Yes, that! Bravo!


#10

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

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