kvanbere
kvanbere

Reputation: 3352

Haskell: How do you structure a forking server which you can shutdown properly

I'm using network-simple in my project. I would like my function, gateway, to return a few functions in a tuple -- one of which will be shutdown -- a function to boot all the clients and shutdown the server.

If gateway produces many forks using Network.Simple.serve, what's the best way to implement shutdown?

I could implement it using TVar to indicate the server is shutting down, but polling would be a suboptimal solution in my opinion.

Upvotes: 0

Views: 86

Answers (1)

Don Stewart
Don Stewart

Reputation: 137947

There are many ways. Somehow you need a control channel to the server, and that control channel accepts at least one message, "Shutdown".

You could do this with:

  • asynchronous exception: send the Shutdown data type to the server, which will have a handler to do the client
  • test in the event loop: if your server has an eventloop, just treat the incoming control message as part of the event stream
  • polling

Upvotes: 4

Related Questions