Skip to content

Instantly share code, notes, and snippets.

@talum
Created September 20, 2017 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talum/f9e2b8ca1916e755d48b698ba2b09f2d to your computer and use it in GitHub Desktop.
Save talum/f9e2b8ca1916e755d48b698ba2b09f2d to your computer and use it in GitHub Desktop.
programming phoenix ch 11

Programming Phoenix Ch 11 : OTP

Managing State with Processes

  • Use concurrency and recursion to manage state
  • separate of client and server
  • used diff abstractions for asynchronous and synchronous communication with server

Building GenServers for OTP

  • don't need to worry about ref
  • GenServer in control of receive loop

Supervisors

  • trust error reporting to log errors so we can fix what's broken
  • automatically restart services in last good state
  • self-healing software
  • child_spec defines the children that an Elixir app will start
  • server can crash and then restart w/ a supervisor

Restart Strategies

  • :permanent, child is always restarted (default)
  • :temporary, child is never restarted. Useful for when a restart is unlikely to resolve problem
  • :transient, child is restarted only if it termiantes abnormally (with exit reason other than :normal, :shutdown, or {:shutdown, term}

More options

  • max_restarts and max_seconds
  • OTP only restart application max_restarts times in max_seconds before failing and reporting error up supervision tree

Supervision Strategies

  • one_for_one, if child terminates, supervisor restarts only that process
  • one_for_all, if child termiantes, supervisor terminates all children and then restarts all children
  • rest_for_one, if child terminates, supevisor terminates all child processes defined after the one that dies. Supervisor restarts all terminated processes
  • simple_one_for_one, used when supervisor needs to dynamically supervise processes

Q: kill all children in that part of the tree?

Using Agents

  • simpler abstraction of benefits of GenServer
  • agent has five main functions: start_link, stop, update, get, get_and_update
  • pass functions?
  • What's the point?
  • behind the scenes, agent is OTP genserver

Registering Processes

  • can give process a name by passing it into start_link and then send messages to the process by name
  • Agents are constructs built on top of OTP

OTP and Channels

  • build a tree of supervisors where each node knows how to restart any major service if it fails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment