[erlang-questions] The gen server simplified (how it works)

Attila Rajmund Nohl attila.r.nohl@REDACTED
Wed Apr 20 20:23:16 CEST 2011


2011/4/20, Jeroen Koops <koops.j@REDACTED>:
[...]
> On a related note, I have been moving away from using gen_servers all the
> time. The reason for this is that in a gen_server, the state consists of
> just the state term. In an arbitrary process, the state actually consists of
> the process' entire call-stack, and it makes the flow in code such as the
> almost proverbial:
>
> idle() ->
>     receive
>         { Number, incoming } ->
>              start_ringing(),
>              ringing(Number);
>         off_hook ->
>              start_tone(),
>              dial()
>     end.
>
> ... much easier to follow than when using a gen_server or gen_fsm.

On the other hand it's far from the functionality of a gen_server,
because e.g. it doesn't send back data to the caller. When you add
this functionality, you essentially reinvent the gen_server.

In other words: gen_server is great to provide safe access to shared
data, maybe it's not so great to control stuff. There's one more great
point in using the gen_* behaviours - they provide usually useful and
detailed crash reports when something goes wrong.



More information about the erlang-questions mailing list