Chapter 11. Getting Started with OTP
In order to help me understand how the gen_server
behavior works,
I drew the diagram shown in Figure 11-1.
Figure 11-1. Processing a call in gen_server
The client does a gen_server:call(Server, Request)
. The server will
then call the handle_call/3
function that you have provided in the
Module
that you told gen_server
to use. gen_server
will send your
module the client’s Request
, an identifier telling who the request is
From
, and the server’s current State
.
Your handle_call/3
function will fulfill the client’s Request
and
send a {reply, Reply, NewState}
tuple back to the server. It, in turn,
will send the Reply
back to the client, and use the NewState
to update
its state.
In Introducing Erlang and in the next two études,
the client is you, using the shell. The module that handles the
client’s call is contained within the same module as the gen_server
framework, but, as the preceding diagram shows, it does not have to be.
Note
You can learn more about working with OTP basics in Chapters 11 and 12 of Erlang Programming, Chapters 16 and 18 of Programming Erlang, Chapter 4 of Erlang and OTP in Action, and Chapters 14 through 20 of Learn You Some Erlang For Great Good!.
Get Études for Erlang now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.