Download - Concurrency in Elixir with OTP
![Page 1: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/1.jpg)
Concurrency in Elixir w ith OTP
Presented forCode & Supply
![Page 2: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/2.jpg)
You dow n w ith OTP?
![Page 3: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/3.jpg)
You dow n w ith OTP?
Yeah you know me!
![Page 4: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/4.jpg)
Open
Telecom
Platform
W hat is OTP?
![Page 5: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/5.jpg)
Open
Telecom
Platform
W hat is OTP?Open source project
Developed by Ericsson, opened in 1998
18 years proven
https://github.com/erlang/otp
![Page 6: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/6.jpg)
Open
Telecom
Platform
W hat is OTP?Fault tolerant
Distributed
Concurrent
![Page 7: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/7.jpg)
Open
Telecom
Platform
W hat is OTP?Design principles for writing concurrent applications in Erlang
Libraries
![Page 8: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/8.jpg)
![Page 9: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/9.jpg)
HistoryxgenIntegrates Elixir, Mix and OTP
Merged into Elixir
Available in Elixir 0.13.3 and higher
https://github.com/josevalim/xgen
![Page 10: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/10.jpg)
ComponentsApplication
Agents
Tasks
Supervisors
GenServers
GenEvents
![Page 11: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/11.jpg)
Concurrency
![Page 12: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/12.jpg)
Starts Supervisors
Application# mix.exsdef application do [mod: { CatFeed, [] }]end
# lib/cat_feed.exdefmodule CatFeed do use Application
def start(_type, _args) do CatFeed.Supervisor.start_link endend
![Page 13: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/13.jpg)
Supervises processes
Supervisor# lib/cat_feed/supervisor.exdefmodule CatFeed.Supervisor do use Supervisor
def start_link do Supervisor.start_link(__MODULE__, :ok) end
def init(:ok) do children = [ worker(CatFeed.FeederLady) ] supervise children, strategy :one_for_one endend
![Page 14: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/14.jpg)
Supervises processes
Supervisor# lib/cat_feed/supervisor.exdefmodule CatFeed.Supervisor do use Supervisor
def start_link do Supervisor.start_link(__MODULE__, :ok) end
def init(:ok) do children = [ worker(CatFeed.FeederLady) ] supervise children, strategy :one_for_one endend
![Page 15: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/15.jpg)
one_for_one
If a child process terminates, only that process is restarted.
Restart Strategies
![Page 16: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/16.jpg)
one_for_one
If a child process terminates, only that process is restarted.
Restart Strategies
![Page 17: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/17.jpg)
one_for_one
If a child process terminates, only that process is restarted.
Restart Strategies
![Page 18: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/18.jpg)
rest_for_one
Restarts all processes started after one that failed
Restart Strategies
![Page 19: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/19.jpg)
rest_for_one
Restarts all processes started after one that failed
Restart Strategies
![Page 20: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/20.jpg)
rest_for_one
Restarts all processes started after one that failed
Restart Strategies
![Page 21: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/21.jpg)
one_for_all
Restarts all processes if any fail
Restart Strategies
![Page 22: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/22.jpg)
one_for_all
Restarts all processes if any fail
Restart Strategies
![Page 23: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/23.jpg)
one_for_all
Restarts all processes if any fail
Restart Strategies
![Page 24: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/24.jpg)
Handles state and does things
GenServer# lib/cat_feed/feeder_lady.exdefmodule CatFeed.FeederLady do use Server
# some stuff here …
def handle_call({:fill, bowl}, _from, state) do {:reply, CatFeed.BowlAgent.put(bowl, :food, state), state} # or HashDict.put(bowl, key, value) endend
![Page 25: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/25.jpg)
Specialized for storing state
Agent# lib/cat_feed/bowl_agent.exdefmodule CatFeed.BowlAgent do def start_link do # set initial state Agent.start_link(fn -> HashDict.new end) end def get(bucket, key) do # get state Agent.get(bucket, &HashDict.get(&1, key)) end
def put(bowl, key, value) do # update state Agent.update(bowl, &HashDict.put(&1, key, value)) endend
![Page 26: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/26.jpg)
![Page 27: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/27.jpg)
Handled in supervisor
BEAM processes
Spread across machines seamlessly
Concurrency
![Page 28: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/28.jpg)
# lib/cat_feed/supervisor.exdefmodule CatFeed.Supervisor do use Supervisor
def start_link do Supervisor.start_link(__MODULE__, :ok) end
def init(:ok) do children = [ worker(CatFeed.FeederLady) ] supervise children, strategy :one_for_one endend
Concurrency
![Page 29: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/29.jpg)
# lib/cat_feed/supervisor.exdefmodule CatFeed.Supervisor do use Supervisor
# def start_link do // folded
def init(:ok) do children = [ worker(CatFeed.FeederLady), worker(CatFeed.FeederLady), CatFeed.Supervisor.start_link ] supervise children, strategy :one_for_one endend
Concurrency
![Page 30: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/30.jpg)
RecapOTP
Based on Erlang OTP
Supervision tree
Restart strategies
State managed by select entities
![Page 31: Concurrency in Elixir with OTP](https://reader033.vdocuments.mx/reader033/viewer/2022051400/54c330aa4a7959ec1b8b45ff/html5/thumbnails/31.jpg)
Concurrency in Elixir w ith OTP
Presented forCode & Supply
http://codeandsupply.co
@justinxreese