Using OTP and gen_server Effectively


Published on

(Given to the Vancouver Erlang Meetup group on January 19, 2010.)

Wondering how to model things in Erlang, and what the Actor model is all about? This talk will cover the best of Erlang OTP, delve into using gen_server to model systems in Erlang, and demonstrate the use of supervisor trees to ensure your application stays alive and well. Those new to Erlang might want to look over an intro first, but should still gleam some insights from this talk either way.

Speaker Bio:
Ken Pratt has been working with Erlang full time since the beginning of 2009, which makes him slightly less of an Erlang noob than he was previously. Ken holds a B.Sc. in Computer Science from UBC, and is currently hard at work developing a social networking game at Pug Pharm Productions.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Using OTP and gen_server Effectively

  1. 1. Using OTP and gen_server effectively January 19, 2010 Ken Pratt
  2. 2. OTP = "Open Telecom Platform" A set of Erlang libraries designed for fault tolerance and portability. Now part of the Erlang standard library.
  3. 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. 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. 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. 6. gen_server A behaviour for client-server-like interactions. (not necessarily a strict client-server model)
  7. 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. 8. Stopwatch Example - Vanilla Erlang stopwatch_client.erl stopwatch_server.erl
  9. 9. Stopwatch Example - gen_server stopwatch.erl
  10. 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. 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. 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).
  13. 13. Actor/gen_server examples Web server: State: {Cookies} Call: process_request(Headers, Body) Cast: clear_cache() Cast: stop()
  14. 14. Actor/gen_server examples Chat room: State: {ActiveUsers, ChatLog} Call: enter_room(Handle) Call: leave_room(Handle) Cast: say(Handle, Message) Call: view_log() Cast: stop()
  15. 15. Actor/gen_server examples Database connection: State: {ConnectionPid} or maybe {TcpSocket} Call: execute_sql(Sql) Call: close()
  16. 16. Supervisor trees
  17. 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. 18. Supervisor trees Multiple restart strategies Configurable number of restart tries, intervals, etc Support for dynamic children (removing and adding children at runtime)
  19. 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
  20. 20. Echo supervisor example Echo supervisor: echo_supervisor:start_link(). echo_supervisor.erl
  21. 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. 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. 23. Other tips Automatic code reloader (handy for development): com/p/mochiweb/source/browse/trunk/src/reloader.erl Log4erl (if you want a more standard logger): Appmon - built-in application monitor/viewer AJAX-y docs -
  24. 24. Books covering OTP Only the basics Only the basics Intermediate Erlang Design Principles