elixir and otp

49
Pedro Medeiros Software Engineer @ Globo.com /pedrosnk /pesnk Elixir and OTP

Upload: pedro-medeiros

Post on 09-Feb-2017

194 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Elixir and OTP

Pedro Medeiros

Software Engineer @ Globo.com

/pedrosnk /pesnk

Elixir and OTP

Page 2: Elixir and OTP

Briefing

➔ Introduction to Elixir➔ Processes (concurrency model)➔ OTP➔ GenServer, Supervisor, Application

Page 3: Elixir and OTP

Elixir

Page 4: Elixir and OTP

ElixirFunctional, meta-programming aware language built on top of the Erlang VM

Page 5: Elixir and OTP

ElixirFunctional, meta-programming aware language built on top of the Erlang VM

Page 6: Elixir and OTP

ElixirSample

defmodule Fibonacci do

def fib(0), do: 0

def fib(1), do: 1

def fib(n), do

fib(n-1) + fib(n-2)

end

end

IO.puts Fibonacci.fib(10)

Page 7: Elixir and OTP

Disclaimer

Page 8: Elixir and OTP

Disclaimer

Elixir is not about a better syntax to the Erlang VM.

Page 9: Elixir and OTP

1. Extensibility

2. Productivity

3. CompatibilityElixir Design Goals

Page 10: Elixir and OTP

Types and modules polymorphismExtensibility

Page 11: Elixir and OTP

Extensibility

Process

Data 1 Data 2

data.value

Page 12: Elixir and OTP

Extensibility

Process

Process

Process

Call.here?

Page 13: Elixir and OTP

Extensibility

Process

Message 1 Message 2

JSON.decode

Page 14: Elixir and OTP
Page 15: Elixir and OTP

Productivity1. Mix2. Hex3. Docs4. Test

Great tooling

Page 16: Elixir and OTP

Productivity

Mix

Page 17: Elixir and OTP

Productivity

Hex

Page 18: Elixir and OTP

Productivity

Docs

Page 19: Elixir and OTP

Productivity

Test

Page 20: Elixir and OTP

Productivity

Test

defmodule Fibonacci do

@doc """

Fibonacci

iex> Fibonacci.fib(10)

55

"""

def fib(0), do: 0

def fib(1), do: 1

def fib(n) do

fib(n - 1) + fib(n - 2)

end

end

Page 21: Elixir and OTP

Productivity

Summary

$ mix new foobar$ cd foobar$ mix test$ mix hex.publish$ mix hex.docs

Page 22: Elixir and OTP

Compatibility Elixir and Erlang sitting on a tree

Page 23: Elixir and OTP

Compatibility

Elixir calls Erlang

Page 24: Elixir and OTP

Compatibility

Erlang calls Elixir

Page 25: Elixir and OTP

Process 101

Basic concurrency model.

Page 26: Elixir and OTP

Process

def ExpensiveProcess do

def loop do

receive do

{:operation, {sender, operation}} ->

operation = expensive_method(operation)

send sender, {:ok, operation}

loop()

:stop ->

:error

_ ->

loop()

end

end

end

Page 27: Elixir and OTP

ProcessA P

B

C

Page 28: Elixir and OTP

ProcessA P

B

C

Page 29: Elixir and OTP

ProcessA P

B

C

Page 30: Elixir and OTP

ProcessA P

B

C

Page 31: Elixir and OTP

OTP

Open Telecom Platform

Page 32: Elixir and OTP

1. Built with the Erlang VM2. Super Toolset of tools to

build distributed fault-tolerant applications.

OTP

Open Telecom Platform

Page 33: Elixir and OTP

OTP

What’s the problem?

Page 34: Elixir and OTP

OTP

Switch

Switch

Page 35: Elixir and OTP

OTP

Server

Browser, Mobile, IoTsEndpoints

Page 36: Elixir and OTP

OTP

Open Telecom Platform

1. GenServer2. Supervisor3. Application

Behaviours

Page 37: Elixir and OTP

OTP

GenServer

defmodule MyServer do

use GenServer

def handle_call({:pop, _from, [item | state]}) do

{:reply, item, state}

End

def handle_cast({:push, item}, state) do

{:noreply, [item | state]}

End

def handle_info(:log, state) do

IO.puts("State of server is #{inspect(state)}")

{:noreply, state}

end

end

Page 38: Elixir and OTP

OTP

GenServer

defmodule MyServer do

use GenServer

def handle_call({:pop, _from, [item | state]}) do

{:reply, item, state}

End

def handle_cast({:push, item}, state) do

{:noreply, [item | state]}

End

def handle_info(:log, state) do

IO.puts("State of server is #{inspect(state)}")

{:noreply, state}

end

end

synchronous

asynchronous

message

Page 39: Elixir and OTP

OTP

Supervisor

Supervisor

S1 S2 S3

● Watches its children.● Manage its children based on a

strategy.● :one_for_one, :rest_for_all, :

rest_for_one

Page 40: Elixir and OTP

OTP

Supervisor

Supervisor

S1 S3

Strategy :one_for_one

:one_for_one

Supervisor

S1 S3S1

Page 41: Elixir and OTP

OTP

Supervisor

Supervisor

S1 S3

Strategy :rest_for_all

:rest_for_all

Supervisor

S1 S3S1

Page 42: Elixir and OTP

OTP

Supervisor

Supervisor

S1 S3

Strategy :rest_for_one

:rest_for_all

Supervisor

S1 S3S1

Page 43: Elixir and OTP

OTP

Supervisor

defmodule MyServer.Supervisor do

use Supervisor

def start_link do

Supervisor.start_link(__MODULE__, [])

end

def init([]) do

children = [

worker(MyServer, []),

worker(MyServer2, [])

]

supervise(children, strategy: :one_for_one)

end

end

Page 44: Elixir and OTP

OTP

Applicaton

Application is where the top supervisors are initialized.

● Small code.● Initialize supervisors to

“live forever”

Page 45: Elixir and OTP

OTP

Applicaton

Page 46: Elixir and OTP

OTP

Applicaton

defmodule Logger.App do

use Application

def start(_type, _args) do

import Supervisor.Spec

# ...

options = [strategy: :rest_for_one, name: Logger.

Supervisor]

children = [worker(GenEvent, [[name: Logger]]),

worker(Logger.Watcher, [Logger, Logger.Config, []],

[id: Logger.Config, function: :watcher]),

supervisor(Logger.Watcher, [Logger.Config, :handlers, []]),

worker(Logger.Watcher,

[:error_logger, Logger.ErrorHandler,

{otp_reports?, sasl_reports?, threshold},

:link],

[id: Logger.ErrorHandler, function: :

watcher])]

case Supervisor.start_link(children, options) do

{:ok, sup} ->

{:ok, sup, config}

{:error, _} = error ->

error

end

end

Page 47: Elixir and OTP

Remember this picture?

Server

Browser, Mobile, IoTsEndpoints

Page 48: Elixir and OTP

Thanks =D

Page 49: Elixir and OTP

Recursos:

1. http://elixir-lang.org/2. https://hex.pm3. Programming Elixir Book4. The little Elixir and OTP Guidebook5. slideshare.net/pemedeiros