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

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.



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

Search: