Chapter 4. Generic Servers
Having broken up the radio frequency allocator into generic and
specific modules and investigated some of the corner cases that can occur
when dealing with concurrency, you will have figured out there is no need to
go through this process every time you have to implement a client-server
behavior. In this chapter, we introduce the gen_server OTP behavior, a library module that
contains all of the generic client-server functionality while handling a
large number of corner cases. Generic servers are the most commonly used
behavior pattern, setting the foundations for other behaviors, all of which
can be (and in the early days of OTP were) implemented using this
module.
Generic Servers
The gen_server module implements the client-server behavior we extracted in the
previous chapter. It is part of the standard library application and
available as part of the Erlang/OTP distribution. It contains the generic
code that interfaces with the callback module through a set of callback functions. The
callback module, in our example containing the code specific to the frequency server, is
implemented by the programmer. The callback module has to export a series
of functions that follow naming and typing conventions, so that their
inputs and return values conform to the protocol required by the
behavior.
As seen in Figure 4-1, the functions of both the behavior and callback module execute within the scope the same server process. In other words, a process loops in the generic ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access