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
フルカイテン株式会社 採用資料
fullkaiten
0
40k
dev 補講: プロダクトセキュリティ / Product security overview
wa6sn
1
2.3k
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
5
590
障害対応指揮の意思決定と情報共有における価値観 / Waroom Meetup #2
arthur1
5
470
インフラとバックエンドとフロントエンドをくまなく調べて遅いアプリを早くした件
tubone24
1
430
スクラムチームを立ち上げる〜チーム開発で得られたもの・得られなかったもの〜
ohnoeight
2
350
透過型SMTPプロキシによる送信メールの可観測性向上: Update Edition / Improved observability of outgoing emails with transparent smtp proxy: Update edition
linyows
2
210
Engineer Career Talk
lycorp_recruit_jp
0
120
Amazon CloudWatch Network Monitor のススメ
yuki_ink
1
200
AWS Media Services 最新サービスアップデート 2024
eijikominami
0
190
マルチモーダル / AI Agent / LLMOps 3つの技術トレンドで理解するLLMの今後の展望
hirosatogamo
37
12k
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
280
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
A Philosophy of Restraint
colly
203
16k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
120
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Building an army of robots
kneath
302
43k
Documentation Writing (for coders)
carmenintech
65
4.4k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
For a Future-Friendly Web
brad_frost
175
9.4k
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