[Haskell] select(2) or poll(2)-like function?

Johan Tibell johan.tibell at gmail.com
Mon Apr 18 15:14:46 CEST 2011


On Mon, Apr 18, 2011 at 2:25 PM, Mike Meyer <mwm at mired.org> wrote:

> Only if you also made the TCP/IP connection overhead trivial so you
> could stop with HTTP/1.0 and not deal with HTTP/1.1. Failing that, the
> most natural way to do this is:
>
> forever do
>        accept the next connection
>         handle requests from connection in new child
>               wait for next events
>               if one is a client request, start the response
>               if one is a finished response, return it to the client
>               if one is something else, something broke, deal with it.
>
> I.e, an event-driven loop for each incoming connection running in it's
> own process.
>

I'm not sure if I would call this "event driven", depending what's hiding
behind the sentences above. Here's how I've written servers that deal with
keep-alive, errors, etc.

server sock = forever $ do
    clientSock <- accept sock
    forkIO $ talk clientSock

talk sock = do
    maybeReq <- read sock
    case maybeReq of
        Nothing -> ... -- error while reading/parsing. Close socket or send
400 Bad Request
        Just req -> do
            resp <- handler req
            send sock resp
            when (isKeepAlive req) $ talk sock

The above code is executed efficiently using epoll but the programming model
is one with blocking system calls.

Johan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell/attachments/20110418/bd844b0b/attachment.htm>


More information about the Haskell mailing list