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

Attila Rajmund Nohl attila.r.nohl@REDACTED
Wed Apr 20 14:51:39 CEST 2011


2011/4/20, Joe Armstrong <erlang@REDACTED>:
> On Wed, Apr 20, 2011 at 10:59 AM, Samuel Rivas
> <samuel.rivas@REDACTED>wrote:
>
>> > Things become problematic when you do not entirely understand the
>> > abstraction.
>> > Maybe the abstraction is inappropriate for your needs. I have seen many
>> > examples
>> > of code where the gen_server *is* inappropriate. The acid test is "does
>> the
>> > gen_sever code look like spaghetti" if the answer is yes then all you
>> have
>> > done
>> > is shoe horn the applications into an inappropriate form. In this case
>> > you should ditch the gen_server and roll-your own.
>>
>> Another problem I often see is that newbies tend to regard gen_server
>> as something that does magic, and fail to understand how the code runs
>> in the server and the clients (since that code lives in the same
>> module, usually).

I think this is the most confusing design pattern in Erlang:

send(x,y) ->
  gen_server:call(?NAME, {send, x, y}).

handle_call({send, x, y}, State) ->
  i_send(x, y).

i_send(x,y) ->
  ...

One always have to keep in mind that actual code he's looking at is in
the client process (send) or server (i_send). Obviously calling send
from i_send in the above example would just lead to a deadlock. Maybe
it would be a nice tool that I could run on a huge codebase and split
these modules into a client and server part...



More information about the erlang-questions mailing list