Deploy the main server loop in Haskell?

What is the generally accepted way to implement the main server loop that needs to wait for a heterogeneous set of events? That is, the server must wait (not busywait) until one of the following events occurs:

  • connecting a new socket
  • data available on an existing socket
  • OS signal
  • 3rd party callbacks
+5
source share
4 answers

I think you are thinking in terms of the C paradigm with a single thread, non-blocking I / O and calling select ().

You can write something like this in Haskell, but Haskell offers much more features:

STM.

+6

takeMVar putMVar . , , . ghc docs.

+2

, , , , , , .

,

data SocketConn = ....
data DataAvail = ...
data OSSignal = ...
data Callback = ...

data ServerEvent = Sok SocketConn | Dat DataAvail | Sig OSSignal | Call Callback

handleEvent :: ServerEvent -> IO ()
handleEvent (Soc s) = ....
handleEvent (Dat d) = ....
handleEvent (Sig o) = ....
handleEvent (Call c) = ....

, !

+1

(STM) - .

, , , , Haskell , , .

You do not want to create thousands of OS threads, but a thousand Haskell threads are not a problem.

(If these threads need to coordinate from time to time, then again STM is probably the easiest and most reliable way to do this.)

0
source

All Articles