This document discusses using OTP (Open Telecom Platform) and the gen_server behavior in Erlang. It begins with an overview of OTP and behaviors, then describes gen_server for client-server interactions. It provides an example stopwatch app in vanilla Erlang and using gen_server. It also discusses other OTP components like supervisor trees, gen_fsm, gen_event and application packaging. Finally, it provides some tips on tools and books related to OTP.
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Using OTP and gen_server Effectively
1. Using OTP and gen_server
effectively
January 19, 2010
Ken Pratt
http://kenpratt.net/
2. OTP = "Open Telecom Platform"
A set of Erlang libraries designed for fault tolerance
and portability.
Now part of the Erlang standard library.
3. Behaviours
A formalization of Erlang design patterns.
Enforce an interface, but have some implementation too.
Kind of like an abstract class in Java.
4. OTP Behaviours
application Erlang application packaging
gen_server Client-server interactions
gen_fsm Finite state machines
gen_event Event handlers
supervisor Process supervision trees
5. Levels of OTP-ification
1. Vanilla Erlang
2. gen_server and gen_...
3. Supervisor trees
4. Application packaging
5. Releases (& boot scripts)
6. Live upgrade support
6. gen_server
A behaviour for client-server-like interactions.
(not necessarily a strict client-server model)
7. Stopwatch Example
Simple stopwatch client & server
Client:
Start stopwatch timer
Stop stopwatch timer
Read timer value
Server:
Start server
Stop server
Maintain stopwatch timer state
Support client operations
8. Stopwatch Example - Vanilla Erlang
stopwatch_client.erl
stopwatch_server.erl
http://gist.github.com/279841
10. Actors
"OOP to me means only messaging, local retention and
protection and hiding of state-process, and extreme late-
binding of all things. It can be done in Smalltalk and in LISP.
There are possibly other systems in which this is possible, but
I'm not aware of them." - Alan Kay, father of Smalltalk
11. Actors
"Actually I made up the term "object-oriented", and I can tell
you I did not have C++ in mind." - Alan Kay
12. Actors
You can model systems using gen_server much like you would
model in an OOP.
Since gen_server synchronizes access to its' internal state, an
entire class of programming errors is sidestepped.
(That said, you can still have race conditions between different
gen_server modules or clients of those).
17. Supervisor trees
Supervisors monitor their children, and attempt to restart them
if they die unexpectedly.
Erlang philosophy is to "let it crash":
No huge amounts of error handling code
No throw/catch hierarchies
Instead, let the process crash and the supervisor will restart
it
18. Supervisor trees
Multiple restart strategies
Configurable number of restart tries, intervals, etc
Support for dynamic children (removing and adding children
at runtime)
19. Echo server example
Echo server:
echo_server:start_link(dog, "Woof!", 3000).
echo_server:echo(dog).
echo_server:stop(dog).
echo_server:assassinate(dog).
echo_server.erl
http://gist.github.com/280607
20. Echo supervisor example
Echo supervisor:
echo_supervisor:start_link().
echo_supervisor.erl
http://gist.github.com/280618
21. gen_fsm
gen_fsm is sort of a super-gen_server.
You define all the states and events, and then the transitions.
Can do async events and sync events.
Earlier stopwatch example would probably be better off as a
state machine if it had more features.
22. gen_event
gen_event can be used to set up chains of event handling for
things like alarms, statistics, and debug traces.
But in reality, it is mostly just used for logging.
23. Other tips
Automatic code reloader (handy for development):
http://code.google.
com/p/mochiweb/source/browse/trunk/src/reloader.erl
Log4erl (if you want a more standard logger):
http://github.com/dilshod/log4erl
Appmon - built-in application monitor/viewer
AJAX-y docs - http://erldocs.com/
24. Books covering OTP
Only the basics Only the basics Intermediate
Erlang Design Principles
http://www.erlang.org/doc/design_principles/des_princ.html