Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Supervisors, Links and Monitors

Sean Cribbs
September 20, 2016

Supervisors, Links and Monitors

Using OTP tools in your Elixir application for great justice

Sean Cribbs

September 20, 2016
Tweet

More Decks by Sean Cribbs

Other Decks in Technology

Transcript

  1. PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤

    Buggy request handler? CRASH twitter client mochiweb handler mnesia
  2. PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤

    Buggy request handler? CRASH ➤ Mnesia acting weird? CRASH twitter client mochiweb handler mnesia
  3. PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤

    Buggy request handler? CRASH ➤ Mnesia acting weird? CRASH twitter client mochiweb handler mnesia RESULT: Unpredictable behavior
  4. HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client

    and mochiweb handler as children twitter client mochiweb handler mnesia my_app (supervisor)
  5. HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client

    and mochiweb handler as children ➤ Supervisor initializes mnesia tables twitter client mochiweb handler mnesia my_app (supervisor)
  6. HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client

    and mochiweb handler as children ➤ Supervisor initializes mnesia tables twitter client mochiweb handler mnesia RESULT: Reliability my_app (supervisor)
  7. A B

  8. A B

  9. A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the

    message tagged with the monitor reference {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
  10. A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the

    message tagged with the monitor reference ➤ Receive {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
  11. A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the

    message tagged with the monitor reference ➤ Receive ➤ {^ref, reply} ⇒ 
 {:ok, reply} {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
  12. A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the

    message tagged with the monitor reference ➤ Receive ➤ {^ref, reply} ⇒ 
 {:ok, reply} ➤ :DOWN ⇒ exit(reason) {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
  13. A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the

    message tagged with the monitor reference ➤ Receive ➤ {^ref, reply} ⇒ 
 {:ok, reply} ➤ :DOWN ⇒ exit(reason) ➤ timeout ⇒ exit(:timeout) {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
  14. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node
  15. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry client
  16. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child client
  17. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child conn client
  18. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child ➤ Registry ⇒ monitor connection conn client
  19. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child ➤ Registry ⇒ monitor connection ➤ Client ⇐ connection pid 
 (maybe monitor?) conn client
  20. registry conn
 sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:

    Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child ➤ Registry ⇒ monitor connection ➤ Client ⇐ connection pid 
 (maybe monitor?) ➤ DOWN ⇒ Registry cleans up client
  21. TIPS AND TRICKS ➤ Don’t put too much on your

    Supervisor ➤ Child order matters ➤ Use links and monitors ➤ Read the Elixir and Erlang source
  22. REFERENCES ➤ Erlang Reference Manual: Processes
 http://erlang.org/doc/reference_manual/processes.html ➤ Erlang Reference

    Manual: Errors
 http://erlang.org/doc/reference_manual/errors.html ➤ OTP Design Principles: Overview
 http://erlang.org/doc/design_principles/des_princ.html ➤ OTP Design Principles: Supervisors
 http://erlang.org/doc/design_principles/sup_princ.html ➤ OTP Design Principles: Applications
 http://erlang.org/doc/design_principles/applications.html