Hacker Timesnew | past | comments | ask | show | jobs | submitlogin
Building a concurrent TCP server with libmill (libmill.org)
69 points by rumcajz on July 25, 2015 | hide | past | favorite | 16 comments


If I understand correctly, this is a framework for non-preemptive cooperative multitasking whereby provided API functions serve as yield points. Pretty much what Windows API was before NT/2000. Not exactly a co-routine library, at least not in a conventional definition of co-routines.


Yes, that's exactly what it is. But it also matches wikipedia's difinition of coroutine: "Coroutines are computer program components that generalize subroutines for nonpreemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations."


Fibers and coroutines are different uses for the same thing. If you schedule your coroutines based on the kernel scheduler, they're fibers. If you jump between fibers explicitly, they're coroutines.


Using that definition, libmill is definitely coroutine-based. It does the scheduling itself.


I think it's mainly a fiber library, actually. The TCP and channel functions yield and resume automatically based on when the underlying operation blocks.

They have yield(), but I don't see the opposite, the ability to explicitly resume a stopped context.

Edit: for a "pure" coroutine library, I'd recommend libco from BSNES. It only has 4 functions: active, create, switch and delete. I wrote a fiber library using libco plus libuv, which was fairly easy.

https://web.archive.org/web/20140209005854/http://byuu.org/p... (unfortunately he still hasn't rebuilt his site)


Can anyone explain the deadlines vs timeout distinction?


Timeout: finish in 10 seconds.

Deadline: finish at 7:15:35PM.

The advantage of deadlines is that you can define a fixed deadline and then re-use it for any number of calls, without having to subtract the time already spent in previous calls.


Yes, and if you set the deadline on the client, the server knows (subject to its time skew) when the request needs to finish. With a timeout, you have to factor in latency, which is necessarily error prone.

Basically, deadline is a context-free timeout.


... unless, of course, it's on the wall clock scale, in which case changing machine time will invalidate all your deadlines. Just need to be careful to use something like 'ticks from the last boot' rather than UTC time.


In competently run environments, wall clock changes are smeared. :) there is no way to use ticks since last boot, since the server will have a different value from the client.


This looks very useful. Would this be a good base to make something like a custom HTTP proxy with?


Are the coroutines scheduled across cores or do they all run in the same thread?


Same thread.


Very cool. Any idea how this compares to lthread?


Would be cool to add KONG [1] to libmill to secure and monitor API traffic.

[1] https://githu.com/mashape/kong


This comment doesn't even make sense.

Kong is a cool product, but Kong spam from a couple of accounts [1] on HN is getting tiresome. (Maybe it's just that I look through the comments of most stories to do with APIs.)

https://hn.algolia.com/?query=mashape%2Fkong&sort=byPopulari...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: