Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Supervisors, Links and Monitors
Search
Sean Cribbs
September 20, 2016
Technology
0
92
Supervisors, Links and Monitors
Using OTP tools in your Elixir application for great justice
Sean Cribbs
September 20, 2016
Tweet
Share
More Decks by Sean Cribbs
See All by Sean Cribbs
Adopting Stream Processing for Instrumentation
seancribbs
1
360
Getting the Word Out: Membership, Dissemination, and Population Protocols
seancribbs
0
340
Reliable High-Performance HTTP Infrastructure with nginx and Lua
seancribbs
0
570
The Refreshingly Rewarding Realm of Research Papers
seancribbs
0
360
Roll-your-own API Management Platform with NGINX and Lua
seancribbs
10
1.1k
Techniques for Metaprogramming in Erlang
seancribbs
2
1.4k
The Final Causal Frontier
seancribbs
6
910
Erlang - Building Blocks for Global Distributed Systems
seancribbs
4
790
A Brief History of Time in Riak
seancribbs
4
1.7k
Other Decks in Technology
See All in Technology
「視座」の上げ方が成人発達理論にわかりやすくまとまってた / think_ perspective_hidden_dimensions
shuzon
2
5.4k
物価高なラスベガスでの過ごし方
zakky
0
390
来年もre:Invent2024 に行きたいあなたへ - “集中”と“つながり”で楽しむ -
ny7760
0
480
新卒1年目が向き合う生成AI事業の開発を加速させる技術選定 / ai-web-launcher
cyberagentdevelopers
PRO
7
1.5k
ガバメントクラウド先行事業中間報告を読み解く
sugiim
1
1.4k
APIテスト自動化の勘所
yokawasa
7
4.2k
10分でわかるfreeeのQA
freee
1
3.4k
いまさらのStorybook
ikumatadokoro
0
150
フルカイテン株式会社 採用資料
fullkaiten
0
36k
pandasはPolarsに性能面で追いつき追い越せるのか
vaaaaanquish
4
4.7k
Forget efficiency – Become more productive without the stress
ufried
0
150
「 SharePoint 難しい」ってよく聞くけど、そんなに言うなら8歳の息子に試してもらった
taichinakamura
1
630
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
363
19k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
9
680
Docker and Python
trallard
40
3.1k
The Cult of Friendly URLs
andyhume
78
6k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
Why Our Code Smells
bkeepers
PRO
334
57k
4 Signs Your Business is Dying
shpigford
180
21k
What's new in Ruby 2.0
geeforr
342
31k
The Language of Interfaces
destraynor
154
24k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
167
49k
Facilitating Awesome Meetings
lara
49
6k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Transcript
SUPERVISORS, LINKS & MONITORS Sean Cribbs
FLASHBACK: 2008 a more innocent time
PLAYSKOOL: MY FIRST ERLANG twitter client mochiweb handler mnesia
PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH twitter
client mochiweb handler mnesia
PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤
Buggy request handler? CRASH twitter client mochiweb handler mnesia
PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤
Buggy request handler? CRASH ➤ Mnesia acting weird? CRASH twitter client mochiweb handler mnesia
PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤
Buggy request handler? CRASH ➤ Mnesia acting weird? CRASH twitter client mochiweb handler mnesia RESULT: Unpredictable behavior
SUPERVISORS are not for restarting processes
SUPERVISORS are for structuring your application
HINDSIGHT IS 20/20 twitter client mochiweb handler mnesia my_app (supervisor)
HINDSIGHT IS 20/20 ➤ mnesia as application dependency twitter client
mochiweb handler mnesia my_app (supervisor)
HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client
and mochiweb handler as children twitter client mochiweb handler mnesia my_app (supervisor)
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)
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)
:observer.start()
SUPERVISOR STRATEGIES ➤ :one_for_one ➤ :one_for_all ➤ :rest_for_one ➤ :simple_one_for_one
SUPERVISOR STRATEGIES ➤ :one_for_one ➤ :one_for_all ➤ :rest_for_one ➤ :simple_one_for_one
㱺 Task.Supervisor
SUPERVISORS use links and trap exits
LINKS intertwine fates of two processes
A B
A B Process.link(b)
file_server_2 Port (driver)
file_server_2 Port (driver) shutdown!
file_server_2 Port (driver) shutdown! EXIT close()
None
TRAP EXITS if you need to handle exit signals
TRAP EXITS if you need to handle exit signals Process.flag(:trap_exit,
true)
TRAP EXITS if you need to handle exit signals Process.flag(:trap_exit,
true) {:EXIT, pid, reason}
MONITORS notify of process exit without linking
A B
A B Process.monitor(b)
A B Process.monitor(b) {:DOWN, ref, :process, pid, reason}
A B GENSERVER.CALL/3
A B GENSERVER.CALL/3 ➤ Monitor the target {:ok, ref} =
Process.monitor(b)
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}
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}
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}
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}
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}
registry conn sup app sup CUSTOM PROCESS REGISTRY
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry client
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
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
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
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
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
TIPS AND TRICKS ➤ Don’t put too much on your
Supervisor ➤ Child order matters ➤ Use links and monitors ➤ Read the Elixir and Erlang source
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