concurrency in elixir with otp

31
Concurrency in Elixir with OTP Presented for Code & Supply

Upload: justin-reese

Post on 24-Jan-2015

197 views

Category:

Software


2 download

DESCRIPTION

A brief overview of concurrency in Elixir using OTP.

TRANSCRIPT

Page 1: Concurrency in Elixir with OTP

Concurrency in Elixir w ith OTP

Presented forCode & Supply

Page 2: Concurrency in Elixir with OTP

You dow n w ith OTP?

Page 3: Concurrency in Elixir with OTP

You dow n w ith OTP?

Yeah you know me!

Page 4: Concurrency in Elixir with OTP

Open

Telecom

Platform

W hat is OTP?

Page 5: Concurrency in Elixir with 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

Page 6: Concurrency in Elixir with OTP

Open

Telecom

Platform

W hat is OTP?Fault tolerant

Distributed

Concurrent

Page 7: Concurrency in Elixir with OTP

Open

Telecom

Platform

W hat is OTP?Design principles for writing concurrent applications in Erlang

Libraries

Page 8: Concurrency in Elixir with OTP
Page 9: Concurrency in Elixir with OTP

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

ComponentsApplication

Agents

Tasks

Supervisors

GenServers

GenEvents

Page 11: Concurrency in Elixir with OTP

Concurrency

Page 12: Concurrency in Elixir with OTP

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

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

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

one_for_one

If a child process terminates, only that process is restarted.

Restart Strategies

Page 16: Concurrency in Elixir with OTP

one_for_one

If a child process terminates, only that process is restarted.

Restart Strategies

Page 17: Concurrency in Elixir with OTP

one_for_one

If a child process terminates, only that process is restarted.

Restart Strategies

Page 18: Concurrency in Elixir with OTP

rest_for_one

Restarts all processes started after one that failed

Restart Strategies

Page 19: Concurrency in Elixir with OTP

rest_for_one

Restarts all processes started after one that failed

Restart Strategies

Page 20: Concurrency in Elixir with OTP

rest_for_one

Restarts all processes started after one that failed

Restart Strategies

Page 21: Concurrency in Elixir with OTP

one_for_all

Restarts all processes if any fail

Restart Strategies

Page 22: Concurrency in Elixir with OTP

one_for_all

Restarts all processes if any fail

Restart Strategies

Page 23: Concurrency in Elixir with OTP

one_for_all

Restarts all processes if any fail

Restart Strategies

Page 24: Concurrency in Elixir with OTP

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

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
Page 27: Concurrency in Elixir with OTP

Handled in supervisor

BEAM processes

Spread across machines seamlessly

Concurrency

Page 28: Concurrency in Elixir with OTP

# 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

# 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

RecapOTP

Based on Erlang OTP

Supervision tree

Restart strategies

State managed by select entities

Page 31: Concurrency in Elixir with OTP

Concurrency in Elixir w ith OTP

Presented forCode & Supply

http://codeandsupply.co

@justinxreese