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
370
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
800
A Brief History of Time in Riak
seancribbs
4
1.7k
Other Decks in Technology
See All in Technology
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
100
OpenShift Virtualizationのネットワーク構成を真剣に考えてみた/OpenShift Virtualization's Network Configuration
tnk4on
0
130
バクラクのドキュメント解析技術と実データにおける課題 / layerx-ccc-winter-2024
shimacos
2
1k
社外コミュニティで学び社内に活かす共に学ぶプロジェクトの実践/backlogworld2024
nishiuma
0
250
Snykで始めるセキュリティ担当者とSREと開発者が楽になる脆弱性対応 / Getting started with Snyk Vulnerability Response
yamaguchitk333
2
180
サイバー攻撃を想定したセキュリティガイドライン 策定とASM及びCNAPPの活用方法
syoshie
3
1.2k
1等無人航空機操縦士一発試験 合格までの道のり ドローンミートアップ@大阪 2024/12/18
excdinc
0
150
社内イベント管理システムを1週間でAKSからACAに移行した話し
shingo_kawahara
0
180
日本版とグローバル版のモバイルアプリ統合の開発の裏側と今後の展望
miichan
1
120
WACATE2024冬セッション資料(ユーザビリティ)
scarletplover
0
190
CustomCopを使ってMongoidのコーディングルールを整えてみた
jinoketani
0
220
LINEヤフーのフロントエンド組織・体制の紹介【24年12月】
lycorp_recruit_jp
0
530
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
0
94
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
440
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
The Language of Interfaces
destraynor
154
24k
Building Your Own Lightsaber
phodgson
103
6.1k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.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