concurrency in elixir with otp
DESCRIPTION
A brief overview of concurrency in Elixir using OTP.TRANSCRIPT
Concurrency in Elixir w ith OTP
Presented forCode & Supply
You dow n w ith OTP?
You dow n w ith OTP?
Yeah you know me!
Open
Telecom
Platform
W hat is OTP?
Open
Telecom
Platform
W hat is OTP?Open source project
Developed by Ericsson, opened in 1998
18 years proven
https://github.com/erlang/otp
Open
Telecom
Platform
W hat is OTP?Fault tolerant
Distributed
Concurrent
Open
Telecom
Platform
W hat is OTP?Design principles for writing concurrent applications in Erlang
Libraries
HistoryxgenIntegrates Elixir, Mix and OTP
Merged into Elixir
Available in Elixir 0.13.3 and higher
https://github.com/josevalim/xgen
ComponentsApplication
Agents
Tasks
Supervisors
GenServers
GenEvents
Concurrency
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
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
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
one_for_one
If a child process terminates, only that process is restarted.
Restart Strategies
one_for_one
If a child process terminates, only that process is restarted.
Restart Strategies
one_for_one
If a child process terminates, only that process is restarted.
Restart Strategies
rest_for_one
Restarts all processes started after one that failed
Restart Strategies
rest_for_one
Restarts all processes started after one that failed
Restart Strategies
rest_for_one
Restarts all processes started after one that failed
Restart Strategies
one_for_all
Restarts all processes if any fail
Restart Strategies
one_for_all
Restarts all processes if any fail
Restart Strategies
one_for_all
Restarts all processes if any fail
Restart Strategies
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
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
Handled in supervisor
BEAM processes
Spread across machines seamlessly
Concurrency
# 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
# 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
RecapOTP
Based on Erlang OTP
Supervision tree
Restart strategies
State managed by select entities
Concurrency in Elixir w ith OTP
Presented forCode & Supply
http://codeandsupply.co
@justinxreese