[erlang-questions] Beginner gen_server and child monitoring

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Wed Aug 3 14:26:15 CEST 2016


On Thu, Jul 28, 2016 at 7:47 AM, Giovanni Giorgi <jj@REDACTED> wrote:

> Now the question is: Is a correct approach?


The key observation is: "How do you want to handle error in the processes?"
That is, you have a Main process and several Ticker processes. What should
happen if the Main process crashes or goes away? What should happen if a
Ticker process crashes or goes away? This can guide a solution.

You could simply run the tickers as Torben suggests and then spawn_link
them under the main process. This means, by default, if any ticker or the
main process crashes, so does everyone else.

You could also use an asymmetric solution: spawn_monitor the tickers and
have the tickers monitor the main process. This allows you to handle the
case where you want to restart a crashing ticker for instance, but you have
to write the logic yourself.

In general, spawning processes without knowing where they are only works
for processes which are guaranteed to terminate. Otherwise, you end up with
process leaks.

The final solution is to use a supervision tree: have a main_sup with
children MainProcess and TickerPool, the latter being a simple_one_for_one
supervisor. Each ticker is added into the TickerPool with
supervisor:start_child. It is the the most complex solution, but it has an
advantage over the simpler methods: it encodes a *policy* in the
supervisors for handling failure modes. That is, you can tolerate some
tickers dying, but not too many before trying to restart larger parts of
the system.

So the TL;DR is to think about the failure mode of the application and
write it accordingly.


-- 
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160803/25a287b9/attachment.htm>


More information about the erlang-questions mailing list