0
votes

So we have generic part and the specific part thanks to erlang/OTP behaviours which greatly simplifies our code and gives us a common skeleton that everybody understands.

I am an Erlang newbie and trying to get my head around concepts of OTP. But amongst

  • gen_server
  • gen_fsm
  • gen_event
  • supervisor

which behaviour is suitable for what kind of application.

The reason why I am asking this question is because after going through some verbose theory,I am still unsure about which is more suited for a XYZ application.

Is there any criteria or a Rule of thumb,or is it just the programmers instincts that matter in this case? Any help?

2

2 Answers

1
votes

Remark, it is a very general question that doesn't fit well to the usage of this community.

Supervisor are here to control the way your application starts and evolves in time. It starts processes in the right order with a verification that everything is going fine, manage the restart strategy in case of failure, pool of processes, supervision tree... The advantages are that it helps you to follow the "let it crash" rule in your application, and it concentrates in a few line of code the complex task to coordinate all your processes.

I use gen_event mainly for logging purposes. You can use this behavior to connect on demand different handlers depending on your target. In one of my application, I have by default a graphical logger that allows to check the behavior at a glance, and a textual file handler that allows deeper and post mortem analysis.

Gen_server is the basic brick of all applications. It allows to keep a state, react on call, cast and even simple messages. It manages the code changes and provides features for start and stop. Most of my modules are gen_servers, you can see it as an actor which is responsible to execute a single task.

Gen_fsm brings to gen server the notion of a current state with a specific behavior, and transition from state to state. It can be used for example in a game to control the evolution of the game (configuration, waiting for players, play, change level...)

When you create an application you will identify the different tasks yo have to do and assign them to either gen_server either gen_fsm. You will add gen_event to allow publish/subscribe mechanism. Then you will have to group all processes considering their dependency and error recovering. This will give you the supervision tree and restart strategy.

0
votes

I use following rules.

  • If you have some long-living entity that serves multiple clients in request-response manner - you use gen_server.
  • If this long-living entity should support more complex protocol that just request/response, there is good idea to use gen_fsm. I like use gen_fsm for network protocol implementation, for example.
  • If you need some abstract event bus, where any process can publish messages, and any client may want receive this messages, you need gen_event.
  • If you have some proceeses which must be restarted together (for example, if one of them fails), you should put this processes under supervisor, which ties this processes together.