Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Indeed. I hate to contribute to this, but when I get home, I am going to blog about my coroutine-based echo server. Fewer lines of code than the original, scales linearly over active connections (and constant time over idle sockets), and my tiny eeepc laptop can easily handle 1,000,000 open connections.

Sometimes "worse is better" isn't.



If you aren't joking, I want to see this.


Here is the gist of the code:

    use EV;
    use Coro;
    use Coro::EV;
    use Coro::Handle;
    use AnyEvent::Socket;

    tcp_server undef, 1234, sub {
        my ($_fh, $host, $port) = @_;

        # this makes <>, print, etc. non-blocking on $fh
        my $fh = unblock $_fh;
    
        # this creates the new thread
        async {
            while(my $line = <$fh>){
                print $fh $line;
            }
        };
    };

    EV::loop;
This code, a client and alternative server implementation are available on github for perusal:

http://gist.github.com/204811

I do not have time to write up the details now... but basically, I run out of file descriptors before the client or server use a measurable amount of memory. This is around 30,000 connections. With 100 connections, it does around 8000 requests a second (client and server on same dual-core machine). With 30,000 concurrent connections, it does about the same. That is O(1) at its finest. (The 1 is the 1 connection we are actively using in the client.)

The EV-only server in the github repository performs about the same, but keeps track of all the open connections so you can trivially shutdown the server cleanly.


Are you using coroutines in Perl or some other language? And I wanna see that code in any case :p


Yes, Perl. See code in separate reply.




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

Search: